Librarian with fixed epub thank-you note
[wolnelektury.git] / apps / funding / widgets.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from decimal import Decimal
6 from django.conf import settings
7 from django import forms
8 from django.template.loader import render_to_string
9
10
11 class PerksAmountWidget(forms.Textarea):
12     def perks_input_name(self, name):
13         return "_%s_perk" % name
14
15     def render(self, name, value, attrs=None):
16         try:
17             value = Decimal(value)
18         except:
19             pass
20         perks = list(self.form_instance.offer.get_perks())
21         perk_chosen = False
22         for perk in perks:
23             if perk.price == value:
24                 perk.chosen = True
25                 perk_chosen = True
26                 break
27
28         return render_to_string("funding/widgets/amount.html", {
29                 "perks": perks,
30                 "name": name,
31                 "perks_input_name": self.perks_input_name(name),
32                 "perk_chosen": perk_chosen,
33                 "value": value,
34                 "attrs": attrs,
35             })
36
37     def value_from_datadict(self, data, files, name):
38         num_str = data.get(self.perks_input_name(name)) or data[name]
39         return num_str.replace(',', '.')
40             
41