From 16ca77302fb14d694b89a2416c326a347803ea1c Mon Sep 17 00:00:00 2001 From: zahiramanzur Date: Thu, 17 May 2018 10:40:46 -0300 Subject: [PATCH] Custom cover --- banner/forms.py | 1 + banner/migrations/0023_banner_custom_cover.py | 20 ++++++++ banner/models.py | 1 + banner/templates/event_list.html | 49 ++++++++++++++----- banner/utils.py | 24 +++++++-- banner/views.py | 15 ++++-- 6 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 banner/migrations/0023_banner_custom_cover.py diff --git a/banner/forms.py b/banner/forms.py index 34e4295..ac181ee 100644 --- a/banner/forms.py +++ b/banner/forms.py @@ -72,6 +72,7 @@ class BannerForm(forms.ModelForm): required=False, widget=forms.Textarea(attrs={'cols': 90, 'rows': 4}) ) + custom_cover = forms.FileField(required=False) class Meta: model = Banner diff --git a/banner/migrations/0023_banner_custom_cover.py b/banner/migrations/0023_banner_custom_cover.py new file mode 100644 index 0000000..90549a3 --- /dev/null +++ b/banner/migrations/0023_banner_custom_cover.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2018-05-17 14:38 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('banner', '0022_auto_20180507_1154'), + ] + + operations = [ + migrations.AddField( + model_name='banner', + name='custom_cover', + field=models.FileField(max_length=500, null=True, upload_to=b'custom_cover/'), + ), + ] diff --git a/banner/models.py b/banner/models.py index 9e6e16c..c611c04 100644 --- a/banner/models.py +++ b/banner/models.py @@ -47,6 +47,7 @@ class Banner(models.Model): design = models.ForeignKey(BannerDesign, null=True) title = models.CharField(max_length=200) description = models.TextField() + custom_cover = models.FileField(upload_to='custom_cover/', max_length=500, null=True) created = models.DateTimeField(auto_now_add=True, null=True, blank=True) changed = models.DateTimeField(auto_now=True, blank=True) diff --git a/banner/templates/event_list.html b/banner/templates/event_list.html index 6d0d2e6..6e41b11 100644 --- a/banner/templates/event_list.html +++ b/banner/templates/event_list.html @@ -21,7 +21,6 @@

Editar banner

{% endif %} -
@@ -39,18 +38,30 @@

Información del banner

Ingresa aquí la información correspondiente para que luego puedas identificar este banner y su contenido.

- {% for field in form %} -
- {% bootstrap_field field %} -
- {% endfor %} - -
+ {% for field in form %} +
+ {% bootstrap_field field %} +
+ {% endfor %} + + + + + {{ formset.management_form }} {{ formset.media }}
-
+

Seleccionar eventos

Selecciona los eventos que quieres que aparezcan cuando el banner esté @@ -58,7 +69,7 @@

Seleccionar eventos

-
+

Buscar eventos

Busca eventos de otros organizadores a traves de Eventbrite

@@ -139,6 +150,8 @@

Descripción

{% endif %}
+ + {% endif %} @@ -205,10 +218,24 @@

Descripción

+ + {% endblock %} diff --git a/banner/utils.py b/banner/utils.py index b5cf6ed..3b77970 100644 --- a/banner/utils.py +++ b/banner/utils.py @@ -1,5 +1,7 @@ """ This are the methods that supports the behaviour of the views """ import calendar +import hashlib +import time import json import requests import threading @@ -91,20 +93,34 @@ def replace_data(event): return event -def img_upload(logo, banner, event): +def get_unique_file_name(user_first_name, user_id, file_name): + timestamp = str(int(time.time())) + f_name = file_name.replace(' ', '-') + h = hashlib.sha224(timestamp).hexdigest() + name = user_first_name + '-' + \ + str(user_id) + '-' + h + '-' + f_name + return name + + +def img_upload(self, logo): """ Here is the upload of an image (or any file really) to the firebase service """ - + import ipdb; ipdb.set_trace() firebase = pyrebase.initialize_app(settings.FIREBASECONFIG) storage = firebase.storage() + unique_file_name = get_unique_file_name( + self.request.user.first_name, + self.request.user.id, + 'image.png' + ) storage.child( - 'custom_logos/' + str(banner) + '/' + str(event) + '-' + logo.name + 'custom_logos/' + unique_file_name ).put(logo) return storage.child( - 'custom_logos/' + str(banner) + '/' + str(event) + '-' + logo.name + 'custom_logos/' + unique_file_name ).get_url(1) diff --git a/banner/views.py b/banner/views.py index 39434b3..1642021 100644 --- a/banner/views.py +++ b/banner/views.py @@ -30,6 +30,7 @@ get_events_data, get_auth_token, get_api_events, + get_unique_file_name, img_upload, ) from eventbrite import Eventbrite @@ -121,6 +122,7 @@ def get_form_kwargs(self, **kwargs): ).get_form_kwargs() kwargs['initial']['title'] = banner.title kwargs['initial']['description'] = banner.description + kwargs['initial']['custom_cover'] = banner.custom_cover return kwargs def edit_banner(self, form, formset, *args, **kwargs): @@ -128,6 +130,7 @@ def edit_banner(self, form, formset, *args, **kwargs): updating_banner = Banner.objects.get(id=self.kwargs['pk']) updating_banner.title = form.cleaned_data['title'] updating_banner.description = form.cleaned_data['description'] + updating_banner.custom_cover = form.cleaned_data['custom_cover'] updating_banner.save() updating_events = Event.objects.filter(banner_id=self.kwargs['pk']) events_evb_id_list = [event.evb_id for event in updating_events] @@ -199,6 +202,7 @@ def edit_banner(self, form, formset, *args, **kwargs): def post(self, request, *args, **kwargs): form = forms.BannerForm( request.POST, + request.FILES, ) event_formset = modelformset_factory( @@ -232,7 +236,6 @@ def post(self, request, *args, **kwargs): @transaction.atomic def form_valid(self, form, formset): - form.instance.user = self.request.user banner = form.save(commit=False) events = formset.save(commit=False) @@ -242,6 +245,12 @@ def form_valid(self, form, formset): id=DEFAULT_BANNER_DESIGN, ) banner.design = b_design + if banner.custom_cover: + banner.custom_cover = img_upload( + self, + banner.custom_cover, + ) + import ipdb; ipdb.set_trace() banner.save() e_design = EventDesign.objects.get( @@ -250,7 +259,8 @@ def form_valid(self, form, formset): last_banner_id = Banner.objects.latest('created').id - count_events = [event for event in enumerate(sorted(events, reverse=True), 1) if event[1] != None] + count_events = [event for event in enumerate( + sorted(events, reverse=True), 1) if event[1] is not None] for idx, event in count_events: @@ -334,7 +344,6 @@ def get_context_data(self, **kwargs): return context - @method_decorator(login_required, name='dispatch') class BannerDetailView(DetailView, LoginRequiredMixin):