Beispiel #1
0
def calculator(request):
    output = {
        'tickets': [],
        'coupon': 0,
        'total': 0,
    }
    if request.method == 'POST':
        form = p3forms.P3FormTickets(data=request.POST,
                                     user=request.user.assopy_user)
        if not form.is_valid():
            # se la form non valida a causa del coupon lo elimino dai dati per
            # dare cmq un feedback all'utente
            if 'coupon' in form.errors:
                qdata = request.POST.copy()
                del qdata['coupon']
                form = p3forms.P3FormTickets(data=qdata,
                                             user=request.user.assopy_user)

        if form.is_valid():
            data = form.cleaned_data
            coupons = []
            if data['coupon']:
                coupons.append(data['coupon'])
            totals = amodels.Order\
                .calculator(items=data['tickets'], coupons=coupons, user=request.user.assopy_user)

            def _fmt(x):
                if x == 0:
                    # x è un Decimal e ottengo una rappresentazione diversa tra 0 e -0
                    return '0'
                else:
                    return '%.2f' % x

            grand_total = 0
            # per permettere al client di associare ad ogni biglietto il giusto
            # costo, riscrivo le informazioni nello stesso "formato" in cui mi
            # sono state inviate.
            tickets = []
            for row in totals['tickets']:
                fcode = row[0].code
                total = row[2]
                params = row[1]
                if 'period' in params:
                    start = settings.P3_HOTEL_RESERVATION['period'][0]
                    params['period'] = map(lambda x: (x - start).days,
                                           params['period'])
                tickets.append((fcode, params, _fmt(total)))
                grand_total += total
            output['tickets'] = tickets

            if data['coupon']:
                total = totals['coupons'][data['coupon'].code][0]
                output['coupon'] = _fmt(total)
                grand_total += total

            output['total'] = _fmt(grand_total)
        else:
            return form.errors

    return output
Beispiel #2
0
def cart(request):
    u = None
    if request.user.is_authenticated():
        try:
            u = request.user.assopy_user
        except AttributeError:
            pass
    at = 'p'

    # user-cart is needed for the confirmation page with invoce data,
    # I want to be suer that the only way to set it is when there is
    # a valid POST request
    request.session.pop('user-cart', None)
    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseRedirectSeeOther(reverse('p3-cart'))
        form = p3forms.P3FormTickets(data=request.POST, user=u)
        if form.is_valid():
            request.session['user-cart'] = form.cleaned_data
            return redirect('p3-billing')
    else:
        form = p3forms.P3FormTickets(initial={
            'order_type':
            'deductible' if at == 'c' else 'non-deductible',
        },
                                     user=u)
    fares = {}
    for f in form.available_fares():
        if not f.code.startswith('_'):
            fares[f.code] = f
    fares_ordered = sorted(fares.values(), key=lambda x: x.name)
    ctx = {
        'form': form,
        'fares': fares,
        'fares_ordered': fares_ordered,
        'account_type': at,
    }
    return render(request, 'p3/cart.html', ctx)
Beispiel #3
0
def cart(request):
    u = None
    if request.user.is_authenticated():
        try:
            u = request.user.assopy_user
        except AttributeError:
            pass
    at = 'p'

    # user-cart serve alla pagina di conferma con i dati di fatturazione,
    # voglio essere sicuro che l'unico modo per impostarlo sia quando viene
    # fatta una POST valida
    request.session.pop('user-cart', None)
    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseRedirectSeeOther(reverse('p3-cart'))
        form = p3forms.P3FormTickets(data=request.POST, user=u)
        if form.is_valid():
            request.session['user-cart'] = form.cleaned_data
            return redirect('p3-billing')
    else:
        form = p3forms.P3FormTickets(initial={
            'order_type':
            'deductible' if at == 'c' else 'non-deductible',
        },
                                     user=u)
    fares = {}
    for f in form.available_fares():
        if not f.code.startswith('_'):
            fares[f.code] = f
    fares_ordered = sorted(fares.values(), key=lambda x: x.name)
    ctx = {
        'form': form,
        'fares': fares,
        'fares_ordered': fares_ordered,
        'account_type': at,
    }
    return render(request, 'p3/cart.html', ctx)
Beispiel #4
0
def calculator(request):
    output = {
        'tickets': [],
        'coupon': 0,
        'total': 0,
    }
    if request.method == 'POST':
        form = p3forms.P3FormTickets(data=request.POST,
                                     user=request.user.assopy_user)
        if not form.is_valid():
            # if the fom is not validate because of the coupon I'm deleting it
            # from the data to be able to give the user a feedback anyway
            if 'coupon' in form.errors:
                qdata = request.POST.copy()
                del qdata['coupon']
                form = p3forms.P3FormTickets(data=qdata,
                                             user=request.user.assopy_user)

        if form.is_valid():
            data = form.cleaned_data
            coupons = []
            if data['coupon']:
                coupons.append(data['coupon'])
            totals = amodels.Order\
                .calculator(items=data['tickets'], coupons=coupons, user=request.user.assopy_user)
            try:
                booking = models.HotelBooking.objects\
                    .get(conference=settings.CONFERENCE_CONFERENCE)
            except models.HotelBooking.DoesNotExist:
                booking = None

            def _fmt(x):
                if x == 0:
                    # x is a Decimal and 0 and -0 are different
                    return '0'
                else:
                    return '%.2f' % x

            grand_total = 0
            # to allow the client to associate each ticket with the correct price
            # infos are rewritten inthe same "format" that has been used to send them.
            tickets = []
            for row in totals['tickets']:
                fcode = row[0].code
                total = row[2]
                params = row[1]
                if 'period' in params:
                    start = booking.booking_start
                    params['period'] = map(lambda x: (x - start).days,
                                           params['period'])
                tickets.append((fcode, params, _fmt(total)))
                grand_total += total
            output['tickets'] = tickets

            if data['coupon']:
                total = totals['coupons'][data['coupon'].code][0]
                output['coupon'] = _fmt(total)
                grand_total += total

            output['total'] = _fmt(grand_total)
        else:
            return form.errors

    return output