-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46d9023
commit a243df3
Showing
9 changed files
with
92 additions
and
14 deletions.
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
venv/ | ||
.pytest_cache/ | ||
**__pycache__/ | ||
/db.sqlite3 |
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,47 +1,54 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import render | ||
from django.views import generic | ||
|
||
from .models import Driver, Car, Manufacturer | ||
|
||
|
||
def index(request): | ||
@login_required | ||
def index(request: HttpRequest) -> HttpResponse: | ||
"""View function for the home page of the site.""" | ||
|
||
num_drivers = Driver.objects.count() | ||
num_cars = Car.objects.count() | ||
num_manufacturers = Manufacturer.objects.count() | ||
num_visits = request.session.get("num_visits", 0) + 1 | ||
request.session["num_visits"] = num_visits | ||
|
||
context = { | ||
"num_drivers": num_drivers, | ||
"num_cars": num_cars, | ||
"num_manufacturers": num_manufacturers, | ||
"num_visits": num_visits, | ||
} | ||
|
||
return render(request, "taxi/index.html", context=context) | ||
|
||
|
||
class ManufacturerListView(generic.ListView): | ||
class ManufacturerListView(LoginRequiredMixin, generic.ListView): | ||
model = Manufacturer | ||
context_object_name = "manufacturer_list" | ||
template_name = "taxi/manufacturer_list.html" | ||
paginate_by = 5 | ||
|
||
|
||
class CarListView(generic.ListView): | ||
class CarListView(LoginRequiredMixin, generic.ListView): | ||
model = Car | ||
paginate_by = 5 | ||
queryset = Car.objects.select_related("manufacturer") | ||
|
||
|
||
class CarDetailView(generic.DetailView): | ||
class CarDetailView(LoginRequiredMixin, generic.DetailView): | ||
model = Car | ||
|
||
|
||
class DriverListView(generic.ListView): | ||
class DriverListView(LoginRequiredMixin, generic.ListView): | ||
model = Driver | ||
paginate_by = 5 | ||
|
||
|
||
class DriverDetailView(generic.DetailView): | ||
class DriverDetailView(LoginRequiredMixin, generic.DetailView): | ||
model = Driver | ||
queryset = Driver.objects.prefetch_related("cars__manufacturer") |
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
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,6 +1,16 @@ | ||
<ul class="sidebar-nav"> | ||
<li><a href="{% url "taxi:index" %}">Home page</a></li> | ||
<li><a href="{% url "taxi:manufacturer-list" %}">Manufacturers</a></li> | ||
<li><a href="{% url "taxi:car-list" %}">Cars</a></li> | ||
<li><a href="{% url "taxi:driver-list" %}">Drivers</a></li> | ||
{% if user.is_authenticated %} | ||
<li><a href="{% url 'taxi:driver-detail' user.id %}">User: {{ user.username }}</a></li> | ||
{% else %} | ||
<li><a href="{% url 'login' %}">Login</a></li> | ||
{% endif %} | ||
<br> | ||
<li><a href="{% url 'taxi:index' %}">Home page</a></li> | ||
<li><a href="{% url 'taxi:manufacturer-list' %}">Manufacturers</a></li> | ||
<li><a href="{% url 'taxi:car-list' %}">Cars</a></li> | ||
<li><a href="{% url 'taxi:driver-list' %}">Drivers</a></li> | ||
<br> | ||
{% if user.is_authenticated %} | ||
<li><a href="{% url 'logout' %}">Logout</a></li> | ||
{% endif %} | ||
</ul> |
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,35 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
|
||
{% if form.errors %} | ||
<p>Your username and password didn't match. Please try again.</p> | ||
{% endif %} | ||
|
||
{% if next %} | ||
{% if user.is_authenticated %} | ||
<p>Your account doesn't have access to this page. To proceed, | ||
please login with an account that has access.</p> | ||
{% else %} | ||
<p>Please login to see this page.</p> | ||
{% endif %} | ||
{% endif %} | ||
|
||
<form method="post" action="{% url 'login' %}"> | ||
{% csrf_token %} | ||
<table> | ||
<tr> | ||
<td>{{ form.username.label_tag }}</td> | ||
<td>{{ form.username }}</td> | ||
</tr> | ||
<tr> | ||
<td>{{ form.password.label_tag }}</td> | ||
<td>{{ form.password }}</td> | ||
</tr> | ||
</table> | ||
|
||
<input type="submit" value="login"> | ||
<input type="hidden" name="next" value="{{ next }}"> | ||
</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