From e795950e16bf45a229eb6822dc5488a210db0edc Mon Sep 17 00:00:00 2001 From: root Date: Tue, 19 Dec 2017 13:34:43 +0000 Subject: [PATCH] resources, make shortname unique --- content/admin.py | 11 +++-- content/migrations/0007_content_gallery.py | 22 ++++++++++ content/migrations/0008_auto_20171219_1311.py | 20 +++++++++ content/migrations/0009_auto_20171219_1320.py | 25 +++++++++++ content/models.py | 44 ++++++++++++------- content/templates/links.html | 18 ++++++-- 6 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 content/migrations/0007_content_gallery.py create mode 100644 content/migrations/0008_auto_20171219_1311.py create mode 100644 content/migrations/0009_auto_20171219_1320.py diff --git a/content/admin.py b/content/admin.py index 0564513..b010f3d 100644 --- a/content/admin.py +++ b/content/admin.py @@ -15,14 +15,18 @@ class ContentParentsInline(admin.TabularInline): model = ContentContent fk_name = 'contentid2' raw_id_fields = ['contentid1'] + extra = 0 class ImagesInline(admin.StackedInline): + extra = 0 model = Image class FileInline(admin.StackedInline): + extra = 0 model = File class LinkInline(admin.StackedInline): + extra = 0 model = Link ''' @@ -52,17 +56,12 @@ class ContentAdmin(admin.ModelAdmin): list_display = ('__unicode__', 'datestart', 'shortname', 'type') list_filter = ['datestart', 'type'] search_fields = ['title', 'body', 'header', 'shortname'] - inlines = [ContentParentsInline, ImagesInline, FileInline, LinkInline] + inlines = [ContentParentsInline, FileInline, LinkInline] formfield_overrides = { models.TextField: {'widget': AdminMarkdownxWidget}, } -# inlines = [SubdomainInline, DomainAliasInline] -# list_display = ('url', 'server', 'manage_nameserver', 'domain_registrar', 'email', 'is_active') -# list_editable = ('server', 'manage_nameserver', 'domain_registrar', 'email', 'is_active') - - admin.site.register(Content, ContentAdmin) admin.site.unregister(Gallery) admin.site.register(Gallery, GalleryAdmin) diff --git a/content/migrations/0007_content_gallery.py b/content/migrations/0007_content_gallery.py new file mode 100644 index 0000000..d867345 --- /dev/null +++ b/content/migrations/0007_content_gallery.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2017-12-19 13:06 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('photologue', '0010_auto_20160105_1307'), + ('content', '0006_auto_20171219_1243'), + ] + + operations = [ + migrations.AddField( + model_name='content', + name='gallery', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='content', to='photologue.Gallery'), + ), + ] diff --git a/content/migrations/0008_auto_20171219_1311.py b/content/migrations/0008_auto_20171219_1311.py new file mode 100644 index 0000000..bc3415f --- /dev/null +++ b/content/migrations/0008_auto_20171219_1311.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2017-12-19 13:11 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('content', '0007_content_gallery'), + ] + + operations = [ + migrations.AlterField( + model_name='content', + name='shortname', + field=models.CharField(db_column='shortName', max_length=255, unique=True), + ), + ] diff --git a/content/migrations/0009_auto_20171219_1320.py b/content/migrations/0009_auto_20171219_1320.py new file mode 100644 index 0000000..359c876 --- /dev/null +++ b/content/migrations/0009_auto_20171219_1320.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2017-12-19 13:20 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('content', '0008_auto_20171219_1311'), + ] + + operations = [ + migrations.RenameField( + model_name='file', + old_name='fil', + new_name='file', + ), + migrations.AlterField( + model_name='content', + name='shortname', + field=models.CharField(db_column='shortName', max_length=255, unique=True, verbose_name='Slug'), + ), + ] diff --git a/content/models.py b/content/models.py index 058f771..8ed1e7b 100644 --- a/content/models.py +++ b/content/models.py @@ -62,7 +62,7 @@ class Comments(models.Model): #not used class Content(models.Model): type = models.ForeignKey("ContentTypes", db_column="type") - shortname = models.CharField(db_column='shortName', max_length=255) # Field name made lowercase. + shortname = models.CharField('Slug', db_column='shortName', max_length=255, unique=True) title = models.CharField(max_length=255) header = MarkdownxField(blank=True, null=True, default='') body = MarkdownxField(blank=True, null=True, default='') @@ -87,6 +87,7 @@ class Content(models.Model): parents = models.ManyToManyField('Content', through='ContentContent', related_name="children") resources = models.ManyToManyField('Resources', through='ContentResource', related_name="content") + gallery = models.ForeignKey(Gallery, null=True, blank=True, related_name="content") # delete after migration parentid = models.IntegerField(null=True, db_column='parentID', blank=True, editable=False) @@ -123,9 +124,6 @@ class Content(models.Model): if self.image: return settings.IMAGE_PREFIX + self.image - def links(self): - return self.resources.filter(type=3).order_by('orderno') - def images(self): return self.resources.filter(type=2).exclude(href=self.image).order_by('orderno') @@ -142,16 +140,19 @@ class Content(models.Model): return '/' + '/'.join(parts) def get_gallery(self): - gallery, created = Gallery.objects.get_or_create(slug=self.shortname) - if created: - title = self.title - n = 1 - while Gallery.objects.filter(title=title).exclude(pk=gallery.pk).exists(): - n += 1 - title = '%s [%s]' % (self.title, n) - gallery.title = title - gallery.save() - return gallery + if not self.gallery: + gallery, created = Gallery.objects.get_or_create(slug=self.shortname) + if created: + title = self.title + n = 1 + while Gallery.objects.filter(title=title).exclude(pk=gallery.pk).exists(): + n += 1 + title = '%s [%s]' % (self.title, n) + gallery.title = title + gallery.save() + self.gallery = gallery + self.save() + return self.gallery class Meta: managed = True @@ -307,11 +308,21 @@ class Resources(models.Model): class File(models.Model): content = models.ForeignKey('Content') - fil = models.FileField(upload_to='files') + file = models.FileField(upload_to='files') description = models.TextField(blank=True, null=True) date = models.DateTimeField(auto_now_add=True) order = models.IntegerField(blank=True, null=True) + class Meta: + ordering = ['order', 'file'] + + def get_absolute_url(self): + return '/' + self.file.name + + @property + def name(self): + return self.description or self.file.name + class Image(models.Model): content = models.ForeignKey('Content', related_name='images') image = models.ImageField(upload_to='camp/static/images') @@ -326,6 +337,9 @@ class Link(models.Model): date = models.DateTimeField(auto_now_add=True) order = models.IntegerField(blank=True, null=True) + class Meta: + ordering = ['order', 'url'] + def __unicode__(self): return self.url diff --git a/content/templates/links.html b/content/templates/links.html index 3f26824..e10d0a7 100644 --- a/content/templates/links.html +++ b/content/templates/links.html @@ -1,10 +1,22 @@ -{% if content.links.exists %} +{% if content.link_set.exists %} +{% endif %} +{% if content.file_set.exists %} +
+ Resources: +