Commit 90a82479 authored by Ciella Francisco's avatar Ciella Francisco 😵

Modified views.py to enable integration of formset for multiple image upload

parent ce0cf129
...@@ -5,11 +5,53 @@ from django.views.generic.detail import DetailView ...@@ -5,11 +5,53 @@ from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView from django.views.generic.edit import CreateView, UpdateView
from .forms import EventForm from .forms import (EventForm, PromoFormSet)
from .models import Event from .models import Event, Promo
def delete_image(request, pk):
try:
image = Promo.objects.get(id=pk)
except Promo.DoesNotExist:
messages.success(
request, 'Object Does not exist'
)
return redirect('event_management:event-update', pk=image.event.id)
image.delete()
messages.success(
request, 'Image deleted successfully'
)
return redirect('event_management:event-update', pk=image.event.id)
class PromoInline():
form_class = EventForm
template_name = 'event_management/event-form.html'
def form_valid(self, form):
named_formsets = self.get_named_formsets()
if not all((x.is_valid() for x in named_formsets.values())):
return self.render_to_response(self.get_context_data(form=form))
self.object = form.save()
for name, formset in named_formsets.items():
formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None)
if formset_save_func is not None:
formset_save_func(formset)
else:
formset.save()
return redirect('event_management:event-list')
def formset_images_valid(self, formset):
images = formset.save(commit=False)
for obj in formset.deleted_objects:
obj.delete()
for image in images:
image.event = self.object
image.save()
class EventDetailView(DetailView): class EventDetailView(DetailView):
model = Event model = Event
fields = '__all__'
template_name = 'event_management/event-details.html' template_name = 'event_management/event-details.html'
class EventListView(ListView): class EventListView(ListView):
...@@ -17,13 +59,43 @@ class EventListView(ListView): ...@@ -17,13 +59,43 @@ class EventListView(ListView):
fields = '__all__' fields = '__all__'
template_name = 'event_management/event-list.html' template_name = 'event_management/event-list.html'
class EventCreateView(CreateView): # class EventCreateView(CreateView):
# model = Event
# form_class = EventForm
# success_url = '/events/'
# template_name = 'event_management/event-form.html'
# class EventUpdateView(UpdateView):
# model = Event
# fields = '__all__'
# # success_url =
# template_name = 'event_management/event-form.html'
class EventCreateView(PromoInline, CreateView):
model = Event model = Event
form_class = EventForm def get_context_data(self, **kwargs):
success_url = '/events/' ctx = super(EventCreateView, self).get_context_data(**kwargs)
template_name = 'event_management/event-form.html' ctx['named_formsets'] = self.get_named_formsets()
return ctx
class EventUpdateView(UpdateView): def get_named_formsets(self):
if self.request.method == "GET":
return{
'images': PromoFormSet(prefix='images')
}
else:
return{
'images': PromoFormSet(self.request.POST or None, self.request.FILES or None, prefix='images')
}
class EventUpdateView(PromoInline, UpdateView):
model = Event model = Event
fields = '__all__' def get_context_data(self, **kwargs):
template_name = 'event_management/event-form.html' ctx = super(EventUpdateView, self).get_context_data(**kwargs)
\ No newline at end of file ctx['named_formsets'] = self.get_named_formsets()
return ctx
def get_named_formsets(self):
return {
'images': PromoFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object, prefix='images'),
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment