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 #814

Open
wants to merge 4 commits 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
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
django-debug-toolbar==3.2.4
django-crispy-forms==1.14.0
django-crispy-forms==1.14.0
crispy_bootstrap4
44 changes: 44 additions & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django import forms
from .models import Car, Driver
from django.core.exceptions import ValidationError


class LicenseNumberValidationMixin:
def clean_license_number(self):
license_number = self.cleaned_data.get("license_number")
if len(license_number) != 8:
raise ValidationError(
"License number must consist of exactly 8 characters."
)
first_three = license_number[:3]
if not first_three.isupper() or not first_three.isalpha():
raise ValidationError(
"The first 3 characters must be uppercase letters."
)
if not license_number[3:].isdigit():
raise ValidationError("The last 5 characters must be digits.")
return license_number


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

class Meta:
model = Car
fields = ["model", "manufacturer", "drivers"]


class DriverForm(LicenseNumberValidationMixin, forms.ModelForm):
class Meta:
model = Driver
fields = ["username", "first_name", "last_name", "license_number"]


class DriverLicenseUpdateForm(LicenseNumberValidationMixin, forms.ModelForm):
class Meta:
model = Driver
fields = ["license_number"]
27 changes: 27 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
DriverCreateView,
DriverDeleteView,
DriverUpdateView,
assign_driver,
remove_driver,
)

urlpatterns = [
Expand Down Expand Up @@ -46,6 +51,28 @@
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path("drivers/create/", DriverCreateView.as_view(), name="driver-create"),
path(
"drivers/<int:pk>/delete/",
DriverDeleteView.as_view(),
name="driver-delete"
),
path(
"driver/<int:pk>/update/",
DriverUpdateView.as_view(),
name="driver-update"
),
path("car/<int:pk>/", CarDetailView.as_view(), name="car-detail"),
path(
"car/<int:car_id>/assign-driver/",
assign_driver,
name="assign-driver"
),
path(
"car/<int:car_id>/remove-driver/",
remove_driver,
name="remove-driver"
),
]

app_name = "taxi"
61 changes: 58 additions & 3 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Driver, Car, Manufacturer
from .forms import DriverLicenseUpdateForm, CarCreateForm


@login_required
Expand Down Expand Up @@ -61,16 +62,24 @@ class CarListView(LoginRequiredMixin, generic.ListView):
class CarDetailView(LoginRequiredMixin, generic.DetailView):
model = Car

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
car = self.object
context["is_driver"] = self.request.user in car.drivers.all()
return context


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


class CarUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Car
fields = "__all__"
form_class = CarCreateForm
template_name = "taxi/car_form.html"
success_url = reverse_lazy("taxi:car-list")


Expand All @@ -87,3 +96,49 @@ 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
fields = [
"username",
"first_name",
"last_name",
"license_number",
"password"
]
template_name = "taxi/driver_form.html"
success_url = reverse_lazy("taxi:driver-list")

def form_valid(self, form):
driver = form.save(commit=False)
driver.set_password(form.cleaned_data["password"])
driver.save()
return super().form_valid(form)


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


class DriverUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
form_class = DriverLicenseUpdateForm
template_name = "taxi/driver_license_update_form.html"
success_url = reverse_lazy("taxi:driver-list")


@login_required
def assign_driver(request, car_id):
car = get_object_or_404(Car, pk=car_id)
car.drivers.add(request.user)
return redirect("taxi:car-detail", pk=car.id)


@login_required
def remove_driver(request, car_id):
car = get_object_or_404(Car, pk=car_id)
car.drivers.remove(request.user)
return redirect("taxi:car-detail", pk=car.id)
1 change: 1 addition & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"crispy_bootstrap4",
"debug_toolbar",
"crispy_forms",
"taxi",
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Add additional CSS in static file -->

{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
Expand Down
13 changes: 13 additions & 0 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ <h1>Drivers</h1>
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
</ul>

{% if not is_driver %}
<form action="{% url 'taxi:assign-driver' car.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Assign me to this car" class="btn btn-success">
</form>
{% else %}
<form action="{% url 'taxi:remove-driver' car.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete me from this car" class="btn btn-warning">
</form>
{% endif %}

{% endblock %}
10 changes: 10 additions & 0 deletions templates/taxi/driver_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.html' %}

{% block content %}
<h1>Are you sure you want to delete "{{ object }}"?</h1>
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Confirm Delete</button>
<a href="{% url 'taxi:driver-list' %}" class="btn btn-secondary">Cancel</a>
</form>
{% endblock %}
5 changes: 5 additions & 0 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ <h4>Cars</h4>
<p>No cars!</p>
{% endfor %}
</div>

<div class="mt-3">
<a href="{% url 'taxi:driver-update' driver.id %}" class="btn btn-primary">Update License</a>
<a href="{% url 'taxi:driver-delete' driver.id %}" class="btn btn-danger">Delete Driver</a>
</div>
{% endblock %}
11 changes: 11 additions & 0 deletions templates/taxi/driver_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}
<h1>Create a New Driver</h1>
<form method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Save" class="btn btn-primary">
</form>
{% endblock %}
10 changes: 10 additions & 0 deletions templates/taxi/driver_license_update_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "base.html" %}

{% block content %}
<h1>Update Driver License</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Update License</button>
</form>
{% endblock %}
2 changes: 2 additions & 0 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{% block content %}
<h1>Driver List
</h1>

<a href="{% url 'taxi:driver-create' %}" class="btn btn-primary mb-3">Create New Driver</a>

{% if driver_list %}
<table class="table">
Expand Down