From 1e56d5a09a3105b90143e9a4cdfb75c633bb41e9 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Thu, 2 Nov 2017 12:59:23 +1100 Subject: [PATCH] Install django-model-utils and use it for automatic timestamp fields on entries --- Pipfile | 1 + Pipfile.lock | 8 +++- entries/admin.py | 4 +- entries/migrations/0006_auto_20171102_1200.py | 41 +++++++++++++++++++ entries/models.py | 18 +++++--- 5 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 entries/migrations/0006_auto_20171102_1200.py diff --git a/Pipfile b/Pipfile index f62c9dd..87c5bf6 100644 --- a/Pipfile +++ b/Pipfile @@ -37,6 +37,7 @@ django-annoying = "*" django-shorturls = "*" accept-types = "*" django-analytical = "*" +django-model-utils = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 3a61c96..95545cd 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "d1b1d871bb10ecfd71c966c40292cb5ba24d4dc70639d9b147c302e04b522a62" + "sha256": "0c229a0ec55a08b58868faee7ce35a6adc3621d8e8619a67ec8939afcccc7dfe" }, "host-environment-markers": { "implementation_name": "cpython", @@ -145,6 +145,12 @@ ], "version": "==1.4" }, + "django-model-utils": { + "hashes": [ + "sha256:60ead1ba50e1353f38bde12ab8b4a80b6a0f825a8e53c348fe259548cbd1a312" + ], + "version": "==3.0.0" + }, "django-otp": { "hashes": [ "sha256:54f35d7a84d8c46f35d20b969f38ef1afc0fa7627e44c481e4ab5f66a8da187e", diff --git a/entries/admin.py b/entries/admin.py index 408e83c..c1cf273 100644 --- a/entries/admin.py +++ b/entries/admin.py @@ -8,8 +8,8 @@ class SyndicationInline(admin.TabularInline): class EntryAdmin(admin.ModelAdmin): - date_hierarchy = 'published' - list_display = ('title', 'id', 'kind', 'published') + date_hierarchy = 'created' + list_display = ('title', 'id', 'kind', 'created') list_filter = ('kind',) inlines = ( SyndicationInline, diff --git a/entries/migrations/0006_auto_20171102_1200.py b/entries/migrations/0006_auto_20171102_1200.py new file mode 100644 index 0000000..af62ba0 --- /dev/null +++ b/entries/migrations/0006_auto_20171102_1200.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.6 on 2017-11-02 01:00 +from __future__ import unicode_literals + +from django.db import migrations +import django.utils.timezone +import model_utils.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('entries', '0005_auto_20171027_1557'), + ] + + operations = [ + migrations.AlterModelOptions( + name='entry', + options={'ordering': ['-created'], 'verbose_name_plural': 'entries'}, + ), + migrations.RenameField( + model_name='entry', + old_name='published', + new_name='created', + ), + migrations.RenameField( + model_name='entry', + old_name='updated', + new_name='modified', + ), + migrations.AlterField( + model_name='entry', + name='created', + field=model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created'), + ), + migrations.AlterField( + model_name='entry', + name='modified', + field=model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified'), + ), + ] diff --git a/entries/models.py b/entries/models.py index f57e8af..7c4644a 100644 --- a/entries/models.py +++ b/entries/models.py @@ -8,6 +8,7 @@ from textwrap import shorten from urllib.parse import urljoin from meta.models import ModelMeta +from model_utils.models import TimeStampedModel from users.models import Profile from . import kinds @@ -20,7 +21,7 @@ class EntryManager(models.Manager): return qs.select_related('author').prefetch_related('syndications') -class Entry(ModelMeta, models.Model): +class Entry(ModelMeta, TimeStampedModel): objects = EntryManager() kind = models.CharField( max_length=30, @@ -39,8 +40,13 @@ class Entry(ModelMeta, models.Model): on_delete=models.CASCADE, ) - published = models.DateTimeField() - updated = models.DateTimeField() + @property + def published(self): + return self.created + + @property + def updated(self): + return self.modified _metadata = { 'description': 'excerpt', @@ -117,8 +123,8 @@ class Entry(ModelMeta, models.Model): }, 'headline': self.title, 'description': self.excerpt, - 'datePublished': self.published.isoformat(), - 'dateModified': self.updated.isoformat(), + 'datePublished': self.created.isoformat(), + 'dateModified': self.modified.isoformat(), } if self.photo: posting['image'] = (urljoin(base, self.photo.url), ) @@ -126,7 +132,7 @@ class Entry(ModelMeta, models.Model): class Meta: verbose_name_plural = 'entries' - ordering = ['-published'] + ordering = ['-created'] class SyndicationManager(models.Manager):