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
9 from catalogue.models import Book
12 class Offer(models.Model):
13 author = models.CharField(_('author'), max_length=255)
14 title = models.CharField(_('title'), max_length=255)
15 slug = models.SlugField(_('slug'))
16 book = models.ForeignKey(Book, null=True, blank=True)
17 redakcja_url = models.URLField(_('redakcja URL'), blank=True)
18 target = models.DecimalField(_('target'), decimal_places=2, max_digits=10)
19 start = models.DateField(_('start'))
20 end = models.DateField(_('end'))
23 verbose_name = _('offer')
24 verbose_name_plural = _('offers')
27 def __unicode__(self):
28 return u"%s – %s" % (self.author, self.title)
30 def get_absolute_url(self):
31 return reverse('funding_offer', args=[self.slug])
36 objects = cls.objects.filter(start__lte=today, end__gte=today)
45 return cls.objects.filter(start__lte=today)
47 def get_perks(self, amount=None):
48 perks = Perk.objects.filter(
49 models.Q(offer=self) | models.Q(offer=None)
51 if amount is not None:
52 perks = perks.filter(price__lte=amount)
55 def fund(self, name, email, amount):
56 funding = self.funding_set.create(
57 name=name, email=email, amount=amount,
58 payed_at=datetime.now())
59 funding.perks = self.get_perks(amount)
63 return self.funding_set.aggregate(s=models.Sum('amount'))['s'] or 0
66 if self.sum() >= self.target:
68 elif self.start <= date.today() <= self.end:
74 class Perk(models.Model):
75 offer = models.ForeignKey(Offer, verbose_name=_('offer'), null=True, blank=True)
76 price = models.DecimalField(_('price'), decimal_places=2, max_digits=10)
77 name = models.CharField(_('name'), max_length=255)
78 description = models.TextField(_('description'), blank=True)
81 verbose_name = _('perk')
82 verbose_name_plural = _('perks')
85 def __unicode__(self):
86 return "%s (%s%s)" % (self.name, self.price, u" for %s" % self.offer if self.offer else "")
89 class Funding(models.Model):
90 offer = models.ForeignKey(Offer, verbose_name=_('offer'))
91 name = models.CharField(_('name'), max_length=127)
92 email = models.EmailField(_('email'))
93 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
94 payed_at = models.DateTimeField(_('payed_at'))
95 perks = models.ManyToManyField(Perk, verbose_name=_('perks'), blank=True)
98 verbose_name = _('funding')
99 verbose_name_plural = _('fundings')
100 ordering = ['-payed_at']
102 def __unicode__(self):
103 return "%s payed %s for %s" % (self.name, self.amount, self.offer)
106 class Spent(models.Model):
107 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
108 timestamp = models.DateField(_('when'))
109 book = models.ForeignKey(Book)
111 def __unicode__(self):
112 return u"Spent: %s" % unicode(self.book)