fix speaker proposal image upload stuff, maybe

This commit is contained in:
Thomas Steen Rasmussen 2017-03-14 21:45:12 +01:00
parent d159844750
commit 9f44c0f300
3 changed files with 70 additions and 3 deletions

View File

@ -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),
),
]

View File

@ -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

View File

@ -3,7 +3,7 @@
{% block program_content %}
<h3>{% if object %}Update{% else %}Create{% endif %} {{ camp.title }} Speaker Proposal</h3>
<form method="POST">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
{% bootstrap_button "Save draft" button_type="submit" button_class="btn-primary" %}