Exemple #1
0
def claim_voucher(request, human_token=None):
    """
    /vouchers/claim/
    /vouchers/claim/<human_token>
    """
    human_token = slugify(human_token or '')
    voucher_name = ''
    is_claimed = False
    form = None
    voucher = None

    if request.method == 'GET' and human_token:
        try:
            voucher = Voucher.objects.get(human_token=human_token)
        except Voucher.DoesNotExist:
            raise Http404
        form = get_voucher_form(voucher)()
        if voucher.user and voucher.user != request.user:
            raise Http404()
        elif not voucher.user:
            voucher.user = request.user
            voucher.save()
            voucher_name = get_voucher_form_name(voucher)
            messages.success(request, "The voucher %s has been assigned to you, "
                                      "fill in the form to claim your voucher!" % voucher_name)
        is_claimed = voucher.is_claimed()

    elif request.method == 'GET' and not human_token:
        form = TokenForm()
    elif request.method == 'POST':
        token_form = TokenForm(request.POST)
        if token_form.is_valid():
            human_token = slugify(token_form.cleaned_data['token'])
            new_url = reverse('claim_voucher', args=[human_token])
            return redirect(new_url)

        if 'submit' in request.POST and 'Send' in request.POST['submit'] and human_token:
            human_token = slugify(human_token)
            try:
                voucher = Voucher.objects.get(human_token=human_token, user=request.user)
            except Voucher.DoesNotExist:
                raise Http404
            voucher_klass_form = get_voucher_form(voucher)
            voucher_name = get_voucher_form_name(voucher)
            form = voucher_klass_form(request.POST)
            is_claimed = voucher.is_claimed()
            if form.is_valid() and not is_claimed:
                try:
                    voucher.claim_voucher(request.user, human_token, form.cleaned_data)
                    messages.success(request, 'Voucher information has been sent!')
                except ValidationError as error:
                    messages.error(request, '\n'.join(error.messages))
    template = get_voucher_template(voucher)
    name = template.name if hasattr(template, 'name') else template.template.name
    return render_to_response(name,
                              RequestContext(request, {'voucher_name': voucher_name,
                                                       'token': human_token,
                                                       'is_claimed': is_claimed,
                                                       'form': form,
                                                       'voucher': voucher}))
Exemple #2
0
    def claim_voucher(self, user, human_token, voucher_info=None):
        if self.is_claimed():
            raise ValidationError("Voucher already claimed by %s" % self.user)

        if not slugify(self.voucher) in VOUCHERS:
            raise ValidationError("Invalid voucher choice.")

        voucher_form = get_voucher_form(self)(voucher_info or {})
        if not voucher_form.is_valid():
            raise ValidationError("Invalid voucher info.")

        if human_token != self.human_token:
            raise ValidationError("Cannot claim voucher, invalid token.")

        if not isinstance(user, User):
            user = get_object_or_404(User, username=user)

        if not self.user:
            self.user = user
        elif self.user and user != self.user:
            raise ValidationError("Cannot claim voucher, the user assigned don't match")

        self.voucher_info = voucher_form.cleaned_data
        self.claimed_date = datetime.datetime.now()
        self.activated = False
        self.notified = False
        self.save()
Exemple #3
0
 def get_voucher_info_template(self):
     if self.voucher_info and self.voucher:
         voucher_form = get_voucher_form(self)
         for field in voucher_form.base_fields:
             voucher_form.base_fields[field].widget.attrs['readonly'] = True
         voucher_form = voucher_form(self.voucher_info)
         if voucher_form.is_valid():
             return voucher_form.as_table()
     return ''
Exemple #4
0
 def get_voucher_form(self, voucher_info=None):
     voucher_info = voucher_info or self.voucher_info or {}
     if self.voucher and self.voucher in VOUCHERS:
         form = get_voucher_form(self)(voucher_info)
         form.is_valid()
         return form