-
Notifications
You must be signed in to change notification settings - Fork 791
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 #831
Open
tpavliohlo
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
tpavliohlo:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #831
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django import forms | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.forms import UserCreationForm | ||
from django.forms import ValidationError | ||
from taxi.models import Driver, Car | ||
|
||
|
||
class CarForm(forms.ModelForm): | ||
drivers = forms.ModelMultipleChoiceField( | ||
queryset=get_user_model().objects.all(), | ||
widget=forms.CheckboxSelectMultiple, | ||
required=False, | ||
) | ||
|
||
class Meta: | ||
model = Car | ||
fields = "__all__" | ||
|
||
|
||
class DriverCreationForm(UserCreationForm): | ||
class Meta(UserCreationForm.Meta): | ||
model = Driver | ||
fields = UserCreationForm.Meta.fields + ( | ||
"first_name", | ||
"last_name", | ||
"license_number", | ||
) | ||
|
||
def clean_license_number(self): | ||
return validate_license_number(self.cleaned_data["license_number"]) | ||
|
||
|
||
class DriverLicenseUpdateForm(forms.ModelForm): | ||
class Meta: | ||
model = Driver | ||
fields = ("license_number",) | ||
|
||
def clean_license_number(self): | ||
return validate_license_number(self.cleaned_data["license_number"]) | ||
|
||
|
||
def validate_license_number(license_number: str) -> str: | ||
|
||
if len(license_number) != 8: | ||
raise ValidationError("License must consist only of 8 characters") | ||
elif not license_number[:3].isalpha() or not license_number[:3].isupper(): | ||
raise ValidationError( | ||
"First 3 characters of license are not uppercase or letters" | ||
) | ||
elif not license_number[3:].isdigit(): | ||
raise ValidationError("Last 5 characters are not digits") | ||
return license_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.http import HttpResponseRedirect | ||
from django.shortcuts import render | ||
from django.urls import reverse_lazy | ||
from django.views import generic | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from taxi.forms import CarForm, DriverCreationForm, DriverLicenseUpdateForm | ||
|
||
|
||
from .forms import DriverCreationForm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import of |
||
from .models import Driver, Car, Manufacturer | ||
|
||
|
||
|
@@ -64,13 +68,13 @@ class CarDetailView(LoginRequiredMixin, generic.DetailView): | |
|
||
class CarCreateView(LoginRequiredMixin, generic.CreateView): | ||
model = Car | ||
fields = "__all__" | ||
form_class = CarForm | ||
success_url = reverse_lazy("taxi:car-list") | ||
|
||
|
||
class CarUpdateView(LoginRequiredMixin, generic.UpdateView): | ||
model = Car | ||
fields = "__all__" | ||
form_class = CarForm | ||
success_url = reverse_lazy("taxi:car-list") | ||
|
||
|
||
|
@@ -87,3 +91,30 @@ 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 | ||
success_url = reverse_lazy("taxi:driver-list") | ||
|
||
|
||
class DriverLicenseUpdateView(LoginRequiredMixin, generic.UpdateView): | ||
model = Driver | ||
form_class = DriverLicenseUpdateForm | ||
success_url = reverse_lazy("taxi:driver-list") | ||
|
||
|
||
class DriverDeleteView(LoginRequiredMixin, generic.DeleteView): | ||
model = Driver | ||
success_url = reverse_lazy("taxi:driver-list") | ||
|
||
|
||
@login_required | ||
def toggle_car(request, pk): | ||
driver = Driver.objects.get(id=request.user.id) | ||
if Car.objects.get(id=pk) in driver.cars.all(): | ||
driver.cars.remove(pk) | ||
else: | ||
driver.cars.add(pk) | ||
return HttpResponseRedirect(reverse_lazy("taxi:car-detail", args=[pk])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<h1>Delete Driver?</h1> | ||
<form action="" method="post"> | ||
{% csrf_token %} | ||
|
||
<input type="submit" value="Yes" class="btn btn-danger"> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends "base.html" %} | ||
{% load crispy_forms_filters %} | ||
|
||
{% block content %} | ||
<h1>{{ object|yesno:"Update,Create" }} driver</h1> | ||
<form action="" method="post" novalidate> | ||
{% csrf_token %} | ||
{{ form|crispy }} | ||
|
||
<input type="submit" value="Submit" class="btn btn-primary"> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
drivers
field inCarForm
usesget_user_model().objects.all()
as its queryset. Ensure that this is correct, as it might be more appropriate to useDriver.objects.all()
ifDriver
is a separate model from the user model. This aligns with the checklist point to useget_user_model()
only when appropriate.