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.
5 from django.core.urlresolvers import reverse
6 from django.db import models
7 from django.utils.translation import ugettext_lazy as _, ugettext as __
8 from datetime import date, datetime
11 class Offer(models.Model):
12 author = models.CharField(_('author'), max_length=255)
13 title = models.CharField(_('title'), max_length=255)
14 slug = models.SlugField(_('slug'))
15 book_url = models.URLField(_('book URL'), blank=True)
16 redakcja_url = models.URLField(_('redakcja URL'), blank=True)
17 target = models.DecimalField(_('target'), decimal_places=2, max_digits=10)
18 start = models.DateField(_('start'))
19 end = models.DateField(_('end'))
22 verbose_name = _('offer')
23 verbose_name_plural = _('offers')
26 def __unicode__(self):
27 return u"%s – %s" % (self.author, self.title)
29 def get_absolute_url(self):
30 return reverse('funding_offer', args=[self.slug])
35 objects = cls.objects.filter(start__lte=today, end__gte=today)
44 return cls.objects.filter(start__lte=today)
46 def get_perks(self, amount=None):
47 perks = Perk.objects.filter(
48 models.Q(offer=self) | models.Q(offer=None)
50 if amount is not None:
51 perks = perks.filter(price__lte=amount)
54 def fund(self, name, email, amount):
55 funding = self.funding_set.create(
56 name=name, email=email, amount=amount,
57 payed_at=datetime.now())
58 funding.perks = self.get_perks(amount)
62 return self.funding_set.aggregate(s=models.Sum('amount'))['s'] or 0
65 if self.sum() >= self.target:
67 elif self.start <= date.today() <= self.end:
73 class Perk(models.Model):
74 offer = models.ForeignKey(Offer, verbose_name=_('offer'), null=True, blank=True)
75 price = models.DecimalField(_('price'), decimal_places=2, max_digits=10)
76 name = models.CharField(_('name'), max_length=255)
77 description = models.TextField(_('description'), blank=True)
80 verbose_name = _('perk')
81 verbose_name_plural = _('perks')
84 def __unicode__(self):
85 return "%s (%s%s)" % (self.name, self.price, u" for %s" % self.offer if self.offer else "")
88 class Funding(models.Model):
89 offer = models.ForeignKey(Offer, verbose_name=_('offer'))
90 name = models.CharField(_('name'), max_length=127)
91 email = models.EmailField(_('email'))
92 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
93 payed_at = models.DateTimeField(_('payed_at'))
94 perks = models.ManyToManyField(Perk, verbose_name=_('perks'), blank=True)
97 verbose_name = _('funding')
98 verbose_name_plural = _('fundings')
99 ordering = ['-payed_at']
101 def __unicode__(self):
102 return "%s payed %s for %s" % (self.name, self.amount, self.offer)