Skip to content

Commit

Permalink
Merge pull request #3 from lukchm94/tennis-app
Browse files Browse the repository at this point in the history
Tennis app
  • Loading branch information
lukchm94 authored Jun 6, 2024
2 parents 233b81f + c884902 commit 49b8587
Show file tree
Hide file tree
Showing 60 changed files with 316 additions and 94 deletions.
77 changes: 0 additions & 77 deletions app/calculator/__calc_config.py

This file was deleted.

21 changes: 13 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ dev = [
]

[tool.pdm.scripts]
prod = 'pdm run python app/manage.py runserver'
migrate = 'pdm run python app/manage.py migrate'
make_migration = 'pdm run python app/manage.py makemigrations'
test_calculator = "pdm run coverage run --source='.' app/manage.py test calculator"
tests = "pdm run coverage run --source='.' app/manage.py test"
coverage_report = 'pdm run coverage report'
check_code_format = 'black --check app/'
format_code = 'black app/'
prod = 'pdm run python src/manage.py runserver'

migrate = 'pdm run python src/manage.py migrate'
make_migration = 'pdm run python src/manage.py makemigrations'

check_code_format = 'black --check src/'
format_code = 'black src/'

test_app = "pdm run coverage run --source='.' src/manage.py test app"
test_calculator = "pdm run coverage run --source='.' src/manage.py test calculator"
test_tennis = "pdm run coverage run --source='.' src/manage.py test tennis"

coverage_report = 'pdm run coverage report'
15 changes: 15 additions & 0 deletions app/app/__app_configs.py → src/app/__app_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ def to_dict(cls):
class Paths(str, ValidationEnum):
root = "/"
admin = "admin"
admin_path = f"{admin}{root}"
calc = "calculator"
calc_path = f"{calc}{root}"
tennis = "tennis"
tennis_path = f"{tennis}{root}"
health = "health"
health_path = f"{health}{root}"


