From a875f9ff78d41141d4e61efacaf4e9412959be5b Mon Sep 17 00:00:00 2001 From: zahiramanzur Date: Tue, 15 May 2018 12:52:46 -0300 Subject: [PATCH] fix resolved --- banner/forms.py | 2 +- banner/templates/event/sort_event.html | 4 +- banner/views.py | 52 ++++++++++++++++---------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/banner/forms.py b/banner/forms.py index dadbcb4..34e4295 100644 --- a/banner/forms.py +++ b/banner/forms.py @@ -96,7 +96,7 @@ class SelectBannerForm(forms.Form): class EventsSortForm(forms.Form): - sort = forms.IntegerField(required=True) + sort = forms.ChoiceField(required=True) event = forms.IntegerField( required=True, widget=forms.HiddenInput(), diff --git a/banner/templates/event/sort_event.html b/banner/templates/event/sort_event.html index 42cb041..b7dc572 100644 --- a/banner/templates/event/sort_event.html +++ b/banner/templates/event/sort_event.html @@ -49,8 +49,8 @@

Título

Descripción

{{ forms.description.value }}

-
- {% bootstrap_field forms.sort %} +
+ {{ forms.sort }}
diff --git a/banner/views.py b/banner/views.py index 9ae97d4..39434b3 100644 --- a/banner/views.py +++ b/banner/views.py @@ -144,7 +144,7 @@ def edit_banner(self, form, formset, *args, **kwargs): for event in updated_events: if event['selection']: - '''create events''' + '''add events in events (create in bd)''' if int(event['evb_id']) not in events_evb_id_list: e_design = EventDesign.objects.get( id=DEFAULT_EVENT_DESIGN, @@ -231,6 +231,8 @@ 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) @@ -247,21 +249,25 @@ def form_valid(self, form, formset): ) last_banner_id = Banner.objects.latest('created').id - for event in events: - if event is not None: - event.banner = banner - event.design = e_design - if Event.objects.all().count() == 0: - last_event_id = 1 - else: - last_event_id = Event.objects.latest('created').id - if event.custom_logo: - event.custom_logo = img_upload( - event.custom_logo, - last_banner_id + 1, - last_event_id + 1, - ) - event.save() + + count_events = [event for event in enumerate(sorted(events, reverse=True), 1) if event[1] != None] + + for idx, event in count_events: + + event.sort = idx + event.banner = banner + event.design = e_design + if Event.objects.all().count() == 0: + last_event_id = 1 + else: + last_event_id = Event.objects.latest('created').id + if event.custom_logo: + event.custom_logo = img_upload( + event.custom_logo, + last_banner_id + 1, + last_event_id + 1, + ) + event.save() except IntegrityError as error: print error.message @@ -563,7 +569,6 @@ def get_context_data(self, **kwargs): for event in events: initial_data.append( { - 'sort': 1, 'event': event.id, 'description': event.description, 'title': event.title, @@ -583,6 +588,10 @@ def get_context_data(self, **kwargs): initial=initial_data ) + for ev_form in formset: + sort = enumerate(range(1, len(events) + 1), 1) + ev_form.fields['sort'].choices = [i for i in sort] + context['form'] = form context['formset'] = formset context['events'] = events @@ -590,7 +599,6 @@ def get_context_data(self, **kwargs): return context def post(self, request, *args, **kwargs): - banner = Banner.objects.get(id=self.kwargs['pk']) events = Event.objects.filter(banner=banner) @@ -612,7 +620,6 @@ def post(self, request, *args, **kwargs): for event in events: initial_data.append( { - 'sort': 1, 'event': event.id, 'description': event.description, 'title': event.title, @@ -625,7 +632,12 @@ def post(self, request, *args, **kwargs): initial=initial_data ) + for ev_form in formset: + sort = enumerate(range(1, len(events) + 1), 1) + ev_form.fields['sort'].choices = [i for i in sort] + if bannerform.is_valid() and formset.is_valid(): + list_input_sort = [] for element in formset.cleaned_data: list_input_sort.append(element['sort']) @@ -637,7 +649,7 @@ def post(self, request, *args, **kwargs): NON_FIELD_ERRORS, 'Colocó ordenes iguales') - max_sort = max(sort) + max_sort = str(max(sort)) max_list_input_sort = max(list_input_sort) if max_list_input_sort > max_sort: