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

Marc's Cars_and_Brands WIP solution #4

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
venv/
.venv/
.vscode/
__pycache__/
*.py[cod]
18 changes: 2 additions & 16 deletions car_app/car_app/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
"""car_app URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('brands/', include('cars_and_brands.urls'))
]
3 changes: 3 additions & 0 deletions car_app/cars_and_brands/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.contrib import admin
from .models import Brand, Car

# Register your models here.
admin.site.register(Brand)
admin.site.register(Car)
12 changes: 12 additions & 0 deletions car_app/cars_and_brands/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django import forms
from .models import Brand, Car

class BrandForm(forms.ModelForm):
class Meta:
model = Brand
fields = ('name',)

class CarForm(forms.ModelForm):
class Meta:
model = Car
fields = ('name', 'year')
31 changes: 31 additions & 0 deletions car_app/cars_and_brands/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 2.1.3 on 2021-07-14 01:53

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Brand',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
],
),
migrations.CreateModel(
name='Car',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('year', models.CharField(max_length=200)),
('brand', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cars', to='cars_and_brands.Brand')),
],
),
]
29 changes: 29 additions & 0 deletions car_app/cars_and_brands/migrations/0002_auto_20210714_0311.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 2.1.3 on 2021-07-14 03:11

import cars_and_brands.models
from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='brand',
name='name',
field=models.CharField(max_length=200, validators=[cars_and_brands.models.validate_brand_name_presence]),
),
migrations.AlterField(
model_name='car',
name='name',
field=models.CharField(max_length=200, validators=[cars_and_brands.models.validate_car_name_presence]),
),
migrations.AlterField(
model_name='car',
name='year',
field=models.CharField(max_length=200, validators=[cars_and_brands.models.validate_car_year_presence]),
),
]
35 changes: 35 additions & 0 deletions car_app/cars_and_brands/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as text

# Create your models here.
def validate_brand_name_presence(name):
if len(name) == 0:
raise ValidationError(text(
'This field cannot be blank.'
))

def validate_car_name_presence(name):
if len(name) == 0:
raise ValidationError(text(
'This field cannot be blank.'
))

def validate_car_year_presence(year):
if len(year) == 0:
raise ValidationError(text(
'This field cannot be blank.'
))


class Brand(models.Model):
name = models.CharField(max_length=200, validators=[validate_brand_name_presence])

def __str__(self) -> str:
return f"ID: {self.id} Name: {self.name}"

class Car(models.Model):
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, related_name='cars')

name = models.CharField(max_length=200, validators=[validate_car_name_presence])
year = models.CharField(max_length=200, validators=[validate_car_year_presence])

def __str__(self) -> str:
return f"ID: {self.id} Name: {self.name} Year: {self.year}"
3 changes: 3 additions & 0 deletions car_app/cars_and_brands/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: lightblue;
}
28 changes: 28 additions & 0 deletions car_app/cars_and_brands/templates/brand/brand_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="../../static/styles.css" >
<title>Document</title>
</head>
<body>
<div class="d-flex justify-content-center align-items-center flex-column">
<h2>{{ brand.name }}</h2>
<div>
<a href="{% url 'cars_list' brand_id=brand.id %}">Cars made by this Brand</a>
</div>
<br>
<a href="{% url 'edit_brand' brand_id=brand.id %}"> Edit brand</a>
<form name="delete_brand" method="POST" action="{% url 'delete_brand' brand_id=brand.id %}">
{% csrf_token %}
<input type="submit" value="Delete brand" />
</form>

</div>


</body>
</html>
20 changes: 20 additions & 0 deletions car_app/cars_and_brands/templates/brand/brand_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="../../static/styles.css" >
<title>Document</title>
</head>
<body>
<div class="d-flex justify-content-center align-items-center flex-column">
<h2>{{ type_of_request }} Brand</h2>
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
</div>
</body>
</html>
29 changes: 29 additions & 0 deletions car_app/cars_and_brands/templates/brand/brands_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="../../static/styles.css" >
<title>Document</title>
</head>
<body>
<div class="container">
<div class="d-flex justify-content-center">
<h1>Brands</h1>
</div>
<div class="d-flex align-items-center flex-column">
<ul class="list-group w-20 ">
{% for brand in all_brands %}
<li class="list-group-item list-group-item-light text-center"><a href="{% url 'brand_detail' brand_id=brand.id %}"">{{brand.name}}</a></li>
{% endfor %}
</ul>
<a class="text-decoration-none" href="{% url 'new_brand' %}"> Add a new brand </a>
<div>
</div>


</body>
</html>
26 changes: 26 additions & 0 deletions car_app/cars_and_brands/templates/cars/car_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="../../../static/styles.css" >
<title>Document</title>
</head>
<body>
<div class="d-flex justify-content-center align-items-center flex-column">
<h2>{{car.name}}</h2>
<p>Brand: {{brand.name}}</p>
<p>Year: {{car.year}}</p>

<a href="{% url 'edit_car' brand_id=brand.id car_id=car.id%}"> Edit Car</a>
<br>
<br>
<form name="delete_brand" method="POST" action="{% url 'delete_car' brand_id=brand.id car_id=car.id %}">
{% csrf_token %}
<input type="submit" value="Delete car" />
</form>
</div>
</body>
</html>
19 changes: 19 additions & 0 deletions car_app/cars_and_brands/templates/cars/car_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="../../../static/styles.css" >
<title>Document</title>
</head>
<body>
</html>
<h2>{{ type_of_request }} Car</h2>
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>

</body>
25 changes: 25 additions & 0 deletions car_app/cars_and_brands/templates/cars/cars_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="../../static/styles.css" >
<title>Document</title>
</head>
<body>
<div class="d-flex justify-content-center align-items-center flex-column">
<h1> All Cars made by {{ brand.name }} </h1>
<ul class="list-group w-20">
{% for car in cars %}
<div>
<li class="list-group-item"><a href="{% url 'car_detail' brand_id=brand.id car_id=car.id %}">{{ car.name }}</a></li>
</div>
{% endfor %}
</ul>

<a href="{% url 'new_car' brand_id=brand.id %}">Add a new car</a>
</div>
</body>
</html>
37 changes: 37 additions & 0 deletions car_app/cars_and_brands/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
from django.core import exceptions
from django.test import TestCase
from .models import *


# Create your tests here.
class AssociationTestCase(TestCase):
def setUp(self):
self.brand = Brand.objects.create()
self.car = Car.objects.create(brand=self.brand)

def test01_brand_of_car(self):
"returns the brand of the car"
self.assertEqual(self.car.brand, self.brand)

def test02_brand_cars(self):
self.assertEqual(list(self.brand.cars.all()), [self.car])

class ValidationsTestCase(TestCase):

brand = Brand()
car = Car()

def test01_validate_brand_name(self):
try:
self.brand.full_clean()
except ValidationError as e:
self.assertTrue('This field cannot be blank.' in e.message_dict['name'])

def test02_validate_car_name(self):
try:
self.car.full_clean()
except ValidationError as e:
self.assertTrue('This field cannot be blank.' in e.message_dict['name'])

def test03_validate_car_year(self):
try:
self.car.full_clean()
except ValidationError as e:
self.assertTrue('This field cannot be blank.' in e.message_dict['year'])
18 changes: 18 additions & 0 deletions car_app/cars_and_brands/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.urls import path
from . import views



urlpatterns = [
path('', views.brands_list, name='brands_list'),
path('new', views.new_brand, name='new_brand'),
path('<int:brand_id>', views.brand_detail, name='brand_detail'),
path('<int:brand_id>/edit', views.edit_brand, name='edit_brand'),
path('<int:brand_id>/delete', views.delete_brand, name='delete_brand'),

path('<int:brand_id>/cars', views.cars_list, name='cars_list'),
path('<int:brand_id>/cars/new', views.new_car, name='new_car'),
path('<int:brand_id>/cars/<int:car_id>', views.car_detail, name='car_detail'),
path('<int:brand_id>/cars/<int:car_id>/edit', views.edit_car, name='edit_car'),
path('<int:brand_id>/cars/<int:car_id>/delete', views.delete_car, name='delete_car'),
]
Loading