From c574c8e6b285b4d5915bbd15e024adc676736ba1 Mon Sep 17 00:00:00 2001 From: Joost Rijneveld Date: Wed, 3 Aug 2016 23:24:14 +0200 Subject: [PATCH 1/5] Refactor to use database-backed Album model --- website/photos/admin.py | 6 ++-- website/photos/migrations/0001_initial.py | 25 +++++++++++++++++ website/photos/models.py | 32 ++++++++++------------ website/photos/templates/photos/index.html | 2 +- website/photos/urls.py | 2 +- website/photos/views.py | 12 +++----- 6 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 website/photos/migrations/0001_initial.py diff --git a/website/photos/admin.py b/website/photos/admin.py index 64d43253..8b67cd4e 100644 --- a/website/photos/admin.py +++ b/website/photos/admin.py @@ -1,3 +1,5 @@ -# from django.contrib import admin +from django.contrib import admin -# TODO figure out a nice way to make an admin interface +from .models import Album + +admin.site.register(Album) diff --git a/website/photos/migrations/0001_initial.py b/website/photos/migrations/0001_initial.py new file mode 100644 index 00000000..d82fce6a --- /dev/null +++ b/website/photos/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-08-03 21:16 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Album', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('dirname', models.CharField(max_length=200)), + ('date', models.DateField()), + ], + ), + ] diff --git a/website/photos/models.py b/website/photos/models.py index 83b89f64..a2248bbe 100644 --- a/website/photos/models.py +++ b/website/photos/models.py @@ -1,6 +1,7 @@ -from django.utils.text import slugify +from django.urls import reverse from django.conf import settings from django.utils.functional import cached_property +from django.db import models import os import re @@ -12,22 +13,17 @@ COVER_FILENAME = 'cover.jpg' @total_ordering -class Album(object): +class Album(models.Model): + title = models.CharField(max_length=200) + dirname = models.CharField(max_length=200) + date = models.DateField() photosdir = 'photos' photospath = os.path.join(settings.MEDIA_ROOT, photosdir) - def __init__(self, dirname): - date, self.title = dirname.split('_') - # This supports both 1990-11-07_albumname as well as 19901107_albumname - self.date = datetime.strptime(re.sub(r"\D", "", date), '%Y%m%d') - self.dirname = dirname - self.path = os.path.join(Album.photospath, self.dirname) - - @cached_property - def slug(self): - return slugify('{}-{}'.format(self.date.strftime('%Y-%m-%d'), - self.title)) + @property + def path(self): + return os.path.join(Album.photospath, self.dirname) @cached_property def cover(self): @@ -43,12 +39,14 @@ class Album(object): return [os.path.join(Album.photosdir, self.dirname, photo) for photo in os.listdir(self.path) if photo != COVER_FILENAME] - @classmethod - def all(cls): - return [cls(dirname) for dirname in os.listdir(Album.photospath)] - def __eq__(self, other): return self.date == other.date def __lt__(self, other): return self.date < other.date + + def __str__(self): + return '{} {}'.format(self.date.strftime('%Y-%m-%d'), self.title) + + def get_absolute_url(self): + return reverse('photos:album', args=[str(self.pk)]) diff --git a/website/photos/templates/photos/index.html b/website/photos/templates/photos/index.html index 88c3e87a..669dc143 100644 --- a/website/photos/templates/photos/index.html +++ b/website/photos/templates/photos/index.html @@ -10,7 +10,7 @@