def test_coupon_is_valid(self): 
        
        user = User()
        user.first_name = "justin"
        user.last_name = "gray"
        user.username = "******"
        user.email = "*****@*****.**"
        user.save()
        
        user2 = User()
        user2.first_name = "sirius"
        user2.last_name = "gray"
        user2.username = "******"
        user2.save()

        club = Club()
        club.name ="test-test"
        club.save()
 
        c = Coupon()
        c.discount_amount = "10.00"
        c.permanent = True
        c.code = "aaa"
        c.expires = datetime.date.today() + datetime.timedelta(days=1)
        c.club = club
        c.save()
        
        o = Order()
        o.coupon = c
        o.user_prof = user.get_profile()
        o.save()
        
        self.assertTrue(c.is_valid(user))
        c.uses_left = 0
        self.assertTrue(c.is_valid(user))

        c.expires = datetime.date.today() - datetime.timedelta(days=1)    
        self.assertTrue(c.is_valid(user))

        c.permanent = False
        self.assertFalse(c.is_valid(user))

        c.expires = datetime.date.today() + datetime.timedelta(days=1)
        c.uses_left = 1
        self.assertTrue(c.is_valid(user))
        c.uses_left = 0
        self.assertFalse(c.is_valid(user))
        
        c.uses_left = 1
        c.user_prof = user.get_profile()
        self.assertFalse(c.is_valid(user2))
        
        c.user_prof = None
        c.single_use_per_user = True
        self.assertFalse(c.is_valid(user))
    def test_coupon_is_valid(self):

        user = User()
        user.first_name = "justin"
        user.last_name = "gray"
        user.username = "******"
        user.email = "*****@*****.**"
        user.save()

        user2 = User()
        user2.first_name = "sirius"
        user2.last_name = "gray"
        user2.username = "******"
        user2.save()

        club = Club()
        club.name = "test-test"
        club.save()

        c = Coupon()
        c.discount_amount = "10.00"
        c.permanent = True
        c.code = "aaa"
        c.expires = datetime.date.today() + datetime.timedelta(days=1)
        c.club = club
        c.save()

        o = Order()
        o.coupon = c
        o.user_prof = user.get_profile()
        o.save()

        self.assertTrue(c.is_valid(user))
        c.uses_left = 0
        self.assertTrue(c.is_valid(user))

        c.expires = datetime.date.today() - datetime.timedelta(days=1)
        self.assertTrue(c.is_valid(user))

        c.permanent = False
        self.assertFalse(c.is_valid(user))

        c.expires = datetime.date.today() + datetime.timedelta(days=1)
        c.uses_left = 1
        self.assertTrue(c.is_valid(user))
        c.uses_left = 0
        self.assertFalse(c.is_valid(user))

        c.uses_left = 1
        c.user_prof = user.get_profile()
        self.assertFalse(c.is_valid(user2))

        c.user_prof = None
        c.single_use_per_user = True
        self.assertFalse(c.is_valid(user))
Example #3
0
def event_register(request, club_name, season_year, event_name, username=None):
    """register for an event"""
    up = request.user.get_profile()
    e = Event.objects.select_related('season', 'season__club', 'user_profile').\
        get(season__club__safe_name=club_name,
            season__year=season_year,
            safe_name=event_name)
    redirect_target = reverse('registration.views.event',
                              args=[club_name, season_year, event_name])
    form_template = 'registration/event_reg_form.html'

    reg = None
    if username:
        reg = e.regs.get(user_profile__user__username=username)

    show_cars = bool(
        Car.objects.filter(user_profile=up, provisional=False).count())

    class UserRegForm(
            RegForm
    ):  #have to create the form here, since it's specific to a user

        car = CarChoiceField(queryset=Car.objects.filter(user_profile=up,
                                                         provisional=False),
                             required=False,
                             empty_label=None)
        event = ModelChoiceField(queryset=Event.objects.filter(pk=e.pk),
                                 initial=e.pk,
                                 widget=HiddenInput())
        club = ModelChoiceField(queryset=Club.objects.filter(pk=e.club.pk),
                                initial=e.club.pk,
                                widget=HiddenInput())
        user_profile = ModelChoiceField(
            queryset=UserProfile.objects.filter(pk=up.pk),
            initial=up.pk,
            widget=HiddenInput())

        def __init__(self, *args, **kwargs):
            super(UserRegForm, self).__init__(*args, **kwargs)

            if not show_cars:
                self.fields['car'].widget = HiddenInput()

            if reg and reg.paid:  #hide reg if paid
                self.fields['prepay'].widget = HiddenInput()
                self.fields['coupon_code'].widget = HiddenInput()

    if request.method == 'POST':
        form = UserRegForm(request.POST,
                           request.FILES,
                           user=request.user,
                           instance=reg)
        if form.is_valid():
            reg = form.save()
            if form.cleaned_data['prepay']:
                #figure out base reg price (member or not?)
                member = up.is_member(e.club)
                if member:
                    reg.price = e.member_price
                else:
                    reg.price = e.non_member_price

                up = request.user.get_profile()
                #construct order, add reg to order
                order = Order()
                order.user_prof = up
                if form.coupon:
                    order.coupon = form.coupon
                    order.coupon.uses_left -= 1
                order.save()
                reg.order = order
                reg.save()

                # What you want the button to do.
                paypal_dict = {
                    "business": e.club.paypal_email,
                    #'business': '*****@*****.**',
                    "amount": order.calc_total_price(),
                    "item_name":
                    'Registration for %s %s' % (e.club.name, e.name),
                    "invoice": order.pk,
                    "notify_url":
                    request.build_absolute_uri(reverse('paypal-ipn')),
                    "return_url": request.build_absolute_uri(redirect_target),
                    "cancel_return":
                    request.build_absolute_uri(redirect_target),
                }

                if paypal_dict[
                        'amount'] == '0.00':  #don't need to pay if the price is 0
                    order.payment_complete()
                    return HttpResponseRedirect(redirect_target)
                else:
                    paypal_form = PayPalPaymentsForm(initial=paypal_dict)

                    context = {
                        'paypal_form':
                        paypal_form.render(),
                        'price':
                        paypal_dict['amount'],
                        'club':
                        e.club,
                        'order':
                        order,
                        'items': [
                            i.as_leaf_model().cart_name()
                            for i in order.items.all()
                        ]
                    }

                    return render_to_response(
                        'registration/start_pay.html',
                        context,
                        context_instance=RequestContext(request))

            return HttpResponseRedirect(redirect_target)
    else:
        form = UserRegForm(instance=reg)

    context = {
        'event': e,
        'season': e.season,
        'club': e.club,
        'form': form,
    }
    return render_to_response(form_template,
                              context,
                              context_instance=RequestContext(request))