Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #828

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9,Q0,N8,VNE
exclude =
**migrations
venv
*/migrations/*
.venv
tests
57 changes: 57 additions & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm

from taxi.models import Driver, Car


def clean_license_number(license_number: str) -> str:
if (
len(license_number) != 8
or not license_number[:3].isupper()
or not license_number[3:].isdigit()
):
raise forms.ValidationError("License number is invalid")
return license_number


class DriverCreateForm(UserCreationForm):

license_number = forms.CharField(
required=True,
label="License Number",
validators=[clean_license_number],
)

class Meta:
model = Driver
fields = UserCreationForm.Meta.fields + (
"first_name",
"last_name",
"license_number",
)


class LicenseUpdateForm(forms.ModelForm):

license_number = forms.CharField(
required=True,
label="License Number",
validators=[clean_license_number],
)

class Meta:
model = Driver
fields = ("license_number",)


class CarCreateForm(forms.ModelForm):
drivers = forms.ModelMultipleChoiceField(
queryset=Driver.objects.all(),
required=False,
widget=forms.CheckboxSelectMultiple(),
label="Drivers",
)

class Meta:
model = Car
fields = "__all__"
173 changes: 144 additions & 29 deletions taxi/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,169 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [
migrations.CreateModel(
name='Driver',
name="Driver",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('license_number', models.CharField(max_length=255, unique=True)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("password", models.CharField(max_length=128, verbose_name="password")),
(
"last_login",
models.DateTimeField(
blank=True, null=True, verbose_name="last login"
),
),
(
"is_superuser",
models.BooleanField(
default=False,
help_text="Designates that this user has all permissions without explicitly assigning them.",
verbose_name="superuser status",
),
),
(
"username",
models.CharField(
error_messages={
"unique": "A user with that username already exists."
},
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
max_length=150,
unique=True,
validators=[
django.contrib.auth.validators.UnicodeUsernameValidator()
],
verbose_name="username",
),
),
(
"first_name",
models.CharField(
blank=True, max_length=150, verbose_name="first name"
),
),
(
"last_name",
models.CharField(
blank=True, max_length=150, verbose_name="last name"
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.",
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
default=True,
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
("license_number", models.CharField(max_length=255, unique=True)),
(
"groups",
models.ManyToManyField(
blank=True,
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
related_name="user_set",
related_query_name="user",
to="auth.Group",
verbose_name="groups",
),
),
(
"user_permissions",
models.ManyToManyField(
blank=True,
help_text="Specific permissions for this user.",
related_name="user_set",
related_query_name="user",
to="auth.Permission",
verbose_name="user permissions",
),
),
],
options={
'verbose_name': 'driver',
'verbose_name_plural': 'drivers',
"verbose_name": "driver",
"verbose_name_plural": "drivers",
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
("objects", django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Manufacturer',
name="Manufacturer",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
('country', models.CharField(max_length=255)),
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255, unique=True)),
("country", models.CharField(max_length=255)),
],
options={
'ordering': ['name'],
"ordering": ["name"],
},
),
migrations.CreateModel(
name='Car',
name="Car",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('model', models.CharField(max_length=255)),
('drivers', models.ManyToManyField(related_name='cars', to=settings.AUTH_USER_MODEL)),
('manufacturer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='taxi.manufacturer')),
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("model", models.CharField(max_length=255)),
(
"drivers",
models.ManyToManyField(
related_name="cars", to=settings.AUTH_USER_MODEL
),
),
(
"manufacturer",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="taxi.manufacturer",
),
),
],
),
]
34 changes: 33 additions & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
DriverCreateView,
DriverLicenseUpdateView,
DriverDeleteView,
car_add_driver,
car_delete_driver,
)

urlpatterns = [
Expand Down Expand Up @@ -44,7 +49,34 @@
path("cars/<int:pk>/delete/", CarDeleteView.as_view(), name="car-delete"),
path("drivers/", DriverListView.as_view(), name="driver-list"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
"drivers/<int:pk>/",
DriverDetailView.as_view(),
name="driver-detail"
),
path(
"drivers/create/",
DriverCreateView.as_view(),
name="driver-create"
),
path(
"drivers/<int:pk>/license_update/",
DriverLicenseUpdateView.as_view(),
name="driver-license-update",
),
path(
"drivers/<int:pk>/delete/",
DriverDeleteView.as_view(),
name="driver-delete"
),
path(
"car/<int:pk>/add_driver/",
car_add_driver,
name="car-add-driver"
),
path(
"car/<int:pk>/delete_driver/",
car_delete_driver,
name="car-delete-driver"
),
]

Expand Down
55 changes: 53 additions & 2 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

from .forms import DriverCreateForm, LicenseUpdateForm, CarCreateForm
from .models import Driver, Car, Manufacturer


Expand Down Expand Up @@ -64,7 +66,7 @@ class CarDetailView(LoginRequiredMixin, generic.DetailView):

class CarCreateView(LoginRequiredMixin, generic.CreateView):
model = Car
fields = "__all__"
form_class = CarCreateForm
success_url = reverse_lazy("taxi:car-list")


Expand All @@ -87,3 +89,52 @@ class DriverListView(LoginRequiredMixin, generic.ListView):
class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")


class DriverCreateView(LoginRequiredMixin, generic.CreateView):
model = Driver
form_class = DriverCreateForm


class DriverLicenseUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
form_class = LicenseUpdateForm

def get_success_url(self):
return reverse_lazy(
"taxi:driver-detail",
kwargs={"pk": self.object.pk}
)


class DriverDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Driver
success_url = reverse_lazy("taxi:driver-list")


def car_add_driver(request: HttpRequest, pk: int) -> HttpResponseRedirect:
car = get_object_or_404(Car, pk=pk)
if request.user.is_authenticated:
driver = request.user
car.drivers.add(driver)
return HttpResponseRedirect(
reverse_lazy
(
"taxi:car-detail",
kwargs={"pk": car.pk}
)
)


def car_delete_driver(request: HttpRequest, pk: int) -> HttpResponseRedirect:
car = get_object_or_404(Car, pk=pk)
if request.user.is_authenticated:
driver = request.user
car.drivers.remove(driver)
return HttpResponseRedirect(
reverse_lazy
(
"taxi:car-detail",
kwargs={"pk": car.pk}
)
)
Loading
Loading