from django.contrib.formtools.wizard.views import SessionWizardView from django.core.files.storage import FileSystemStorage from django.http import HttpResponseRedirect, HttpResponseForbidden from django.conf import settings from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.core.exceptions import PermissionDenied import os from photologue.models import Photo from exposition.models import Exposition from functions.form_check import translit_with_separator from accounts.models import User from country.models import Country from city.models import City from place_exposition.models import PlaceExposition from theme.models import Tag, Theme class LoginRequiredMixin(object): @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): if not request.user.organiser: raise PermissionDenied return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs) # defining different template for every form TEMPLATES = { '0': 'client/wizard/first_step.html', '1': 'client/wizard/second_step.html', '2': 'client/wizard/third_step.html' } class ExpoWizard(LoginRequiredMixin, SessionWizardView): """main view that handle all data from 3 forms(steps) and finally create an Exposition""" # storing temporary files during upload location = os.path.join(settings.MEDIA_ROOT, 'temp') file_storage = FileSystemStorage(location, settings.MEDIA_URL) SUCCESS_URL = "/" def get_template_names(self): return [TEMPLATES[self.steps.current]] def done(self, form_list, **kwargs): # getting data and files upload_logo = form_list[0].cleaned_data.get('logo') upload_images = self.request.FILES.getlist(u'2-attachments') data = self.get_all_cleaned_data() # creating new exposition lang = self.request.LANGUAGE_CODE expo = Exposition.objects.language(lang).create( name=data.get('name'), data_begin=data.get('date_start'), data_end=data.get('date_end'), main_title=data.get('main_title'), description=data.get('description'), products=data.get('products'), country=Country.objects.language(lang).get(id=data.get('country')), city=City.objects.language(lang).get(id=data.get('city')), place=PlaceExposition.objects.language(lang).get(id=data.get('place')), periodic=data.get('periodic'), web_page=data.get('web_site'), logo=data.get('logo'), foundation_year=data.get('found_year'), area=data.get('square'), price_day=data.get('one_day'), price_all=data.get('all_days'), price_day_bar=data.get('pre_one_day'), price_all_bar=data.get('pre_all_days'), min_area=data.get('min_square'), registration_payment=data.get('registration_depos'), application_deadline=data.get('deadline_date'), min_closed_area=data.get('unequiped'), min_open_area=data.get('open_square'), min_closed_equipped_area=data.get('equiped'), url=translit_with_separator(data.get('name')), quality_label=0, audience=0, creator=User.objects.get(id=self.request.user.id) ) # adding photo to gallery photos = [] for i, photo in enumerate(upload_images): photos.append(Photo.objects.language(self.request.LANGUAGE_CODE).create( image=photo, title=data.get(u'pdescr_%i' % i, photo.name)) ) for photo in photos: expo.upload_photo(photo) expo.tag = Tag.objects.language(lang).filter(id__in=data.get('tag').split(',')) expo.theme = Theme.objects.language(lang).filter(id__in=data.get('theme')) # setting bit fields audience and quality_label self.set_flags(expo, data) expo.save() # remove temporary files if it has any if upload_logo: self.file_storage.delete(upload_logo.name) if upload_images: for f in upload_images: self.file_storage.delete(f.name) return HttpResponseRedirect(self.SUCCESS_URL) @staticmethod def set_flags(expo, data): if data['membership1']: expo.quality_label = (expo.quality_label | Exposition.quality_label.exporating) if data['membership2']: expo.quality_label = (expo.quality_label | Exposition.quality_label.rsva) if data['membership3']: expo.quality_label = (expo.quality_label | Exposition.quality_label.ufi) if data['audience1']: expo.audience = (expo.audience | Exposition.audience.experts) if data['audience2']: expo.audience = expo.audience | getattr(Exposition.audience, 'experts and consumers') if data['audience3']: expo.audience = expo.audience | (getattr(Exposition.audience, 'general public'))