Skip to content

Commit

Permalink
Add basic pdf export view, add Breed model
Browse files Browse the repository at this point in the history
  • Loading branch information
leethobbit committed Nov 26, 2024
1 parent 918e716 commit 89e5fae
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"ms-python.vscode-pylance",
"charliermarsh.ruff",
"batisteo.vscode-django",
"ecmel.vscode-html-css"
"ecmel.vscode-html-css",
"Codeium.codeium"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"django_bootstrap5",
"django_filters",
"widget_tweaks",
"django_xhtml2pdf",
]

LOCAL_APPS = [
Expand Down
2 changes: 2 additions & 0 deletions dragonroost/animals/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.contrib import admin

from .models import Animal
from .models import Breed
from .models import MedicalRecord
from .models import Species

# Register your models here.
admin.site.register(Species)
admin.site.register(MedicalRecord)
admin.site.register(Breed)


@admin.register(Animal)
Expand Down
1 change: 1 addition & 0 deletions dragonroost/animals/management/commands/add_animals.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def create_animals(self, count):
description=fake.text(max_nb_chars=50),
species=secrets.choice(species),
location=secrets.choice(locations),
breed=None,
color=fake.color(),
donation_fee=random.uniform(5.00, 350.00), # noqa: S311
age=random.uniform(1, 50), # noqa: S311
Expand Down
29 changes: 29 additions & 0 deletions dragonroost/animals/migrations/0002_species_has_breeds_breed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.9 on 2024-11-26 19:57

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


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='species',
name='has_breeds',
field=models.BooleanField(default=False),
),
migrations.CreateModel(
name='Breed',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=80, unique=True)),
('origin', models.CharField(blank=True, max_length=80)),
('temperament', models.CharField(choices=[('ACTIVE', 'Active'), ('ENERGETIC', 'Energetic'), ('GENTLE', 'Gentle'), ('INDEPENDENT', 'Independent')], default='ACTIVE', max_length=80)),
('species', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='breeds', to='animals.species')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.9 on 2024-11-26 21:42

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


class Migration(migrations.Migration):

dependencies = [
('animals', '0002_species_has_breeds_breed'),
]

operations = [
migrations.AddField(
model_name='animal',
name='breed',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='animals', to='animals.breed'),
),
migrations.AlterField(
model_name='species',
name='class_name',
field=models.CharField(choices=[('MAMMAL', 'Mammal'), ('REPTILE', 'Reptile'), ('BIRD', 'Bird'), ('AMPHIBIAN', 'Amphibian'), ('INSECT', 'Insect'), ('FISH', 'Fish')], default='REPTILE', max_length=20),
),
]
19 changes: 19 additions & 0 deletions dragonroost/animals/migrations/0004_alter_animal_breed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.9 on 2024-11-26 21:44

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


class Migration(migrations.Migration):

dependencies = [
('animals', '0003_animal_breed_alter_species_class_name'),
]

operations = [
migrations.AlterField(
model_name='animal',
name='breed',
field=models.ForeignKey(blank=True, default='', on_delete=django.db.models.deletion.SET_DEFAULT, related_name='animals', to='animals.breed'),
),
]
19 changes: 19 additions & 0 deletions dragonroost/animals/migrations/0005_alter_animal_breed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.9 on 2024-11-26 22:07

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


class Migration(migrations.Migration):

dependencies = [
('animals', '0004_alter_animal_breed'),
]

operations = [
migrations.AlterField(
model_name='animal',
name='breed',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='animals', to='animals.breed'),
),
]
38 changes: 38 additions & 0 deletions dragonroost/animals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Species(models.Model):
("BIRD", "Bird"),
("AMPHIBIAN", "Amphibian"),
("INSECT", "Insect"),
("FISH", "Fish"),
]
DIET_CHOICES = [
("VEGGIE", "Veggie"),
Expand All @@ -37,6 +38,7 @@ class Species(models.Model):
description = models.TextField()
diet = models.CharField(max_length=80, choices=DIET_CHOICES, default="UNKNOWN")
is_ohio_native = models.BooleanField(default=False)
has_breeds = models.BooleanField(default=False)

def __str__(self):
return self.name
Expand All @@ -45,6 +47,35 @@ def get_absolute_url(self):
return reverse("species_detail", args=[str(self.id)])


class Breed(models.Model):
TEMPERAMENT_CHOICES = [
("ACTIVE", "Active"),
("ENERGETIC", "Energetic"),
("GENTLE", "Gentle"),
("INDEPENDENT", "Independent"),
]

name = models.CharField(max_length=80, unique=True)
origin = models.CharField(max_length=80, blank=True)
species = models.ForeignKey(
Species,
related_name="breeds",
on_delete=models.CASCADE,
default=1,
)
temperament = models.CharField(
max_length=80,
choices=TEMPERAMENT_CHOICES,
default="ACTIVE",
)

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("breed_detail", args=[str(self.id)])


class Animal(models.Model):
# TODO Decide if Creator of an animal should be tracked via ForeignKey

Expand Down Expand Up @@ -131,6 +162,13 @@ class Animal(models.Model):
choices=STATUS_CHOICES,
default="AVAILABLE",
)
breed = models.ForeignKey(
Breed,
on_delete=models.SET_DEFAULT,
related_name="animals",
null=True,
default=None,
)

