diff --git a/.gitignore b/.gitignore index e3fb58e..06e58eb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ /.vscode /.venv __pycache__ -/db.sqlite3 /project/settings_local.py diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..b99bb96 Binary files /dev/null and b/db.sqlite3 differ diff --git a/project/main/apps.py b/project/main/apps.py index 34f1451..bf4f436 100644 --- a/project/main/apps.py +++ b/project/main/apps.py @@ -3,4 +3,4 @@ from django.apps import AppConfig class MainConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" - name = "main" + name = "project.main" diff --git a/project/main/migrations/0001_initial.py b/project/main/migrations/0001_initial.py new file mode 100644 index 0000000..b081fbf --- /dev/null +++ b/project/main/migrations/0001_initial.py @@ -0,0 +1,34 @@ +# Generated by Django 5.0.4 on 2024-05-14 20:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="CatBreed", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=100)), + ("country", models.CharField(max_length=100)), + ("origin", models.CharField(max_length=100)), + ("coat", models.CharField(max_length=100)), + ("pattern", models.CharField(max_length=100)), + ], + options={ + "db_table": "cat_breeds", + }, + ), + ] diff --git a/project/main/cat_breeds.py b/project/main/migrations/0002_example_data.py similarity index 88% rename from project/main/cat_breeds.py rename to project/main/migrations/0002_example_data.py index ddedc53..bf56b87 100644 --- a/project/main/cat_breeds.py +++ b/project/main/migrations/0002_example_data.py @@ -1,8 +1,10 @@ from dataclasses import dataclass +from django.db import migrations + @dataclass -class CatBreed: +class Breed: name: str country: str origin: str @@ -11,672 +13,672 @@ class CatBreed: cat_breeds = [ - CatBreed( + Breed( name="Abyssinian", country="Ethiopia", origin="Natural/Standard", coat="Short", pattern="Ticked", ), - CatBreed( + Breed( name="Aegean", country="Greece", origin="Natural/Standard", coat="Semi-long", pattern="Bi- or tri-colored", ), - CatBreed( + Breed( name="American Curl", country="United States", origin="Mutation", coat="Short/Long", pattern="All", ), - CatBreed( + Breed( name="American Bobtail", country="United States", origin="Mutation", coat="Short/Long", pattern="All", ), - CatBreed( + Breed( name="American Shorthair", country="United States", origin="Natural", coat="Short", pattern="All but colorpoint", ), - CatBreed( + Breed( name="American Wirehair", country="United States", origin="Mutation", coat="Rex", pattern="All but colorpoint", ), - CatBreed( + Breed( name="Arabian Mau", country="Arabian Peninsula", origin="Natural", coat="Short", pattern="", ), - CatBreed( + Breed( name="Australian Mist", country="Australia", origin="Crossbreed", coat="Short", pattern="Spotted and Classic tabby", ), - CatBreed( + Breed( name="Asian", country="United Kingdom", origin="", coat="Short", pattern="Evenly solid", ), - CatBreed( + Breed( name="Asian Semi-longhair", country="United Kingdom", origin="Crossbreed", coat="Semi-long", pattern="Solid", ), - CatBreed( + Breed( name="Balinese", country="United States", origin="Crossbreed", coat="Long", pattern="Colorpoint", ), - CatBreed( + Breed( name="Bambino", country="United States", origin="Crossbreed", coat="Hairless/Furry down", pattern="", ), - CatBreed( + Breed( name="Bengal", country="United States", origin="Hybrid", coat="Short", pattern="Spotted/Marbled", ), - CatBreed( + Breed( name="Birman", country="France", origin="Natural", coat="Semi Long", pattern="Colorpoint", ), - CatBreed( + Breed( name="Bombay", country="United States", origin="Crossbred", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Brazilian Shorthair", country="Brazil", origin="Natural", coat="Short", pattern="All", ), - CatBreed( + Breed( name="British Semi-longhair", country="United Kingdom", origin="", coat="Medium", pattern="All", ), - CatBreed( + Breed( name="British Shorthair", country="United Kingdom", origin="Natural", coat="Short", pattern="All", ), - CatBreed( + Breed( name="British Longhair", country="United Kingdom", origin="", coat="Long", pattern="", ), - CatBreed( + Breed( name="Burmese", country="Burma and Thailand", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Burmilla", country="United Kingdom", origin="Crossbreed", coat="Short/Long", pattern="", ), - CatBreed( + Breed( name="California Spangled", country="United States", origin="Crossbreed", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="Chantilly-Tiffany", country="United States", origin="", coat="", pattern="", ), - CatBreed( + Breed( name="Chartreux", country="France", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Chausie", country="France", origin="Hybrid", coat="Short", pattern="Ticked", ), - CatBreed( + Breed( name="Cheetoh", country="United States", origin="Hybrid Crossbreed", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="Cornish Rex", country="United Kingdom", origin="Mutation", coat="Rex", pattern="All", ), - CatBreed( + Breed( name="Cymric or Manx Longhair", country="United Kingdom", origin="Natural/Mutation", coat="Long", pattern="", ), - CatBreed( + Breed( name="Cyprus", country="Cyprus", origin="Natural", coat="All", pattern="All", ), - CatBreed( + Breed( name="Devon Rex", country="United Kingdom", origin="Mutation", coat="Rex", pattern="All", ), - CatBreed( + Breed( name="Donskoy, or Don Sphynx", country="Russia", origin="", coat="Hairless", pattern="", ), - CatBreed( + Breed( name="Dragon Li", country="China", origin="Natural", coat="Short", pattern="Striped tabby", ), - CatBreed( + Breed( name="Dwarf cat, or Dwelf", country="", origin="Crossbreed", coat="", pattern="Hairless", ), - CatBreed( + Breed( name="Egyptian Mau", country="Egypt", origin="Natural", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="European Shorthair", country="Finland and Sweden", origin="Natural", coat="Short", pattern="", ), - CatBreed( + Breed( name="Exotic Shorthair", country="United States", origin="Crossbreed", coat="Short", pattern="All", ), - CatBreed( + Breed( name="Foldex[4]", country="Canada", origin="Crossbreed", coat="Short", pattern="All", ), - CatBreed( + Breed( name="German Rex", country="East Germany", origin="Mutation", coat="Rex", pattern="", ), - CatBreed( + Breed( name="Havana Brown", country="United Kingdom", origin="", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Highlander", country="United States", origin="Crossbreed", coat="Short/Long", pattern="All", ), - CatBreed( + Breed( name="Himalayan, or Colorpoint Persian", country="United States/United Kingdom", origin="Crossbreed", coat="Long", pattern="Colorpoint", ), - CatBreed( + Breed( name="Japanese Bobtail", country="Japan", origin="Natural", coat="Short/Long", pattern="All but colorpoint and ticked", ), - CatBreed( + Breed( name="Javanese", country="United States", origin="Crossbreed", coat="Long/Short", pattern="Colorpoint", ), - CatBreed( + Breed( name="Karelian Bobtail", country="Western Russia", origin="Natural", coat="", pattern="", ), - CatBreed( + Breed( name="Khao Manee", country="Thailand", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Korat", country="Thailand", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Korean Bobtail", country="Korea", origin="Natural", coat="Short/Long", pattern="Colorprint", ), - CatBreed( + Breed( name="Korn Ja", country="Thailand", origin="Natural", coat="Short/Hairless", pattern="Solid", ), - CatBreed( + Breed( name="Kurilian Bobtail, or Kuril Islands Bobtail", country="Eastern Russia,Japan", origin="Natural", coat="Short/Long", pattern="", ), - CatBreed( + Breed( name="LaPerm", country="United States", origin="Mutation", coat="Rex", pattern="All", ), - CatBreed( + Breed( name="Lykoi", country="United States", origin="Natural/Mutation", coat="Partly Hairless", pattern="Ticked", ), - CatBreed( + Breed( name="Maine Coon", country="United States", origin="Natural", coat="Long", pattern="All but colorpoint and ticked", ), - CatBreed( + Breed( name="Manx", country="United Kingdom", origin="Mutation", coat="Short/Long", pattern="All but colorpoint", ), - CatBreed( + Breed( name="Mekong Bobtail", country="Russia", origin="Natural/Mutation", coat="Short", pattern="Colorpoint", ), - CatBreed( + Breed( name="Minskin", country="United States", origin="Crossbreed", coat="Short/Hairless", pattern="All", ), - CatBreed( + Breed( name="Munchkin", country="United States", origin="Mutation", coat="", pattern="", ), - CatBreed( + Breed( name="Nebelung", country="United States", origin="", coat="Semi-long", pattern="Solid", ), - CatBreed( + Breed( name="Napoleon", country="", origin="", coat="Long/short", pattern="Varied", ), - CatBreed( + Breed( name="Norwegian Forest cat", country="Norway", origin="Natural", coat="Long", pattern="All but colorpoint", ), - CatBreed( + Breed( name="Ocicat", country="United States", origin="Crossbreed", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="Ojos Azules", country="United States", origin="", coat="", pattern="", ), - CatBreed( + Breed( name="Oregon Rex", country="United States", origin="Mutation", coat="Rex", pattern="", ), - CatBreed( + Breed( name="Oriental Bicolor", country="", origin="", coat="", pattern="Bicolor", ), - CatBreed( + Breed( name="Oriental Shorthair", country="", origin="", coat="Short", pattern="All but colorpoint", ), - CatBreed( + Breed( name="Oriental Longhair", country="", origin="", coat="Semi-long", pattern="", ), - CatBreed( + Breed( name="PerFoldæ(Experimental Breed - WCF),", country="Europe", origin="Crossbreed", coat="Long", pattern="All", ), - CatBreed( + Breed( name="Persian (Modern Persian Cat),", country="Iran (Persia),", origin="Crossbreed", coat="Long", pattern="All", ), - CatBreed( + Breed( name="Persian (Traditional Persian Cat),", country="Greater Iran", origin="Natural", coat="Long", pattern="All", ), - CatBreed( + Breed( name="Peterbald", country="Russia", origin="Crossbreed", coat="Hairless", pattern="All", ), - CatBreed( + Breed( name="Pixie-bob", country="United States", origin="Natural", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="Raas", country="Indonesia", origin="Natural", coat="Short", pattern="", ), - CatBreed( + Breed( name="Ragamuffin", country="United States", origin="Crossbreed", coat="Long", pattern="All", ), - CatBreed( + Breed( name="Ragdoll", country="United States", origin="Crossbreed", coat="Long", pattern="Colorpoint/Mitted/Bicolor", ), - CatBreed( + Breed( name="Russian Blue", country="Russia", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Russian White, Black and Tabby", country="Australia", origin="Crossbreed", coat="Short", pattern="", ), - CatBreed( + Breed( name="Sam Sawet", country="Thailand", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Savannah", country="United States", origin="Hybrid", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="Scottish Fold", country="United Kingdom", origin="Natural/Mutation", coat="Short/Long", pattern="All", ), - CatBreed( + Breed( name="Selkirk Rex", country="United States", origin="Mutation/Cross", coat="Rex (Short/Long),", pattern="All", ), - CatBreed( + Breed( name="Serengeti", country="United States", origin="Hybrid Crossbreed", coat="Short", pattern="Spotted", ), - CatBreed( + Breed( name="Serrade petit", country="France", origin="Natural", coat="Short", pattern="", ), - CatBreed( + Breed( name="Siamese", country="Thailand", origin="Natural", coat="Short", pattern="Colorpoint", ), - CatBreed( + Breed( name="Siberian", country="Russia", origin="Natural", coat="Semi-long", pattern="All", ), - CatBreed( + Breed( name="Singapura", country="Singapore", origin="Natural", coat="Short", pattern="Ticked", ), - CatBreed( + Breed( name="Snowshoe", country="United States", origin="Crossbreed", coat="Short", pattern="Colorpoint", ), - CatBreed( + Breed( name="Sokoke", country="Kenya", origin="Natural", coat="Short", pattern="Classic tabby with ticking", ), - CatBreed( + Breed( name="Somali", country="Somalia", origin="Mutation", coat="Long", pattern="Ticked", ), - CatBreed( + Breed( name="Sphynx", country="Canada", origin="Mutation", coat="Hairless", pattern="All", ), - CatBreed( + Breed( name="Suphalak", country="Thailand", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Thai", country="Thailand", origin="Natural", coat="Short", pattern="Colorpoint", ), - CatBreed( + Breed( name="Thai Lilac", country="Thailand", origin="Natural", coat="Short", pattern="Solid", ), - CatBreed( + Breed( name="Tonkinese", country="Canada", origin="Crossbreed", coat="Short", pattern="Colorpoint/Mink/Solid", ), - CatBreed( + Breed( name="Toyger", country="United States", origin="Crossbreed", coat="Short", pattern="Mackerel", ), - CatBreed( + Breed( name="Turkish Angora", country="Turkey", origin="Natural", coat="Semi-long", pattern="All but colorpoint", ), - CatBreed( + Breed( name="Ukrainian Levkoy", country="Ukraine", origin="", coat="Hairless", pattern="", ), - CatBreed( + Breed( name="York Chocolate", country="United States", origin="Natural", @@ -684,3 +686,32 @@ cat_breeds = [ pattern="Solid", ), ] + + +def create_cat_breeds(apps, schema_editor): + CatBreed = apps.get_model("main", "CatBreed") + + bulk = [] + + for cat_breed in cat_breeds: + bulk.append( + CatBreed( + name=cat_breed.name, + country=cat_breed.country, + origin=cat_breed.origin, + coat=cat_breed.coat, + pattern=cat_breed.pattern, + ) + ) + + CatBreed.objects.bulk_create(bulk) + + +class Migration(migrations.Migration): + dependencies = [ + ("main", "0001_initial"), + ] + + operations = [ + migrations.RunPython(create_cat_breeds), + ] diff --git a/project/main/models.py b/project/main/models.py deleted file mode 100644 index 71a8362..0000000 --- a/project/main/models.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.db import models - -# Create your models here. diff --git a/project/main/models/__init__.py b/project/main/models/__init__.py new file mode 100644 index 0000000..191fd62 --- /dev/null +++ b/project/main/models/__init__.py @@ -0,0 +1 @@ +from .cat_breed import CatBreed diff --git a/project/main/models/cat_breed.py b/project/main/models/cat_breed.py new file mode 100644 index 0000000..58af0c2 --- /dev/null +++ b/project/main/models/cat_breed.py @@ -0,0 +1,12 @@ +from django.db import models + + +class CatBreed(models.Model): + name = models.CharField(max_length=100) + country = models.CharField(max_length=100) + origin = models.CharField(max_length=100) + coat = models.CharField(max_length=100) + pattern = models.CharField(max_length=100) + + class Meta: + db_table = "cat_breeds" diff --git a/project/main/views/filter_list.py b/project/main/views/filter_list.py index 765e17e..afdd519 100644 --- a/project/main/views/filter_list.py +++ b/project/main/views/filter_list.py @@ -1,27 +1,31 @@ from typing import Any, Optional -from project.main.cat_breeds import CatBreed, cat_breeds +from django.db.models import Count, Q + +from project.main.models import CatBreed from project.main.views.demo_view_base import DemoViewBase def get_countries() -> list[str]: - return sorted(set([c.country for c in cat_breeds if len(c.country)])) + ann = ( + CatBreed.objects.values("country") + .annotate(Count("country")) + .order_by("country") + ) + return [a["country"] for a in ann] def filter_cat_breeds( breed_filter: Optional[str] = None, country_filter: Optional[str] = None ) -> list[CatBreed]: - if not breed_filter and not country_filter: - return cat_breeds + q = Q() - result = [] - for breed in cat_breeds: - # if (not breed_filter or breed_filter.lower() in breed.name.lower()) or ( - # not country_filter or country_filter == breed.country - # ): - if not breed_filter or breed_filter.lower() in breed.name.lower(): - result.append(breed) - return result + if breed_filter: + q &= Q(name__icontains=breed_filter) + if country_filter: + q &= Q(country=country_filter) + + return CatBreed.objects.filter(q).order_by("name") class FilterListView(DemoViewBase): diff --git a/project/settings.py b/project/settings.py index c398419..efe6b9e 100644 --- a/project/settings.py +++ b/project/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "project.main", ] MIDDLEWARE = [