Install django-model-utils and use it for automatic timestamp fields on entries

This commit is contained in:
Danielle McLean 2017-11-02 12:59:23 +11:00
parent 4b4ab324cc
commit 1e56d5a09a
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 63 additions and 9 deletions

View file

@ -37,6 +37,7 @@ django-annoying = "*"
django-shorturls = "*" django-shorturls = "*"
accept-types = "*" accept-types = "*"
django-analytical = "*" django-analytical = "*"
django-model-utils = "*"
[dev-packages] [dev-packages]

8
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "d1b1d871bb10ecfd71c966c40292cb5ba24d4dc70639d9b147c302e04b522a62" "sha256": "0c229a0ec55a08b58868faee7ce35a6adc3621d8e8619a67ec8939afcccc7dfe"
}, },
"host-environment-markers": { "host-environment-markers": {
"implementation_name": "cpython", "implementation_name": "cpython",
@ -145,6 +145,12 @@
], ],
"version": "==1.4" "version": "==1.4"
}, },
"django-model-utils": {
"hashes": [
"sha256:60ead1ba50e1353f38bde12ab8b4a80b6a0f825a8e53c348fe259548cbd1a312"
],
"version": "==3.0.0"
},
"django-otp": { "django-otp": {
"hashes": [ "hashes": [
"sha256:54f35d7a84d8c46f35d20b969f38ef1afc0fa7627e44c481e4ab5f66a8da187e", "sha256:54f35d7a84d8c46f35d20b969f38ef1afc0fa7627e44c481e4ab5f66a8da187e",

View file

@ -8,8 +8,8 @@ class SyndicationInline(admin.TabularInline):
class EntryAdmin(admin.ModelAdmin): class EntryAdmin(admin.ModelAdmin):
date_hierarchy = 'published' date_hierarchy = 'created'
list_display = ('title', 'id', 'kind', 'published') list_display = ('title', 'id', 'kind', 'created')
list_filter = ('kind',) list_filter = ('kind',)
inlines = ( inlines = (
SyndicationInline, SyndicationInline,

View file

@ -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'),
),
]

View file

@ -8,6 +8,7 @@ from textwrap import shorten
from urllib.parse import urljoin from urllib.parse import urljoin
from meta.models import ModelMeta from meta.models import ModelMeta
from model_utils.models import TimeStampedModel
from users.models import Profile from users.models import Profile
from . import kinds from . import kinds
@ -20,7 +21,7 @@ class EntryManager(models.Manager):
return qs.select_related('author').prefetch_related('syndications') return qs.select_related('author').prefetch_related('syndications')
class Entry(ModelMeta, models.Model): class Entry(ModelMeta, TimeStampedModel):
objects = EntryManager() objects = EntryManager()
kind = models.CharField( kind = models.CharField(
max_length=30, max_length=30,
@ -39,8 +40,13 @@ class Entry(ModelMeta, models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
published = models.DateTimeField() @property
updated = models.DateTimeField() def published(self):
return self.created
@property
def updated(self):
return self.modified
_metadata = { _metadata = {
'description': 'excerpt', 'description': 'excerpt',
@ -117,8 +123,8 @@ class Entry(ModelMeta, models.Model):
}, },
'headline': self.title, 'headline': self.title,
'description': self.excerpt, 'description': self.excerpt,
'datePublished': self.published.isoformat(), 'datePublished': self.created.isoformat(),
'dateModified': self.updated.isoformat(), 'dateModified': self.modified.isoformat(),
} }
if self.photo: if self.photo:
posting['image'] = (urljoin(base, self.photo.url), ) posting['image'] = (urljoin(base, self.photo.url), )
@ -126,7 +132,7 @@ class Entry(ModelMeta, models.Model):
class Meta: class Meta:
verbose_name_plural = 'entries' verbose_name_plural = 'entries'
ordering = ['-published'] ordering = ['-created']
class SyndicationManager(models.Manager): class SyndicationManager(models.Manager):