class Meta:
db_table_comment = "Holds information about animals"
Expand Down
6 changes: 6 additions & 0 deletions dragonroost/animals/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dragonroost.animals.views import AnimalCreateView
from dragonroost.animals.views import AnimalDeleteView
from dragonroost.animals.views import AnimalDetailPDFView
from dragonroost.animals.views import AnimalDetailView
from dragonroost.animals.views import AnimalOutcomeView
from dragonroost.animals.views import AnimalTableSearchView
Expand All @@ -22,6 +23,11 @@
urlpatterns = [
path("new/", AnimalCreateView.as_view(), name="animal-create"),
path("<int:pk>/detail/", AnimalDetailView.as_view(), name="animal-detail"),
path(
"<int:pk>/detail/pdf/",
AnimalDetailPDFView.as_view(),
name="animal-detail-pdf",
),
path("<int:pk>/edit/", AnimalUpdateView.as_view(), name="animal-update"),
path("<int:pk>/outcome/", AnimalOutcomeView.as_view(), name="animal-outcome"),
path("<int:pk>/delete/", AnimalDeleteView.as_view(), name="animal-delete"),
Expand Down
17 changes: 17 additions & 0 deletions dragonroost/animals/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.views.generic import UpdateView
from django_filters.views import FilterView
from django_tables2 import SingleTableMixin
from django_xhtml2pdf.views import PdfMixin

from dragonroost.mixins import PageTitleViewMixin

Expand Down Expand Up @@ -93,6 +94,22 @@ def get_context_data(self, **kwargs):
return data


class AnimalDetailPDFView(PdfMixin, DetailView):
model = Animal
title = "Animal Details"
context_object_name = "animal"
template_name = "animals/partials/animal_detail.html"

def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
medical_records = MedicalRecord.objects.filter(
animal=self.get_object(),
).order_by("-created")
data["medical_records"] = medical_records
data["medical_record_form"] = MedicalRecordForm(instance=self.get_object())
return data


class AnimalCreateView(LoginRequiredMixin, PageTitleViewMixin, CreateView):
model = Animal
form_class = AnimalCreateForm
Expand Down
12 changes: 7 additions & 5 deletions dragonroost/templates/animals/animal_detail_full.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ <h5 class="my-3">
hx-target="#dialog"
hx-swap="innerHTML"
class="btn btn-primary me-4">Edit</a>
<a href="{% url 'animals:animal-detail-pdf' animal.id %}"
class="btn btn-outline-info me-4" target='_blank'>Export to PDF</a>
<a href="{% url 'animals:animal-delete' animal.id %}"
hx-boost="true"
hx-push-url="false"
hx-target="#dialog"
hx-swap="innerHTML"
class="btn btn-outline-danger">Delete</a>
hx-boost="true"
hx-push-url="false"
hx-target="#dialog"
hx-swap="innerHTML"
class="btn btn-outline-danger">Delete</a>
</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions dragonroost/templates/animals/partials/animal_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ <h5 class="my-3">
hx-target="#dialog"
hx-swap="innerHTML"
class="btn btn-primary me-4">Edit</a>
<a href="{% url 'animals:animal-detail-pdf' animal.id %}"
class="btn btn-outline-info me-4" target='_blank'>Export to PDF</a>
<a href="{% url 'animals:animal-delete' animal.id %}"
hx-boost="true"
hx-push-url="false"
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ django-filter==24.3 # https://github.com/carltongibson/django-filter
django-tables2==2.7.0 # https://github.com/jieter/django-tables2
django-bootstrap5==24.3 # TODO Add GH link
django-measurement==3.2.4 # TODO Add GH link
django-xhtml2pdf==0.0.4 # https://github.com/xhtml2pdf/django-xhtml2pdf

0 comments on commit 89e5fae

Please sign in to comment.