From d4809e29c165ae1c0f9e143e61f3e3b96bb2fcf9 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 22 Feb 2018 19:01:52 +0530 Subject: [PATCH] add photo relation --- content/admin.py | 1 + content/migrations/0010_auto_20180222_1319.py | 56 +++++++++++++++++++ content/models.py | 8 ++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 content/migrations/0010_auto_20180222_1319.py diff --git a/content/admin.py b/content/admin.py index 372a1db..7784cc2 100644 --- a/content/admin.py +++ b/content/admin.py @@ -55,6 +55,7 @@ class ContentAdmin(admin.ModelAdmin): list_display = ('id', '__str__', 'datestart', 'shortname', 'type') list_filter = ['datestart', 'type'] search_fields = ['title', 'body', 'header', 'shortname'] + raw_id_fields = ['photo'] inlines = [ContentParentsInline, FileInline, LinkInline] formfield_overrides = { models.TextField: {'widget': AdminMarkdownxWidget}, diff --git a/content/migrations/0010_auto_20180222_1319.py b/content/migrations/0010_auto_20180222_1319.py new file mode 100644 index 0000000..88b331c --- /dev/null +++ b/content/migrations/0010_auto_20180222_1319.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2018-02-22 13:19 +from __future__ import unicode_literals + +from django.db import migrations, models +import photologue.models + +def migrate_photos(apps, schema_editor): + Content = apps.get_model('content', 'Content') + Photo = apps.get_model('photologue', 'Photo') + + for c in Content.objects.exclude(image=None).exclude(image=""): + if not c.photo: + try: + if '/photo' in c.image: + c.photo = Photo.objects.get(image__endswith=c.image.split('/')[-1]) + else: + c.photo = Photo.objects.get(title=c.image) + except Photo.DoesNotExist: + continue + c.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('content', '0009_auto_20171219_1320'), + ('photologue', '0010_auto_20160105_1307'), + ] + + operations = [ + migrations.AlterModelOptions( + name='contentcontent', + options={'verbose_name': 'related content', 'verbose_name_plural': 'related content'}, + ), + migrations.AlterModelOptions( + name='file', + options={'ordering': ['order', 'file']}, + ), + migrations.AlterModelOptions( + name='link', + options={'ordering': ['order', 'url']}, + ), + migrations.AddField( + model_name='content', + name='photo', + field=models.ForeignKey(blank=True, null=True, on_delete=photologue.models.Photo, related_name='main_photo', to='content.Image'), + ), + migrations.AlterField( + model_name='content', + name='image', + field=models.CharField(blank=True, editable=False, max_length=150, null=True), + ), + migrations.RunPython(migrate_photos), + + ] diff --git a/content/models.py b/content/models.py index 1d01248..e574856 100644 --- a/content/models.py +++ b/content/models.py @@ -76,7 +76,9 @@ class Content(models.Model): optbtn3 = models.CharField(db_column='optBtn3', max_length=127, blank=True, null=True) # Field name made lowercase. opttext3 = models.TextField(db_column='optText3', blank=True, null=True) # Field name made lowercase. technotes = models.TextField(db_column='technotes', blank=True, null=True) - image = models.CharField(max_length=150, blank=True, null=True) + image = models.CharField(max_length=150, blank=True, null=True, editable=False) + photo = models.ForeignKey('Image', Photo, null=True, blank=True, related_name="main_photo") + postedby = models.CharField(db_column='postedBy', max_length=50, blank=True, null=True) # Field name made lowercase. datestart = models.DateField(db_column='dateStart', blank=True, null=True) # Field name made lowercase. dateend = models.DateField(db_column='dateEnd', blank=True, null=True) # Field name made lowercase. @@ -123,6 +125,8 @@ class Content(models.Model): @property def image_url(self): + if self.photo.image.url: + return self.photo.image.url if self.image: if self.image.startswith('http') or self.image.startswith('/'): return self.image @@ -174,6 +178,8 @@ class ContentContent(models.Model): class Meta: # managed = False db_table = 'content_content' + verbose_name = 'related content' + verbose_name_plural = 'related content' def reverse(self): r, created = ContentContent.objects.get_or_create(contentid1=self.contentid2, contentid2=self.contentid1)