Ejemplo n.º 1
0
def wizard_event(request, step='step0', template='localsite/wizard_event.html'):
    wizard = request.session.get('wizard')
    output = {}
    formsets = []
    form = None

    if step == 'step0':
        if not wizard:
            wizard = {}
        product = Product()
        if request.method == 'POST':
            form = ProductForm(request.POST, instance=product)
            formsets.append(EventFormInline(request.POST, instance=product))
            if form.is_valid() and formsets[0].is_valid():
                product = form.save(commit=False)
                product.site = Site.objects.get(id=1)
                product.slug = slugify(cyr2lat(product.name))
                product.save()
                form.save_m2m()
                formsets[0].save()
                event = formsets[0].instance.event
                wizard['event'] = event
                wizard['step'] = 1
                request.session['wizard'] = wizard
                for group in event.hallscheme.groups.all():
                    price = SeatGroupPrice(event=event, group=group)
                    price.save()
                return HttpResponseRedirect('/wizards/event/step1/')
        else:
            form = ProductForm(instance=product)
            formsets.append(EventFormInline(instance=product))
    elif step == 'step1':
        if not wizard:
            return HttpResponseRedirect('/wizards/event/')
        event = wizard['event']
        step = wizard['step']
        if step != 1:
            return HttpResponseRedirect('/wizards/event/')
        if request.method == 'POST':
            form = SeatGroupPriceFormset(request.POST, queryset=event.prices.all())
            if form.is_valid():
                form.save()
                wizard['step'] = 2
                request.session['wizard'] = wizard
                return HttpResponseRedirect('/wizards/event/step2/')
        else:
            form = SeatGroupPriceFormset(queryset=event.prices.all())
    elif step == 'step2':
        template='localsite/wizard_event_dates.html'
        if not wizard:
            return HttpResponseRedirect('/wizards/event/')
        event = wizard['event']
        step = wizard['step']
        if step != 2:
            return HttpResponseRedirect('/wizards/event/')
        if request.method == 'POST':
            formsets.append(EventDateFormInline(request.POST, instance=event))
            if formsets[0].is_valid():
                formsets[0].save()
                wizard['step'] = 3
                request.session['wizard'] = wizard
                return HttpResponseRedirect('/wizards/event/step3/')
        else:
            formsets.append(EventDateFormInline(instance=event))
    elif step == 'step3':
        template='localsite/wizard_product_images.html'
        if not wizard:
            return HttpResponseRedirect('/wizards/event/')
        event = wizard['event']
        step = wizard['step']
        if step != 3:
            return HttpResponseRedirect('/wizards/event/')
        if request.method == 'POST':
            formsets.append(ProductImageFormInline(request.POST, request.FILES, instance=event.product))
            if formsets[0].is_valid():
                formsets[0].save()
                wizard['step'] = 4
                request.session['wizard'] = wizard
                return HttpResponseRedirect('/wizards/event/step4/')
        else:
            formsets.append(ProductImageFormInline(instance=event.product))
    elif step == 'step4':
        if not wizard:
            return HttpResponseRedirect('/wizards/event/')
        event = wizard['event']
        step = wizard['step']
        if step != 4:
            return HttpResponseRedirect('/wizards/event/')
        if request.method == 'POST':
            formsets.append(AnnouncementFormInline(request.POST, request.FILES, instance=event))
            if formsets[0].is_valid():
                formsets[0].save()
                wizard['step'] = 5
                request.session['wizard'] = wizard
                return HttpResponseRedirect('/wizards/event/done/')
        else:
            formsets.append(AnnouncementFormInline(instance=event))
    elif step == 'done':
        template='localsite/wizard_event_done.html'
        if not wizard:
            return HttpResponseRedirect('/wizards/event/')
        event = wizard['event']
        output['event'] = event
        step = wizard['step']
        if step != 5:
            return HttpResponseRedirect('/wizards/event/')
        event.create_all_variations()
        del request.session['wizard']
        return HttpResponseRedirect(event.get_absolute_url())

    if form:
        output['form'] = form
    if formsets:
        output['formsets'] = formsets
    ctx = RequestContext(request, output)
    return render_to_response(template, context_instance=ctx)
Ejemplo n.º 2
0
    def get_product(self, day, schedule=None, tour_time=None, create=True):
        """
        Get the TourProduct for the given day and time.  If create is false and product does
        not exist, return None.
        """

        if schedule and tour_time:
            assert False, 'Make up your mind, use a schedule or a tour_time, not both'
        if not schedule and not tour_time:
            assert False, 'Need a time buddy'

        try:
            if schedule:
                tour_product = TourProduct.objects.filter(tour_type=self, day=day).filter(
                    Q(schedule=schedule) | Q(tour_time=schedule.tour_time))[0]
            else:
                tour_product = TourProduct.objects.get(tour_type=self, day=day, tour_time=tour_time)

            if tour_product.tour_type.has_price_raise():
                p = tour_product.product.price_set.all()[0]
                p.price = self.get_raised_base_price(tour_product.day)
                p.save()

            return tour_product
        except (TourProduct.DoesNotExist, IndexError):
            if create:
                # print 'creating product... ', day
                product = Product()
                product.site = Site.objects.get_current()
                product.name = '%s on %s at ' % (self.name, day.strftime('%B %d, %Y'))
                if schedule:
                    product.name += schedule.tour_time.strftime('%I:%M %p')
                else:
                    product.name += tour_time.strftime('%I:%M %p')
                product.items_in_stock = self.get_capacity_for_day(day)
                product.shipclass = 'NO'  # see product.models.SHIP_CLASS_CHOICES
                product.save()

                tour_product = TourProduct()
                tour_product.product = product
                tour_product.tour_type = self
                tour_product.day = day
                if schedule:
                    tour_product.schedule = schedule
                    tour_product.tour_time = schedule.tour_time
                else:
                    tour_product.schedule = None
                    tour_product.tour_time = tour_time
                tour_product.save()

                p = Price()
                p.product = product
                if tour_product.tour_type.has_price_raise():
                    p.price = self.get_raised_base_price(tour_product.day)
                else:
                    p.price = self.base_price
                p.save()

                # Create Variations
                if self.option_group_id:
                    configurable_product = ConfigurableProduct(product=product)
                    configurable_product.save()
                    configurable_product.option_group.add(self.option_group)
                    configurable_product.create_subs = True
                    configurable_product.save()
                return tour_product
            else:
                return None