diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-01-24 10:10:09 +0000 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-01-24 10:10:09 +0000 |
commit | c488ad804bcc7c3febee3bea2089b86bbe419bbb (patch) | |
tree | e610f4f291cf3665e5ac481999ad27abc153d311 /ctrack/organisations | |
parent | 7a65d7e12c30992ceb6b94acc55d2ee152687b6e (diff) |
Played about with CSS/templates and tinkered with admin
Diffstat (limited to 'ctrack/organisations')
-rw-r--r-- | ctrack/organisations/admin.py | 1 | ||||
-rw-r--r-- | ctrack/organisations/migrations/0008_auto_20200123_1657.py | 29 | ||||
-rw-r--r-- | ctrack/organisations/models.py | 21 | ||||
-rw-r--r-- | ctrack/organisations/templates/organisations/organisation_detail.html | 50 | ||||
-rw-r--r-- | ctrack/organisations/urls.py | 2 | ||||
-rw-r--r-- | ctrack/organisations/views.py | 19 |
6 files changed, 116 insertions, 6 deletions
diff --git a/ctrack/organisations/admin.py b/ctrack/organisations/admin.py index 686cb30..f8ad2aa 100644 --- a/ctrack/organisations/admin.py +++ b/ctrack/organisations/admin.py @@ -15,6 +15,7 @@ class AddressInLine(admin.StackedInline): class OrganisationAdmin(admin.ModelAdmin): inlines = [AddressInLine,] + list_display = ('slug', 'name') # Register your models here. diff --git a/ctrack/organisations/migrations/0008_auto_20200123_1657.py b/ctrack/organisations/migrations/0008_auto_20200123_1657.py new file mode 100644 index 0000000..1b9f833 --- /dev/null +++ b/ctrack/organisations/migrations/0008_auto_20200123_1657.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2.9 on 2020-01-23 16:57 + +from django.db import migrations, models +import django.db.models.deletion +import django_extensions.db.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('organisations', '0007_auto_20200121_1806'), + ] + + operations = [ + migrations.AlterModelOptions( + name='address', + options={'verbose_name_plural': 'Addresses'}, + ), + migrations.AddField( + model_name='organisation', + name='slug', + field=django_extensions.db.fields.AutoSlugField(blank=True, editable=False, populate_from=['name']), + ), + migrations.AlterField( + model_name='address', + name='type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='organisations.AddressType', verbose_name='Address Type'), + ), + ] diff --git a/ctrack/organisations/models.py b/ctrack/organisations/models.py index 727ef58..0ecac94 100644 --- a/ctrack/organisations/models.py +++ b/ctrack/organisations/models.py @@ -2,22 +2,30 @@ from django.db import models # Create your models here. from django.urls import reverse +from django_extensions.db.fields import AutoSlugField from slugify import slugify class AddressType(models.Model): descriptor = models.CharField(max_length=50) + def __str__(self): + return self.descriptor + class Organisation(models.Model): name = models.CharField(max_length=255, blank=False) + slug = AutoSlugField(populate_from=['name']) def get_absolute_url(self): - return reverse("organisations:detail", kwargs={"name": self.slugify_name()}) + return reverse("organisations:detail", kwargs={"slug": self.slug}) def slugify_name(self): return slugify(self.name) + def __str__(self): + return self.name + class Address(models.Model): organisation = models.ForeignKey( @@ -27,13 +35,16 @@ class Address(models.Model): AddressType, verbose_name="Address Type", on_delete=models.CASCADE, blank=False ) line1 = models.CharField(max_length=255) - line2 = models.CharField(max_length=255) - line3 = models.CharField(max_length=255) + line2 = models.CharField(max_length=255, blank=True) + line3 = models.CharField(max_length=255, blank=True) city = models.CharField(max_length=100) - county = models.CharField(max_length=100) + county = models.CharField(max_length=100, blank=True) postcode = models.CharField(max_length=10) country = models.CharField(max_length=100) - other_details = models.CharField(max_length=255) + other_details = models.CharField(max_length=255, blank=True) + + def __str__(self): + return " ".join([self.organisation.name, self.line1]) class Meta: verbose_name_plural = "Addresses" diff --git a/ctrack/organisations/templates/organisations/organisation_detail.html b/ctrack/organisations/templates/organisations/organisation_detail.html new file mode 100644 index 0000000..5302ef4 --- /dev/null +++ b/ctrack/organisations/templates/organisations/organisation_detail.html @@ -0,0 +1,50 @@ +{% extends "base.html" %} + +{% block content %} + <h2>Organisation detail</h2> + <div class="container"> + <div class="row"> + {% if n_addr > 1 %} + {% for a in addr %} + <div class="col-md-6"> + <div class="card" style="width: 18rem;"> + <div class="card-body"> + <h5 class="card-title">{{ object.name }}</h5> + <h6 class="card-subtitle mb-2 text-muted">{{ a.type }}</h6> + <p class="card-text"> + {{ a.line1 }}<br> + {{ a.line2 }}<br> + {{ a.line3 }}<br> + {{ a.city }}<br> + {{ a.county }}<br> + {{ a.country}}<br> + </p> + <p><strong>{{ a.other_details }}</strong><br></p> + <a href="#" class="card-link">Edit Details</a> + </div> + </div> + </div> + {% endfor %} + {% else %} + <div class="col-md-12"> + <div class="card" style="width: 18rem;"> + <div class="card-body"> + <h5 class="card-title">{{ object.name }}</h5> + <h6 class="card-subtitle mb-2 text-muted">{{ addr.type }}</h6> + <p class="card-text"> + {{ addr.line1 }}<br> + {{ addr.line2 }}<br> + {{ addr.line3 }}<br> + {{ addr.city }}<br> + {{ addr.county }}<br> + {{ addr.country}}<br> + </p> + <p><strong>{{ addr.other_details }}</strong><br></p> + <a href="#" class="card-link">Edit Details</a> + </div> + </div> + </div> + {% endif %} + </div> + </div> +{% endblock %} diff --git a/ctrack/organisations/urls.py b/ctrack/organisations/urls.py index c6cc382..454d73f 100644 --- a/ctrack/organisations/urls.py +++ b/ctrack/organisations/urls.py @@ -5,5 +5,5 @@ from ctrack.organisations.views import OrganisationDetailView app_name = "organisations" urlpatterns = [ - path("<str:name>/", view=OrganisationDetailView, name="detail") + path("<slug:slug>/", view=OrganisationDetailView.as_view(), name="detail") ] diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py index 92bb134..1ec5469 100644 --- a/ctrack/organisations/views.py +++ b/ctrack/organisations/views.py @@ -1,8 +1,27 @@ +from typing import Any + from django.contrib.auth.mixins import LoginRequiredMixin +from django.http import HttpRequest +from django.http import HttpResponse from django.views.generic import DetailView +from typing import Dict from .models import Organisation class OrganisationDetailView(LoginRequiredMixin, DetailView): model = Organisation + + def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: + context = super().get_context_data() + org = kwargs['object'] + no_addr = org.addresses.count() + if no_addr > 1: + context['no_addr'] = no_addr + addr = org.addresses.all() + context['addr'] = addr + else: + context['no_addr'] = 1 + addr = org.addresses.first() + context['addr'] = addr + return context |