diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/sightings/migrations/0004_ap_main_image.py | 18 | ||||
-rw-r--r-- | app/sightings/migrations/0005_auto_20180516_1759.py | 24 | ||||
-rw-r--r-- | app/sightings/models.py | 13 |
3 files changed, 55 insertions, 0 deletions
diff --git a/app/sightings/migrations/0004_ap_main_image.py b/app/sightings/migrations/0004_ap_main_image.py new file mode 100644 index 0000000..70e1937 --- /dev/null +++ b/app/sightings/migrations/0004_ap_main_image.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.1 on 2018-05-16 17:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sightings', '0003_auto_20180209_1037'), + ] + + operations = [ + migrations.AddField( + model_name='ap', + name='main_image', + field=models.CharField(max_length=200, null=True), + ), + ] diff --git a/app/sightings/migrations/0005_auto_20180516_1759.py b/app/sightings/migrations/0005_auto_20180516_1759.py new file mode 100644 index 0000000..301c0d9 --- /dev/null +++ b/app/sightings/migrations/0005_auto_20180516_1759.py @@ -0,0 +1,24 @@ +# Generated by Django 2.0.1 on 2018-05-16 17:59 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('photos', '0018_auto_20161130_1218'), + ('sightings', '0004_ap_main_image'), + ] + + operations = [ + migrations.RemoveField( + model_name='ap', + name='main_image', + ), + migrations.AddField( + model_name='ap', + name='image', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='photos.LuxImage'), + ), + ] diff --git a/app/sightings/models.py b/app/sightings/models.py index 8531772..8673d4b 100644 --- a/app/sightings/models.py +++ b/app/sightings/models.py @@ -9,9 +9,11 @@ from django.utils import timezone from django import forms from django.conf import settings +from bs4 import BeautifulSoup # from photos.models import LuxImage from locations.models import Location +from photos.models import LuxImage from utils.widgets import parse_image from utils.widgets import markdown_to_html from utils.next_prev import next_in_order, prev_in_order @@ -22,6 +24,12 @@ def render_images(s): return s +def extract_main_image(s): + soup = BeautifulSoup(s, 'html.parser') + image = soup.find_all('img')[0]['id'] + return image.split('image-')[1] + + def get_upload_path(self, filename): return "images/sightings-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename) @@ -67,6 +75,7 @@ class AP(models.Model): apclass = models.ForeignKey(APClass, on_delete=models.CASCADE) body_html = models.TextField(null=True, blank=True) body_markdown = models.TextField(null=True, blank=True) + image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True) # image = models.FileField(upload_to=get_upload_path, null=True, blank=True, help_text="width of high res is 1360px") # image_credit = models.CharField(max_length=200, blank=True, null=True) @@ -115,6 +124,10 @@ class AP(models.Model): if self.pk: md = render_images(self.body_markdown) self.body_html = markdown_to_html(md) + try: + self.image = LuxImage.objects.get(pk=extract_main_image(self.body_markdown)) + except TypeError: + pass self.slug = slugify(self.common_name[:50]) super(AP, self).save(*args, **kwargs) |