Skip to content

Commit

Permalink
add task
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanRamyk committed Apr 12, 2022
1 parent 3d4a5ee commit 4585fdf
Show file tree
Hide file tree
Showing 32 changed files with 589 additions and 51 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

33 changes: 0 additions & 33 deletions .github/workflows/test.yml

This file was deleted.

14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Python boilerplate for GitHub tasks
# Taxi service authentication

- Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before start
- Implement the task described [here](app/main.py)

In this task, you will implement a visit counter, login/logout functionality, and make your site available only
authenticated users.

1. Implement a visit counter on the home screen. It should show how many times a user visited the home page before.

2. Create login/logout screens. Provide authentication to your project using built-in Django authentication.

3. Display the username on the top of the sidebar and login/logout button depending on if the user is authenticated.

4. Protect all your views from unauthenticated users using `login_required` decorator and `LoginRequiredMixin`.
3 changes: 0 additions & 3 deletions app/main.py

This file was deleted.

22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taxi_service.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == "__main__":
main()
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pytest==6.2.5
flake8==4.0.1
django==4.0.2
3 changes: 3 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
margin-top: 20px;
}
File renamed without changes.
23 changes: 23 additions & 0 deletions taxi/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Driver, Car, Manufacturer


@admin.register(Driver)
class DriverAdmin(UserAdmin):
list_display = UserAdmin.list_display + ("license_number",)
fieldsets = UserAdmin.fieldsets + (
(("Additional info", {"fields": ("license_number",)}),)
)
add_fieldsets = UserAdmin.add_fieldsets + (
(("Additional info", {"fields": ("first_name", "last_name", "license_number",)}),)
)


@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
search_fields = ("model",)
list_filter = ("manufacturer",)


admin.site.register(Manufacturer)
6 changes: 6 additions & 0 deletions taxi/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TaxiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "taxi"
63 changes: 63 additions & 0 deletions taxi/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.0.2 on 2022-03-24 06:38

from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

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

operations = [
migrations.CreateModel(
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)),
('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',
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Manufacturer',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('country', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
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')),
],
),
]
22 changes: 22 additions & 0 deletions taxi/migrations/0002_alter_manufacturer_options_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.0.2 on 2022-04-12 06:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('taxi', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='manufacturer',
options={'ordering': ['name']},
),
migrations.AlterField(
model_name='driver',
name='license_number',
field=models.CharField(max_length=255, unique=True),
),
]
Empty file added taxi/migrations/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions taxi/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.db import models
from django.contrib.auth.models import AbstractUser


class Manufacturer(models.Model):
name = models.CharField(max_length=255)
country = models.CharField(max_length=255)

class Meta:
ordering = ["name"]

def __str__(self):
return f"{self.name} {self.country}"


class Driver(AbstractUser):
license_number = models.CharField(max_length=255, unique=True)

class Meta:
verbose_name = "driver"
verbose_name_plural = "drivers"

def __str__(self):
return f"{self.username} ({self.first_name} {self.last_name})"


class Car(models.Model):
model = models.CharField(max_length=255)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
drivers = models.ManyToManyField(Driver, related_name="cars")

def __str__(self):
return self.model
3 changes: 3 additions & 0 deletions taxi/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
14 changes: 14 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.urls import path

from .views import index, CarListView, CarDetailView, DriverListView, DriverDetailView, ManufacturerListView

urlpatterns = [
path("", index, name="index"),
path("manufacturers/", ManufacturerListView.as_view(), name="manufacturer_list"),
path("cars/", CarListView.as_view(), name="car_list"),
path("cars/<int:pk>/", CarDetailView.as_view(), name="car_detail"),
path("drivers/", DriverListView.as_view(), name="driver_list"),
path("drivers/<int:pk>/", DriverDetailView.as_view(), name="driver_detail")
]

app_name = "taxi"
48 changes: 48 additions & 0 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.shortcuts import render
from django.views import generic

from .models import Driver, Car, Manufacturer



def index(request):
"""View function for the home page of the site."""

num_drivers = Driver.objects.count()
num_cars = Car.objects.count()
num_manufacturers = Manufacturer.objects.count()

context = {
"num_drivers": num_drivers,
"num_cars": num_cars,
"num_manufacturers": num_manufacturers,
}

return render(request, "taxi/index.html", context=context)


class ManufacturerListView(generic.ListView):
model = Manufacturer
context_object_name = "manufacturer_list"
template_name = "taxi/manufacturer_list.html"
paginate_by = 2


class CarListView(generic.ListView):
model = Car
paginate_by = 2
queryset = Car.objects.all().select_related("manufacturer")


class CarDetailView(generic.DetailView):
model = Car


class DriverListView(generic.ListView):
model = Driver
paginate_by = 2


class DriverDetailView(generic.DetailView):
model = Driver
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")
Empty file added taxi_service/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions taxi_service/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for taxi_service project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taxi_service.settings")

application = get_asgi_application()
Loading

0 comments on commit 4585fdf

Please sign in to comment.