class HttpMethods(str, ValidationEnum):
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
HEAD = "HEAD"
OPTIONS = "OPTIONS"
PATCH = "PATCH"
TRACE = "TRACE"
CONNECT = "CONNECT"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions app/app/settings.py → src/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
# Application definition
INSTALLED_APPS = [
"calculator.apps.CalculatorConfig",
"tennis.apps.TennisConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand Down
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions src/app/tests/test_validation_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from enum import Enum

from ..__app_configs import ValidationEnum


class TestValidationEnum(unittest.TestCase):
def test_list(self):
class TestEnum(ValidationEnum):
A = "a"
B = "b"
C = "c"

self.assertEqual(TestEnum.list(), ["a", "b", "c"])

def test_to_list(self):
class TestEnum(ValidationEnum):
A = ["a"]
B = ["b"]
C = ["c"]

self.assertEqual(TestEnum.to_list(), ["a", "b", "c"])

def test_to_string(self):
class TestEnum(ValidationEnum):
A = "a"
B = "b"
C = "c"

self.assertEqual(TestEnum.to_string(), "a,b,c")

# Testing with custom separator
self.assertEqual(TestEnum.to_string(separator="|"), "a|b|c")

def test_to_dict(self):
class TestEnum(ValidationEnum):
A = "a"
B = "b"
C = "c"

self.assertEqual(TestEnum.to_dict(), {"A": "a", "B": "b", "C": "c"})
5 changes: 4 additions & 1 deletion app/app/urls.py → src/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
from django.urls import include, path

from .__app_configs import Paths
from .views.health import health

urlpatterns = [
path(Paths.calc_path.value, include("calculator.urls")),
path(Paths.admin.value, admin.site.urls),
path(Paths.tennis_path.value, include("tennis.urls")),
path(Paths.admin_path.value, admin.site.urls),
path(Paths.health_path.value, health, name=Paths.health.value),
]
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions src/calculator/__calc_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from app.__app_configs import ValidationEnum


class Paths(str, ValidationEnum):
none = ""
root = "/"
admin = "admin"
calc = "calculate"
enter_num = "enter_numbers"
enter_num_path = f"{enter_num}{root}"


class MathOperation(str, ValidationEnum):
add = "add"
subtract = "subtract"
multiply = "multiply"
divide = "divide"
none = ""


class Templates(str, ValidationEnum):
calculate = "calculator/calculate.html"
error = "calculator/invalid_operation.html"
enter = "calculator/enter_numbers.html"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions app/calculator/urls.py → src/calculator/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from django.urls import path

from .__calc_config import Paths
from .views import calculate, health
from .views import calculate

urlpatterns = [
path(Paths.enter_num.value, calculate.enter_numbers, name=Paths.enter_num.value),
path(Paths.none.value, calculate.calculate, name=Paths.calc.value),
path(Paths.health_path.value, health.health, name=Paths.health.value),
]
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from multiprocessing import context

from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.template import Template, loader

from ..__calc_config import CalcTemplates, HttpMethods, MathOperation
from app.__app_configs import HttpMethods

from ..__calc_config import MathOperation, Templates
from ..__exceptions import DivisionByZeroError, OperationError
from ..service.operations import Calculate, get_error_context


def enter_numbers(req: HttpRequest) -> HttpResponse:
template: Template = loader.get_template("calculator/enter_numbers.html")
template: Template = loader.get_template(Templates.enter.value)
context: dict = {
"allowed_operations": [
operation
Expand All @@ -37,11 +37,11 @@ def calculate(req: HttpRequest) -> HttpResponse:
operation = req.POST.get("operation", MathOperation.none.value)

try:
template: Template = loader.get_template(CalcTemplates.calculate.value)
template: Template = loader.get_template(Templates.calculate.value)
context: dict = Calculate(num1, num2, operation).get_context()

except (DivisionByZeroError, OperationError) as err:
template: Template = loader.get_template(CalcTemplates.error.value)
template: Template = loader.get_template(Templates.error.value)
context: dict = get_error_context(num1, num2, operation, err)

return HttpResponse(template.render(context, req))
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions src/tennis/__tennis_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from app.__app_configs import ValidationEnum


class Templates(str, ValidationEnum):
add_player = "tennis/add_player.html"
success = "tennis/success.html"
error = "tennis/error.html"


class Urls(str, ValidationEnum):
root = "/"
none = ""
success = "success"
success_path = f"{success}{root}"
add_player = "add_player"
3 changes: 3 additions & 0 deletions src/tennis/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions src/tennis/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TennisConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "tennis"
File renamed without changes.
11 changes: 11 additions & 0 deletions src/tennis/forms/player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django import forms

from ..models.player import TennisPlayer


class TennisPlayerForm(forms.ModelForm):
class Meta:
model = TennisPlayer
fields: list[str] = [
field.name for field in TennisPlayer._meta.fields if field.name != "id"
]
33 changes: 33 additions & 0 deletions src/tennis/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 5.0.6 on 2024-06-06 06:25

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="TennisPlayer",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("first_name", models.CharField(max_length=100)),
("last_name", models.CharField(max_length=100)),
("nationality", models.CharField(max_length=100)),
("date_of_birth", models.DateField()),
("racket_brand", models.CharField(max_length=100)),
("racket_model", models.CharField(max_length=100)),
],
),
]
Empty file.
Empty file added src/tennis/models/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions src/tennis/models/player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models


class TennisPlayer(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
nationality = models.CharField(max_length=100)
date_of_birth = models.DateField()
racket_brand = models.CharField(max_length=100)
racket_model = models.CharField(max_length=100)

def __str__(self):
return f"{self.first_name} {self.last_name}"
Empty file added src/tennis/services/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions src/tennis/services/add_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.forms import ModelForm
from django.http import HttpRequest
from django.shortcuts import redirect, render

from app.__app_configs import HttpMethods

from ..__tennis_config import Templates, Urls


class Add:
form: ModelForm
req: HttpRequest

def __init__(self, form: ModelForm, req: HttpRequest) -> None:
self.form = form
self.req = req

def _err_context(self) -> dict:
return {"err": self.form.errors}

def _get_context(self) -> dict:
return {"form": self.form}

def _post(self) -> None:
if self.form.is_valid():
self.form.save()
return redirect(Urls.success.value)
else:
return render(self.req, Templates.error.value, self._err_context())

def _get(self) -> None:
return render(self.req, Templates.add_player.value, self._get_context())

def save(self) -> None:
return (
self._post() if self.req.method == HttpMethods.POST.value else self._get()
)
Empty file.
Empty file.
Loading

0 comments on commit 49b8587

Please sign in to comment.