Skip to content

Commit

Permalink
Add edit/delete pages, enhance tables
Browse files Browse the repository at this point in the history
  • Loading branch information
leethobbit committed Sep 27, 2024
1 parent 1937d8e commit 148b3a0
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 32 deletions.
1 change: 0 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
"allauth.account.middleware.AccountMiddleware",
"iommi.sql_trace.Middleware",
"iommi.profiling.Middleware",
"iommi.middleware",
]

# STATIC
Expand Down
5 changes: 4 additions & 1 deletion config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#prerequisites
INSTALLED_APPS += ["debug_toolbar"]
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#middleware
MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"]
MIDDLEWARE += [
"debug_toolbar.middleware.DebugToolbarMiddleware",
"iommi.middleware",
]
# https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config
DEBUG_TOOLBAR_CONFIG = {
"DISABLE_PANELS": [
Expand Down
5 changes: 5 additions & 0 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .base import * # noqa: F403
from .base import DATABASES
from .base import INSTALLED_APPS
from .base import MIDDLEWARE
from .base import env

# GENERAL
Expand Down Expand Up @@ -127,6 +128,10 @@
# ------------------------------------------------------------------------------
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
INSTALLED_APPS += ["anymail"]
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#middleware
MIDDLEWARE += [
"iommi.middleware",
]
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
# https://anymail.readthedocs.io/en/stable/esps/mailgun/
Expand Down
20 changes: 16 additions & 4 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.views import defaults as default_views
from django.views.generic import TemplateView

from iommi import Form, Page, Table, html
from iommi import Action, Column, Form, Page, Table, html

from .menu import create_main_navbar
from oar.animals.models import Animal, Species
Expand All @@ -21,10 +21,22 @@ class IndexPage(Page):
attrs__style__width="50%",
)

animals = Table(auto__model=Animal, page_size=5)
species = Table(auto__model=Species, title="Species", page_size=5)
animals = Table(
auto__model=Animal,
page_size=5,
columns__name__cell__url=lambda row, **_: row.get_absolute_url(),
)
species = Table(
auto__model=Species,
title="Species",
page_size=5,
actions__create_species=Action(
attrs__href="/animals/species/create/",
),
columns__edit=Column.edit(),
columns__delete=Column.delete(),
)
location = Table(auto__model=Location, page_size=5)
# create_animal = Form(auto__model=Animal)


urlpatterns = [
Expand Down
6 changes: 6 additions & 0 deletions oar/animals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Species(models.Model):
def __str__(self):
return self.name

def get_absolute_url(self):
return f"/animals/species/{self.id}/"


class Animal(models.Model):
# TODO Decide if Creator of an animal should be tracked via ForeignKey
Expand Down Expand Up @@ -126,6 +129,9 @@ class Meta:
def __str__(self):
return self.name

def get_absolute_url(self):
return f"/animals/{self.id}/"

@property
def animal_tag(self):
return f"A-{self.pk:05d}"
Expand Down
91 changes: 68 additions & 23 deletions oar/animals/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.shortcuts import get_object_or_404
from django.urls import path
from iommi import Action
from iommi import Column
from iommi import EditTable
from iommi import Form
from iommi import Page
from iommi import Table
Expand All @@ -8,58 +11,100 @@
from oar.animals.models import Animal
from oar.animals.models import MedicalRecord
from oar.animals.models import Species
from oar.people.models import Person

app_name = "animals"

# Menu


# Tables
animal_table = EditTable(
auto__model=Animal,
title="Animals",
actions__create_animal=Action(
attrs__href="/animals/create/",
attrs__class={"btn": True, "btn-info": True},
),
columns__edit=Column.edit(),
columns__delete=Column.delete(),
columns__intake_date__include=False,
columns__outcome_date__include=False,
columns__outcome_type__include=False,
columns__updated_at__include=False,
columns__animal_photo__include=False,
attrs__class={"table-hover": True, "table-bordered": True},
).as_view()

species_table = EditTable(
auto__model=Species,
title="Species",
actions__create_species=Action(
attrs__href="/animals/species/create/",
),
columns__edit=Column.edit(),
columns__delete=Column.delete(),
columns__is_ohio_native__display_name="Ohio Native?",
# Turn on edit feature for columns
columns__is_ohio_native__field__include=True,
).as_view()


# Pages

# Forms


def edit_animal(request, animal_pk):
animal = get_object_or_404(Animal, pk=animal_pk)
return Form.edit(auto__instance=animal)


def delete_animal(request, animal_pk):
animal = get_object_or_404(Animal, pk=animal_pk)
return Form.delete(auto__instance=animal)


def edit_species(request, species_pk):
species = get_object_or_404(Species, pk=species_pk)
return Form.edit(auto__instance=species)

def animal_page(request, animal):
animal = get_object_or_404(Animal, name=animal)

def delete_species(request, species_pk):
species = get_object_or_404(Species, pk=species_pk)
return Form.delete(auto__instance=species)


def animal_page(request, animal_pk):
animal = get_object_or_404(Animal, pk=animal_pk)

class AnimalPage(Page):
title = html.h1(animal.name)
medical_records = Table(auto__rows=MedicalRecord.objects.filter(animal=animal))
foster = Table(auto__rows=Person.objects.filter(animal=animal))

return AnimalPage()


# URLs

urlpatterns = [
path("", animal_table, name="animal-list"),
path("species/", species_table, name="species-list"),
path(
"",
Table(auto__model=Animal, title="Animals").as_view(),
name="animal-list",
),
path(
"species/",
Table(
auto__model=Species,
title="Species",
columns__name__cell__url=lambda value, **_: value.get_absolute_url(),
).as_view(),
name="species-list",
),
path(
"create_or_edit/<int:pk>",
Form.create_or_edit(
"species/create/",
Form.create(
auto__model=Species,
instance=lambda **_: None,
).as_view(),
),
path(
"create_animal/",
"create/",
Form.create(
auto__model=Animal,
auto__exclude=["intake_date", "outcome_date", "outcome_type", "updated_at"],
).as_view(),
),
path("pages/<animal>/", animal_page),
path("<animal_pk>/", animal_page),
path("<animal_pk>/edit/", edit_animal),
path("<animal_pk>/delete/", delete_animal),
path("species/<species_pk>/edit/", edit_species),
path("species/<species_pk>/delete/", delete_species),
]
24 changes: 21 additions & 3 deletions oar/animals/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
from iommi import Action
from iommi import Page
from iommi import Table
from iommi import html

from oar.animals.models import MedicalRecord

class AnimalTable(Table):
pass
# Tables


# Forms


# Pages
class AnimalPage(Page):
title = html.h1(lambda params, **_: params.animal.name)
actions__create_animal = Action(
attrs__href="/animals/create/",
)
medical_records = Table(
auto__model=MedicalRecord,
rows=lambda params, **_: MedicalRecord.objects.filter(animal=params.animal),
)


class SpeciesTable(Table):
class SpeciesPage(Page):
pass
3 changes: 3 additions & 0 deletions oar/business/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def __str__(self):
"""
return self.name

def get_absolute_url(self):
return f"/business/locations/{self}/"


# class Meeting
class Meeting(models.Model):
Expand Down

0 comments on commit 148b3a0

Please sign in to comment.