diff options
Diffstat (limited to 'app/jrnl')
-rw-r--r-- | app/jrnl/migrations/0025_auto_20190131_2335.py | 28 | ||||
-rw-r--r-- | app/jrnl/migrations/0026_entry_country_name.py | 18 | ||||
-rw-r--r-- | app/jrnl/models.py | 24 | ||||
-rw-r--r-- | app/jrnl/views.py | 49 |
4 files changed, 85 insertions, 34 deletions
diff --git a/app/jrnl/migrations/0025_auto_20190131_2335.py b/app/jrnl/migrations/0025_auto_20190131_2335.py new file mode 100644 index 0000000..60b9a8c --- /dev/null +++ b/app/jrnl/migrations/0025_auto_20190131_2335.py @@ -0,0 +1,28 @@ +# Generated by Django 2.1.1 on 2019-01-31 23:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('jrnl', '0024_auto_20180902_1217'), + ] + + operations = [ + migrations.AddField( + model_name='entry', + name='location_name', + field=models.CharField(blank=True, max_length=200, null=True), + ), + migrations.AddField( + model_name='entry', + name='region_name', + field=models.CharField(blank=True, max_length=200, null=True), + ), + migrations.AddField( + model_name='entry', + name='state_name', + field=models.CharField(blank=True, max_length=200, null=True), + ), + ] diff --git a/app/jrnl/migrations/0026_entry_country_name.py b/app/jrnl/migrations/0026_entry_country_name.py new file mode 100644 index 0000000..22d07f9 --- /dev/null +++ b/app/jrnl/migrations/0026_entry_country_name.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.1 on 2019-01-31 23:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('jrnl', '0025_auto_20190131_2335'), + ] + + operations = [ + migrations.AddField( + model_name='entry', + name='country_name', + field=models.CharField(blank=True, max_length=200, null=True), + ), + ] diff --git a/app/jrnl/models.py b/app/jrnl/models.py index 809353c..5b7457a 100644 --- a/app/jrnl/models.py +++ b/app/jrnl/models.py @@ -4,6 +4,7 @@ import os from django.dispatch import receiver from django.contrib.gis.db import models from django.urls import reverse +from django.utils.functional import cached_property from django.apps import apps from django.conf import settings from django.contrib.sitemaps import Sitemap @@ -44,6 +45,10 @@ class Entry(models.Model): enable_comments = models.BooleanField(default=False) point = models.PointField(null=True, blank=True) location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True) + location_name = models.CharField(max_length=200, blank=True, null=True) + state_name = models.CharField(max_length=200, blank=True, null=True) + country_name = models.CharField(max_length=200, blank=True, null=True) + region_name = models.CharField(max_length=200, blank=True, null=True) PUB_STATUS = ( (0, 'Draft'), (1, 'Published'), @@ -122,20 +127,7 @@ class Entry(models.Model): print(self.image.url) image_dir, img = self.image.url.split('post-images/')[1].split('/') print(image_dir, img) - return '%spost-images/%s/%s' % (settings.IMAGES_URL, image_dir, img) - - - @property - def state(self): - return self.location.state - - @property - def country(self): - return self.location.state.country - - @property - def region(self): - return self.location.state.country.lux_region + return '%spost-images/%s/%s' % (settings.IMAGES_URL, image_dir, img) @property def longitude(self): @@ -184,6 +176,10 @@ class Entry(models.Model): self.location = Location.objects.filter(geometry__contains=self.point).get() except Location.DoesNotExist: raise forms.ValidationError("There is no location associated with that point, add it: %sadmin/locations/location/add/" % (settings.BASE_URL)) + self.location_name = self.location.name + self.state_name = self.location.state.name + self.country_name = self.location.state.country.name + self.region_name = self.location.state.country.lux_region.name if created and not self.featured_image: self.featured_image = LuxImage.objects.latest() old = type(self).objects.get(pk=self.pk) if self.pk else None diff --git a/app/jrnl/views.py b/app/jrnl/views.py index cd63482..5dc627e 100644 --- a/app/jrnl/views.py +++ b/app/jrnl/views.py @@ -9,7 +9,7 @@ from django.db.models import Q from utils.views import PaginatedListView from .models import Entry, HomepageCurrator, Home -from locations.models import CheckIn, Country, Region, Location +from locations.models import LuxCheckIn, Country, Region, Location from sightings.models import Sighting @@ -17,8 +17,12 @@ class EntryList(PaginatedListView): """ Return a list of Entries in reverse chronological order """ - queryset = Entry.objects.filter(status__exact=1).order_by('-pub_date').select_related() - template_name = "archives/jrnl.html" + model = Entry + + def get_queryset(self): + queryset = super(EntryList, self).get_queryset() + print(queryset) + return queryset.filter(status__exact=1).order_by('-pub_date').select_related('location').prefetch_related('featured_image') class EntryCountryList(PaginatedListView): @@ -72,21 +76,21 @@ class EntryDetailView(DetailView): template_name = "details/entry.html" slug_field = "slug" - def get_object(self): - obj = get_object_or_404( - self.model, - slug=self.kwargs['slug'], - pub_date__month=self.kwargs['month'], - pub_date__year=self.kwargs['year'] - ) + def get_queryset(self): + queryset = super(EntryDetailView, self).get_queryset() + return queryset.select_related('location').prefetch_related('field_notes').prefetch_related('books') + + def get_object(self, queryset=None): + obj = super(EntryDetailView, self).get_object(queryset=queryset) + self.location = obj.location return obj def get_context_data(self, **kwargs): context = super(EntryDetailView, self).get_context_data(**kwargs) context['wildlife'] = Sighting.objects.filter( - Q(location=self.get_object().location) | - Q(location__in=Location.objects.filter(parent=self.get_object().location)) - ).order_by('ap_id', 'ap__apclass__kind').distinct("ap") + Q(location=self.location) | + Q(location__in=Location.objects.filter(parent=self.location)) + ).select_related().order_by('ap_id', 'ap__apclass__kind').distinct("ap") return context @@ -98,19 +102,24 @@ class HomepageList(ListView): """ Return a main entry and list of Entries in reverse chronological order """ - context_object_name = 'recent' - exclude = Home.objects.get(pk=1) - queryset = Entry.objects.filter(status__exact=1).exclude(pk=exclude.featured.pk)[:8] + model = Entry + + def get_home(self): + return Home.objects.filter(pk=1).prefetch_related('featured_image').select_related('featured').select_related('featured__location').get() + + def get_queryset(self): + queryset = super(HomepageList, self).get_queryset() + self.home = self.get_home() + return queryset.filter(status__exact=1).order_by('-pub_date').exclude().select_related('location').select_related('featured_image')[:8] def get_template_names(self): - obj = Home.objects.get(pk=1) - return ['%s' % obj.template_name] + return ['%s' % self.home.template_name] def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(HomepageList, self).get_context_data(**kwargs) - context['homepage'] = Home.objects.get(pk=1) - context['location'] = CheckIn.objects.latest() + context['homepage'] = self.home + context['location'] = LuxCheckIn.objects.latest() context['IMAGES_URL'] = settings.IMAGES_URL return context |