From 9f44c0f300776f2665a160f9bebf3c2ede5e07b0 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 14 Mar 2017 21:45:12 +0100 Subject: [PATCH] fix speaker proposal image upload stuff, maybe --- .../migrations/0034_auto_20170314_2012.py | 26 +++++++++++ src/program/models.py | 45 ++++++++++++++++++- .../templates/speakerproposal_form.html | 2 +- 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/program/migrations/0034_auto_20170314_2012.py diff --git a/src/program/migrations/0034_auto_20170314_2012.py b/src/program/migrations/0034_auto_20170314_2012.py new file mode 100644 index 00000000..71c8c956 --- /dev/null +++ b/src/program/migrations/0034_auto_20170314_2012.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-03-14 19:12 +from __future__ import unicode_literals + +from django.db import migrations, models +import program.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('program', '0033_auto_20170312_1857'), + ] + + operations = [ + migrations.AlterField( + model_name='speakerproposal', + name='picture_large', + field=models.ImageField(blank=True, help_text='A picture of the speaker', null=True, storage=program.models.CustomUrlStorage(), upload_to=program.models.get_speakerproposal_picture_upload_path), + ), + migrations.AlterField( + model_name='speakerproposal', + name='picture_small', + field=models.ImageField(blank=True, help_text='A thumbnail of the speaker picture', null=True, storage=program.models.CustomUrlStorage(), upload_to=program.models.get_speakerproposal_picture_upload_path), + ), + ] diff --git a/src/program/models.py b/src/program/models.py index 47a42545..4e6ce70b 100644 --- a/src/program/models.py +++ b/src/program/models.py @@ -8,6 +8,45 @@ from django.core.exceptions import ValidationError from datetime import timedelta from django.core.urlresolvers import reverse_lazy import uuid +from django.core.files.storage import FileSystemStorage +from django.urls import reverse +from django.apps import apps + + +class CustomUrlStorage(FileSystemStorage): + def __init__(self, location=None): + super(CustomUrlStorage, self).__init__(location) + + def url(self, name): + url = super(CustomUrlStorage, self).url(name) + parts = url.split("/") + if parts[0] != "public": + # first bit should always be "public" + return False + + if parts[1] == "speakerproposals": + # find speakerproposal + speakerproposal_model = apps.get_model('program', 'speakerproposal') + try: + speakerproposal = speakerproposal_model.objects.get(picture_small=name) + picture = "small" + except speakerproposal_model.DoesNotExist: + try: + speakerproposal = speakerproposal_model.objects.get(picture_large=name) + picture = "large" + except speakerproposal_model.DoesNotExist: + return False + url = reverse('speakerproposal_picture', kwargs={ + 'camp_slug': speakerproposal.camp.slug, + 'pk': speakerproposal.pk, + 'picture': picture, + }) + else: + return False + + return url + +storage = CustomUrlStorage() class UserSubmittedModel(CampRelatedModel): @@ -97,14 +136,16 @@ class SpeakerProposal(UserSubmittedModel): null=True, blank=True, upload_to=get_speakerproposal_picture_upload_path, - help_text='A picture of the speaker' + help_text='A picture of the speaker', + storage=storage, ) picture_small = models.ImageField( null=True, blank=True, upload_to=get_speakerproposal_picture_upload_path, - help_text='A thumbnail of the speaker picture' + help_text='A thumbnail of the speaker picture', + storage=storage, ) @property diff --git a/src/program/templates/speakerproposal_form.html b/src/program/templates/speakerproposal_form.html index cfe7d6cc..87723e7a 100644 --- a/src/program/templates/speakerproposal_form.html +++ b/src/program/templates/speakerproposal_form.html @@ -3,7 +3,7 @@ {% block program_content %}

{% if object %}Update{% else %}Create{% endif %} {{ camp.title }} Speaker Proposal

-
+ {% csrf_token %} {% bootstrap_form form %} {% bootstrap_button "Save draft" button_type="submit" button_class="btn-primary" %}