Skip to content

Commit

Permalink
'Solution'
Browse files Browse the repository at this point in the history
  • Loading branch information
Julia15031988 committed Jan 4, 2025
1 parent 74effaf commit 894639f
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 21 deletions.
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
52 changes: 52 additions & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django import forms
from .models import Driver, Car
import re
from django.contrib.auth.forms import UserCreationForm


class DriverLicenseUpdateForm(forms.ModelForm):
class Meta:
model = Driver
fields = ('license_number',)

def clean_license_number(self):
license_number = self.cleaned_data.get('license_number')

if len(license_number) != 8:
raise forms.ValidationError("license_number must have 8 characters")

if not re.match(r'^[A-Z]{3}\d{5}$', license_number):
raise forms.ValidationError("license_number must begin with 3 Uppercase letters and then 5 digits")

return license_number


class DriverCreationForm(UserCreationForm):
license_number = forms.CharField(max_length=255)

class Meta:
model = Driver
fields = UserCreationForm.Meta.fields

def clean_license_number(self):
license_number = self.cleaned_data.get('license_number')

if len(license_number) != 8:
raise forms.ValidationError("license_number must have 8 characters")

if not re.match(r'^[A-Z]{3}\d{5}$', license_number):
raise forms.ValidationError("license_number must begin with 3 Uppercase letters and then 5 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 = '__all__'
13 changes: 13 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
DriverCreateView,
DriverDeleteView,
DriverLicenseUpdateView,
AssignMeToCarView,
RemoveMeFromCarView,
DriverUpdateView,
)

urlpatterns = [
Expand Down Expand Up @@ -46,6 +52,13 @@
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path('driver/create/', DriverCreateView.as_view(), name='driver-create'),
path('driver/<int:pk>/delete/', DriverDeleteView.as_view(), name='driver-delete'),
path('driver/<int:pk>/update_license/', DriverLicenseUpdateView.as_view(), name='driver-license-update'),
path("car/<int:pk>/assign-me/", AssignMeToCarView.as_view(), name="assign-me-to-car"),
path("car/<int:pk>/remove-me/", RemoveMeFromCarView.as_view(), name="remove-me-from-car"),
path('driver/<int:pk>/update/', DriverUpdateView.as_view(), name='driver-update'),

]

app_name = "taxi"
67 changes: 64 additions & 3 deletions taxi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

from .forms import DriverLicenseUpdateForm, DriverCreationForm, CarCreateForm
from .models import Driver, Car, Manufacturer
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect


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

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


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


Expand All @@ -87,3 +90,61 @@ 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 = DriverCreationForm
template_name = 'taxi/driver_create.html'
success_url = reverse_lazy("taxi:driver-list")

def form_valid(self, form):
return super().form_valid(form)

def form_invalid(self, form):
return super().form_invalid(form)


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


class DriverLicenseUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
form_class = DriverLicenseUpdateForm
template_name = 'taxi/driver_license_update.html'
success_url = reverse_lazy('taxi:driver-list')

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

def form_valid(self, form):
messages.success(self.request, "license updated successfully.")
return super().form_valid(form)


class AssignMeToCarView(LoginRequiredMixin, generic.View):
def post(self, request, pk):
car = get_object_or_404(Car, pk=pk)
car.drivers.add(request.user)
return redirect(reverse("taxi:car-detail", kwargs={"pk": pk}))


class RemoveMeFromCarView(LoginRequiredMixin, generic.View):
def post(self, request, pk):
car = get_object_or_404(Car, pk=pk)
car.drivers.remove(request.user)
return redirect(reverse("taxi:car-detail", kwargs={"pk": pk}))


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

def form_valid(self, form):
messages.success(self.request, "Driver information updated successfully.")
return super().form_valid(form)
1 change: 1 addition & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"debug_toolbar",
"crispy_bootstrap4",
"crispy_forms",
"taxi",
]
Expand Down
12 changes: 12 additions & 0 deletions templates/taxi/car_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block content %}
<h1>Create a New Car</h1>

<form method="post">
{% csrf_token %}
{{ form.as_p }}

<button type="submit">Save</button>
</form>
{% endblock %}
51 changes: 34 additions & 17 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
{% extends "base.html" %}

{% block content %}
<h1>
{{ car.model }}
<a href="{% url 'taxi:car-delete' pk=car.id %}" class="btn btn-danger link-to-page">
Delete
</a>

<a href="{% url 'taxi:car-update' pk=car.id %}" class="btn btn-secondary link-to-page">
Update
</a>
</h1>
<h1> MODEL: {{ car.model }}</h1>
<p>Manufacturer: {{ car.manufacturer.name }}</p>
<p>Year: {{ car.year }}</p>

<a href="{% url 'taxi:car-update' pk=car.id %}" class="btn btn-secondary">Update</a>
<a href="{% url 'taxi:car-delete' pk=car.id %}" class="btn btn-danger">Delete</a>

<p>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</p>
<h1>Drivers</h1>
<hr>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
</ul>

<h2>Drivers</h2>
<ul class="drivers-list">
{% for driver in car.drivers.all %}
<li class="driver-item">
<div><strong>Username:</strong> {{ driver.username }}</div>
<div><strong>First Name:</strong> {{ driver.first_name }}</div>
<div><strong>Last Name:</strong> {{ driver.last_name }}</div>
<div><strong>License:</strong> {{ driver.license_number }}</div>
</li>
{% endfor %}
</ul>


{% if user in car.drivers.all %}
<form method="post" action="{% url 'taxi:remove-me-from-car' pk=car.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Delete me from this car</button>
</form>
{% else %}
<form method="post" action="{% url 'taxi:assign-me-to-car' pk=car.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-success">Assign me to this car</button>
</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 {{ driver.name }}?</h1>
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Confirm Delete</button>
</form>
<a href="{% url 'taxi:driver-detail' driver.pk %}" class="btn btn-secondary">Cancel</a>
{% endblock %}
12 changes: 12 additions & 0 deletions templates/taxi/driver_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block content %}
<h1>Create a New DRIVER</h1>

<form method="post">
{% csrf_token %}
{{ form.as_p }}

<button type="submit">Save</button>
</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>


<a href="{% url 'taxi:driver-license-update-' driver.pk %}" class="btn btn-primary">Update License</a>

<a href="{% url 'taxi:driver-delete' driver.pk %}" class="btn btn-danger">Delete Driver</a>
{% endblock %}
13 changes: 13 additions & 0 deletions templates/taxi/driver_license_update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base.html' %}

{% block content %}
<h1>Update license</h1>

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Update license</button>
</form>

<a href="{% url 'driver-list' %}">Back to driver-list</a>
{% endblock %}
1 change: 1 addition & 0 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% block content %}
<h1>Driver List
</h1>
<a href="{% url 'taxi:driver-create' %}" class="btn btn-primary">Create NEW Driver</a>

{% if driver_list %}
<table class="table">
Expand Down
10 changes: 10 additions & 0 deletions templates/taxi/driver_update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.html' %}

{% block content %}
<h2>Edit Driver Information</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
{% endblock %}

0 comments on commit 894639f

Please sign in to comment.