Skip to content

Commit

Permalink
Merge pull request #1 from pydanny/add-django-test-project
Browse files Browse the repository at this point in the history
Add django test project
  • Loading branch information
anna-zhydko authored Sep 12, 2023
2 parents 0b8d5c1 + 309afad commit 13eba98
Show file tree
Hide file tree
Showing 26 changed files with 612 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,4 @@ cython_debug/
.vscode/
.ruff_cache/
changelog.json
.idea
4 changes: 2 additions & 2 deletions src/dj_notebook/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = 'Daniel Roy Greenfeld'
__email__ = '[email protected]'
__author__ = "Daniel Roy Greenfeld"
__email__ = "[email protected]"


def main():
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions tests/django_test_project/book_outlet/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.contrib import admin

from .models import Book, Author, Address, Country

# Register your models here.


class BookAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
list_filter = (
"title",
"rating",
)
list_display = (
"title",
"author",
)


admin.site.register(Book, BookAdmin)
admin.site.register(Author)
admin.site.register(Address)
admin.site.register(Country)
6 changes: 6 additions & 0 deletions tests/django_test_project/book_outlet/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BookOutletConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "book_outlet"
29 changes: 29 additions & 0 deletions tests/django_test_project/book_outlet/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.2 on 2023-06-14 11:14

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Book",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=50)),
("rating", models.IntegerField()),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.2.2 on 2023-06-14 16:20

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("book_outlet", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="book",
name="author",
field=models.CharField(max_length=100, null=True),
),
migrations.AddField(
model_name="book",
name="is_bestselling",
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name="book",
name="rating",
field=models.IntegerField(
validators=[
django.core.validators.MinValueValidator(1),
django.core.validators.MaxValueValidator(5),
]
),
),
]
18 changes: 18 additions & 0 deletions tests/django_test_project/book_outlet/migrations/0003_book_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-28 10:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("book_outlet", "0002_book_author_book_is_bestselling_alter_book_rating"),
]

operations = [
migrations.AddField(
model_name="book",
name="slug",
field=models.SlugField(default=""),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.2.2 on 2023-07-05 11:09

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


class Migration(migrations.Migration):

dependencies = [
("book_outlet", "0003_book_slug"),
]

operations = [
migrations.CreateModel(
name="Author",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("first_name", models.CharField(max_length=100)),
("last_name", models.CharField(max_length=100)),
],
),
migrations.AlterField(
model_name="book",
name="slug",
field=models.SlugField(blank=True, default=""),
),
migrations.AlterField(
model_name="book",
name="author",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="book_outlet.author",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.2 on 2023-07-05 11:33

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


class Migration(migrations.Migration):

dependencies = [
("book_outlet", "0004_author_alter_book_slug_alter_book_author"),
]

operations = [
migrations.AlterField(
model_name="book",
name="author",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="books",
to="book_outlet.author",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.2 on 2023-07-12 11:12

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


class Migration(migrations.Migration):

dependencies = [
("book_outlet", "0005_alter_book_author"),
]

operations = [
migrations.CreateModel(
name="Address",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("street", models.CharField(max_length=80)),
("postal_code", models.CharField(max_length=5)),
("city", models.CharField(max_length=50)),
],
),
migrations.AddField(
model_name="author",
name="address",
field=models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="book_outlet.address",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.2 on 2023-07-14 09:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("book_outlet", "0006_address_author_address"),
]

operations = [
migrations.CreateModel(
name="Country",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=80)),
("code", models.CharField(max_length=2)),
],
),
migrations.AlterModelOptions(
name="address",
options={"verbose_name_plural": "Address Entries"},
),
migrations.AddField(
model_name="book",
name="published_countries",
field=models.ManyToManyField(to="book_outlet.country"),
),
]
Empty file.
62 changes: 62 additions & 0 deletions tests/django_test_project/book_outlet/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
from django.utils.text import slugify


class Country(models.Model):
name = models.CharField(max_length=80)
code = models.CharField(max_length=2)

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

class Meta:
verbose_name_plural = "Countries"


class Address(models.Model):
street = models.CharField(max_length=80)
postal_code = models.CharField(max_length=5)
city = models.CharField(max_length=50)

def __str__(self):
return f"{self.street}, {self.postal_code}, {self.city}"

class Meta:
verbose_name_plural = "Address Entries"


class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
address = models.OneToOneField(Address, on_delete=models.CASCADE, null=True)

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

def __str__(self):
return self.full_name()


class Book(models.Model):
title = models.CharField(max_length=50)
rating = models.IntegerField(
validators=[MinValueValidator(1), MaxValueValidator(5)]
)
author = models.ForeignKey(
Author, on_delete=models.CASCADE, null=True, related_name="books"
)
is_bestselling = models.BooleanField(default=False)
slug = models.SlugField(default="", blank=True, null=False, db_index=True)
published_countries = models.ManyToManyField(Country)

def get_absolute_url(self):
return reverse("book-detail", args=[self.slug])

def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super().save(*args, **kwargs)

def __str__(self):
return f"{self.title} ({self.rating})"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "book_outlet/base.html" %}

{% block title %}
{{ title }}
{% endblock %}

{% block content %}
<h1>{{ title }}</h1>
<h2>{{ author }}</h2>
<p> The book has a rating of {{ rating }}
{% if is_bestseller %}
and is a bestseller.
{% else %}
but isn't a bestseller.
{% endif %}
</p>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "book_outlet/base.html" %}

{% block title %} All books {% endblock %}

{% block content %}
<ul>
{% for book in books %}
<li><a href="{{ book.get_absolute_url }}">{{book.title}} (Rating: {{ book.rating }})</a></li>
{% endfor %}
</ul>
<hr>
<p>Total Nuber of Books: {{ total_number_of_books }}</p>
<p>Average Rating: {{ average_rating.rating__avg }}</p>

{% endblock %}

1 change: 1 addition & 0 deletions tests/django_test_project/book_outlet/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
Loading

0 comments on commit 13eba98

Please sign in to comment.