From: Radek Czajka Date: Mon, 15 Jul 2013 09:08:28 +0000 (+0200) Subject: Send funding notifications. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/11acab073dcbe3138f9208229c29220646e7457e Send funding notifications. --- diff --git a/apps/funding/__init__.py b/apps/funding/__init__.py index e69de29bb..9e81e9ec3 100644 --- a/apps/funding/__init__.py +++ b/apps/funding/__init__.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from django.conf import settings as settings +from catalogue.utils import AppSettings + + +class Settings(AppSettings): + """Default settings for funding app.""" + DEFAULT_LANGUAGE = u'pl' + DEFAULT_AMOUNT = 20 + MIN_AMOUNT = 1 + DAYS_NEAR = 2 + + +app_settings = Settings('FUNDING') diff --git a/apps/funding/admin.py b/apps/funding/admin.py index 59c3ac6e3..c8a25d013 100644 --- a/apps/funding/admin.py +++ b/apps/funding/admin.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from django.contrib import admin from .models import Offer, Perk, Funding, Spent diff --git a/apps/funding/forms.py b/apps/funding/forms.py index bb155b98a..64cceb6ad 100644 --- a/apps/funding/forms.py +++ b/apps/funding/forms.py @@ -1,9 +1,13 @@ -from django.conf import settings +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from django import forms from django.utils import formats from django.utils.translation import ugettext_lazy as _, ugettext, get_language from .models import Funding from .widgets import PerksAmountWidget +from . import app_settings class FundingForm(forms.Form): @@ -24,10 +28,10 @@ class FundingForm(forms.Form): self.fields['amount'].widget.form_instance = self def clean_amount(self): - if self.cleaned_data['amount'] < settings.FUNDING_MIN_AMOUNT: - min_amount = settings.FUNDING_MIN_AMOUNT - if isinstance(settings.FUNDING_MIN_AMOUNT, float): - min_amount = formats.number_format(settings.FUNDING_MIN_AMOUNT, 2) + if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT: + min_amount = app_settings.MIN_AMOUNT + if isinstance(min_amount, float): + min_amount = formats.number_format(min_amount, 2) raise forms.ValidationError( ugettext("The minimum amount is %(amount)s PLN.") % { 'amount': min_amount}) diff --git a/apps/funding/management/__init__.py b/apps/funding/management/__init__.py new file mode 100755 index 000000000..e69de29bb diff --git a/apps/funding/management/commands/__init__.py b/apps/funding/management/commands/__init__.py new file mode 100755 index 000000000..e69de29bb diff --git a/apps/funding/management/commands/funding_notify.py b/apps/funding/management/commands/funding_notify.py new file mode 100755 index 000000000..a583b2785 --- /dev/null +++ b/apps/funding/management/commands/funding_notify.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from optparse import make_option +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + option_list = BaseCommand.option_list + ( + make_option('-q', '--quiet', action='store_false', dest='verbose', default=True, + help='Suppress output'), + ) + help = 'Sends relevant funding notifications.' + + def handle(self, **options): + + from datetime import date, timedelta + from funding.models import Offer + from funding import app_settings + + verbose = options['verbose'] + + for offer in Offer.past().filter(notified_end=None): + if verbose: + print 'Notify end:', offer + offer.notify_end() + + current = Offer.current() + if (current is not None and + current.end <= date.today() + timedelta(app_settings.DAYS_NEAR - 1) and + not current.notified_near): + if verbose: + print 'Notify near:', current + current.notify_near() diff --git a/apps/funding/migrations/0017_auto__add_field_offer_notified_near__add_field_offer_notified_end.py b/apps/funding/migrations/0017_auto__add_field_offer_notified_near__add_field_offer_notified_end.py new file mode 100644 index 000000000..4f12e4d9f --- /dev/null +++ b/apps/funding/migrations/0017_auto__add_field_offer_notified_near__add_field_offer_notified_end.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding field 'Offer.notified_near' + db.add_column(u'funding_offer', 'notified_near', + self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True), + keep_default=False) + + # Adding field 'Offer.notified_end' + db.add_column(u'funding_offer', 'notified_end', + self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'Offer.notified_near' + db.delete_column(u'funding_offer', 'notified_near') + + # Deleting field 'Offer.notified_end' + db.delete_column(u'funding_offer', 'notified_end') + + + models = { + 'catalogue.book': { + 'Meta': {'ordering': "('sort_key',)", 'object_name': 'Book'}, + '_related_info': ('jsonfield.fields.JSONField', [], {'null': 'True', 'blank': 'True'}), + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'common_slug': ('django.db.models.fields.SlugField', [], {'max_length': '120'}), + 'cover': ('catalogue.fields.EbookField', [], {'max_length': '100', 'null': 'True', 'format_name': "'cover'", 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'epub_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'epub'", 'blank': 'True'}), + 'extra_info': ('jsonfield.fields.JSONField', [], {'default': "'{}'"}), + 'fb2_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'fb2'", 'blank': 'True'}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'html_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'html'", 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'default': "'pol'", 'max_length': '3', 'db_index': 'True'}), + 'mobi_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'mobi'", 'blank': 'True'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['catalogue.Book']"}), + 'parent_number': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'pdf_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'pdf'", 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'txt_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'txt'", 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'xml_file': ('catalogue.fields.EbookField', [], {'default': "''", 'max_length': '100', 'format_name': "'xml'", 'blank': 'True'}) + }, + u'funding.funding': { + 'Meta': {'ordering': "['-payed_at']", 'object_name': 'Funding'}, + 'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}), + 'email': ('django.db.models.fields.EmailField', [], {'db_index': 'True', 'max_length': '75', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language_code': ('django.db.models.fields.CharField', [], {'max_length': '2', 'null': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '127', 'blank': 'True'}), + 'notifications': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}), + 'notify_key': ('django.db.models.fields.CharField', [], {'max_length': '32'}), + 'offer': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['funding.Offer']"}), + 'payed_at': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}), + 'perks': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['funding.Perk']", 'symmetrical': 'False', 'blank': 'True'}) + }, + u'funding.offer': { + 'Meta': {'ordering': "['-end']", 'object_name': 'Offer'}, + 'author': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'book': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['catalogue.Book']", 'null': 'True', 'blank': 'True'}), + 'cover': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'end': ('django.db.models.fields.DateField', [], {'db_index': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'notified_end': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'notified_near': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'poll': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['polls.Poll']", 'null': 'True', 'on_delete': 'models.SET_NULL', 'blank': 'True'}), + 'redakcja_url': ('django.db.models.fields.URLField', [], {'max_length': '200', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}), + 'start': ('django.db.models.fields.DateField', [], {'db_index': 'True'}), + 'target': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}) + }, + u'funding.perk': { + 'Meta': {'ordering': "['-price']", 'object_name': 'Perk'}, + 'end_date': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'long_name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'offer': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['funding.Offer']", 'null': 'True', 'blank': 'True'}), + 'price': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}) + }, + u'funding.spent': { + 'Meta': {'ordering': "['-timestamp']", 'object_name': 'Spent'}, + 'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}), + 'book': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['catalogue.Book']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'timestamp': ('django.db.models.fields.DateField', [], {}) + }, + u'polls.poll': { + 'Meta': {'object_name': 'Poll'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'open': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'question': ('django.db.models.fields.TextField', [], {}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}) + } + } + + complete_apps = ['funding'] \ No newline at end of file diff --git a/apps/funding/models.py b/apps/funding/models.py index f88659997..3b42ea63d 100644 --- a/apps/funding/models.py +++ b/apps/funding/models.py @@ -15,6 +15,7 @@ from catalogue.models import Book from catalogue.utils import get_random_hash from polls.models import Poll from django.contrib.sites.models import Site +from . import app_settings class Offer(models.Model): @@ -31,7 +32,10 @@ class Offer(models.Model): help_text=_('Published book.')) cover = models.ImageField(_('Cover'), upload_to = 'funding/covers') poll = models.ForeignKey(Poll, help_text = _('Poll'), null = True, blank = True, on_delete = models.SET_NULL) - + + notified_near = models.DateTimeField(_('Near-end notifications sent'), blank=True, null=True) + notified_end = models.DateTimeField(_('End notifications sent'), blank=True, null=True) + def cover_img_tag(self): return u'' % self.cover.url cover_img_tag.short_description = _('Cover preview') @@ -120,7 +124,8 @@ class Offer(models.Model): query_filter=models.Q(offer=self) ) - def notify_end(self): + def notify_end(self, force=False): + if not force and self.notified_end: return assert not self.is_current() self.notify_all( _('The fundraiser has ended!'), @@ -130,8 +135,11 @@ class Offer(models.Model): 'remaining': self.remaining(), 'current': self.current(), }) + self.notified_end = datetime.now() + self.save() - def notify_near(self): + def notify_near(self, force=False): + if not force and self.notified_near: return assert self.is_current() sum_ = self.sum() need = self.target - sum_ @@ -144,6 +152,8 @@ class Offer(models.Model): 'sum': sum_, 'need': need, }) + self.notified_near = datetime.now() + self.save() def notify_published(self): assert self.book is not None @@ -243,7 +253,7 @@ class Funding(models.Model): 'site': Site.objects.get_current(), } context.update(extra_context) - with override(self.language_code or 'pl'): + with override(self.language_code or app_settings.DEFAULT_LANGUAGE): send_mail(subject, render_to_string(template_name, context), getattr(settings, 'CONTACT_EMAIL', 'wolnelektury@nowoczesnapolska.org.pl'), diff --git a/apps/funding/views.py b/apps/funding/views.py index 581a64e2e..0b84a6c52 100644 --- a/apps/funding/views.py +++ b/apps/funding/views.py @@ -4,7 +4,6 @@ # from datetime import date from django.views.decorators.cache import never_cache -from django.conf import settings from django.core.urlresolvers import reverse from django.http import Http404 from django.shortcuts import redirect, get_object_or_404 @@ -12,6 +11,7 @@ from django.views.decorators.csrf import csrf_exempt from django.views.generic import TemplateView, FormView, DetailView, ListView import getpaid.backends.payu from getpaid.models import Payment +from . import app_settings from .forms import FundingForm from .models import Offer, Spent, Funding @@ -92,7 +92,7 @@ class OfferDetailView(FormView): if self.request.method == 'POST': return form_class(self.object, self.request.POST) else: - return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT}) + return form_class(self.object, initial={'amount': app_settings.DEFAULT_AMOUNT}) def get_context_data(self, *args, **kwargs): ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs) diff --git a/wolnelektury/settings/custom.py b/wolnelektury/settings/custom.py index 6cd036a96..a0bab7a64 100644 --- a/wolnelektury/settings/custom.py +++ b/wolnelektury/settings/custom.py @@ -16,6 +16,3 @@ CATALOGUE_CUSTOMPDF_RATE_LIMIT = '1/m' # set to 'new' or 'old' to skip time-consuming test # for TeX morefloats library version LIBRARIAN_PDF_MOREFLOATS = None - -FUNDING_DEFAULT = 20 -FUNDING_MIN_AMOUNT = 1