Added: Fundusz, dummy payments.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 26 Mar 2013 16:10:46 +0000 (17:10 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 26 Mar 2013 16:10:46 +0000 (17:10 +0100)
14 files changed:
.gitignore
apps/funding/admin.py
apps/funding/forms.py [new file with mode: 0755]
apps/funding/migrations/0002_auto__add_spent__del_field_offer_book_url__add_field_offer_book.py [new file with mode: 0644]
apps/funding/models.py
apps/funding/static/funding/.sass-cache/9a22640771b7b9d791c15ce50176b00a63a474df/funding.scssc [deleted file]
apps/funding/static/funding/funding.css
apps/funding/static/funding/funding.scss
apps/funding/templates/funding/offer_detail.html
apps/funding/templates/funding/offer_list.html
apps/funding/templates/funding/thanks.html [new file with mode: 0755]
apps/funding/templates/funding/wlfund.html
apps/funding/urls.py
apps/funding/views.py

index c3e3e48..6714e23 100644 (file)
@@ -5,6 +5,7 @@ dev.sqlite
 *~
 *.orig
 *.log
+.sass-cache
 
 # Compress output
 /static
index 132c863..7fbb4c4 100755 (executable)
@@ -1,6 +1,7 @@
 from django.contrib import admin
-from .models import Offer, Perk, Funding
+from .models import Offer, Perk, Funding, Spent
 
+admin.site.register(Spent)
 admin.site.register(Offer)
 admin.site.register(Perk)
 admin.site.register(Funding)
diff --git a/apps/funding/forms.py b/apps/funding/forms.py
new file mode 100755 (executable)
index 0000000..eb75fb6
--- /dev/null
@@ -0,0 +1,30 @@
+from django import forms
+from .models import Offer
+
+
+class DummyForm(forms.Form):
+    amount = forms.DecimalField()
+    name = forms.CharField()
+    email = forms.EmailField()
+
+    def __init__(self, offer, *args, **kwargs):
+        self.offer = offer
+        super(DummyForm, self).__init__(*args, **kwargs)
+
+    def clean_amount(self):
+        if self.cleaned_data['amount'] <= 0:
+            raise forms.ValidationError("A!")
+        return self.cleaned_data['amount']
+
+    def clean(self):
+        if self.offer != Offer.current():
+            raise forms.ValidationError("B!")
+        return self.cleaned_data
+
+    def save(self):
+        print self.cleaned_data
+        return self.offer.fund(
+            name=self.cleaned_data['name'],
+            email=self.cleaned_data['email'],
+            amount=self.cleaned_data['amount'])
+
diff --git a/apps/funding/migrations/0002_auto__add_spent__del_field_offer_book_url__add_field_offer_book.py b/apps/funding/migrations/0002_auto__add_spent__del_field_offer_book_url__add_field_offer_book.py
new file mode 100644 (file)
index 0000000..1c03bdb
--- /dev/null
@@ -0,0 +1,108 @@
+# -*- 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 model 'Spent'
+        db.create_table('funding_spent', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('amount', self.gf('django.db.models.fields.DecimalField')(max_digits=10, decimal_places=2)),
+            ('timestamp', self.gf('django.db.models.fields.DateField')()),
+            ('book', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['catalogue.Book'])),
+        ))
+        db.send_create_signal('funding', ['Spent'])
+
+        # Deleting field 'Offer.book_url'
+        db.delete_column('funding_offer', 'book_url')
+
+        # Adding field 'Offer.book'
+        db.add_column('funding_offer', 'book',
+                      self.gf('django.db.models.fields.related.ForeignKey')(to=orm['catalogue.Book'], null=True, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting model 'Spent'
+        db.delete_table('funding_spent')
+
+        # Adding field 'Offer.book_url'
+        db.add_column('funding_offer', 'book_url',
+                      self.gf('django.db.models.fields.URLField')(default='', max_length=200, blank=True),
+                      keep_default=False)
+
+        # Deleting field 'Offer.book'
+        db.delete_column('funding_offer', 'book_id')
+
+
+    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'}),
+            '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'})
+        },
+        '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', [], {'max_length': '75'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '127'}),
+            'offer': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['funding.Offer']"}),
+            'payed_at': ('django.db.models.fields.DateTimeField', [], {}),
+            'perks': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['funding.Perk']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        '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'}),
+            'end': ('django.db.models.fields.DateField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': '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', [], {}),
+            'target': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+        },
+        'funding.perk': {
+            'Meta': {'ordering': "['-price']", 'object_name': 'Perk'},
+            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'offer': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['funding.Offer']", 'null': 'True', 'blank': 'True'}),
+            'price': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'})
+        },
+        'funding.spent': {
+            'Meta': {'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']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'timestamp': ('django.db.models.fields.DateField', [], {})
+        }
+    }
+
+    complete_apps = ['funding']
\ No newline at end of file
index a088a07..8c1ee9a 100644 (file)
@@ -6,13 +6,14 @@ from django.core.urlresolvers import reverse
 from django.db import models
 from django.utils.translation import ugettext_lazy as _, ugettext as __
 from datetime import date, datetime
+from catalogue.models import Book
 
 
 class Offer(models.Model):
     author = models.CharField(_('author'), max_length=255)
     title = models.CharField(_('title'), max_length=255)
     slug = models.SlugField(_('slug'))
-    book_url = models.URLField(_('book URL'), blank=True)
+    book = models.ForeignKey(Book, null=True, blank=True)
     redakcja_url = models.URLField(_('redakcja URL'), blank=True)
     target = models.DecimalField(_('target'), decimal_places=2, max_digits=10)
     start = models.DateField(_('start'))
@@ -100,3 +101,12 @@ class Funding(models.Model):
 
     def __unicode__(self):
         return "%s payed %s for %s" % (self.name, self.amount, self.offer)
+
+
+class Spent(models.Model):
+    amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
+    timestamp = models.DateField(_('when'))
+    book = models.ForeignKey(Book)
+
+    def __unicode__(self):
+        return u"Spent: %s" % unicode(self.book)
diff --git a/apps/funding/static/funding/.sass-cache/9a22640771b7b9d791c15ce50176b00a63a474df/funding.scssc b/apps/funding/static/funding/.sass-cache/9a22640771b7b9d791c15ce50176b00a63a474df/funding.scssc
deleted file mode 100644 (file)
index eea6a03..0000000
Binary files a/apps/funding/static/funding/.sass-cache/9a22640771b7b9d791c15ce50176b00a63a474df/funding.scssc and /dev/null differ
index 7ed53e1..0b3f563 100755 (executable)
@@ -1,17 +1,27 @@
-.standalone-funding {
-  border: 1px solid black; }
-
 .funding {
   font-size: 1.5em;
   padding: 0;
   padding: .5em 1em;
   background-image: url(/static/img/green-pixel.png);
-  background-repeat: repeat-y; }
+  background-repeat: repeat-y;
+  background-color: rgba(13, 126, 133, 0.2); }
   .funding a {
+    display: block;
     color: black; }
 
-.funding-plus {
-  background: rgba(13, 126, 133, 0.2); }
-
-.funding-minus {
-  background: rgba(255, 0, 0, 0.2); }
+.wlfund {
+  width: 100%;
+  border-collapse: collapse; }
+  .wlfund td {
+    padding: 0 0 1em 0;
+    text-align: center; }
+  .wlfund td:last-child {
+    text-align: right; }
+  .wlfund td div {
+    padding: 1em;
+    box-shadow: 0 2px 0 #DDDDDD; }
+  .wlfund .funding-plus td div {
+    background: rgba(13, 126, 133, 0.2); }
+  .wlfund .funding-minus td div {
+    bbackground: rgba(255, 0, 0, 0.2);
+    background: white; }
index 6268f48..8de3ef8 100755 (executable)
@@ -1,23 +1,41 @@
-.standalone-funding {
-    border: 1px solid black;
-}
-
 .funding {
     font-size: 1.5em;
     padding: 0;
     padding: .5em 1em;
     background-image: url(/static/img/green-pixel.png);
     background-repeat: repeat-y;
+    background-color: fade-out(#0D7E85, .8);
 
     a {
+        display: block;
         color: black;
     }
 }
 
-.funding-plus {
-    background: fade-out(#0D7E85, .8);
-}
+.wlfund {
+    width: 100%;
+    border-collapse: collapse;
+
+    td {
+        padding: 0 0 1em 0;
+        text-align: center;
+    }
+
+    td:last-child {
+        text-align: right;
+    }
+
+    td div {
+        padding: 1em;
+        box-shadow: 0 2px 0 #DDDDDD;
+    }
 
-.funding-minus {
-    background: fade-out(red, .8);
+    .funding-plus td div {
+        background: fade-out(#0D7E85, .8);
+    }
+
+    .funding-minus td div {
+        bbackground: fade-out(red, .8);
+        background: white;
+    }
 }
index c7aa639..3f49b88 100755 (executable)
@@ -1,5 +1,7 @@
 {% extends "base.html" %}
+{% load url from future %}
 {% load funding_tags %}
+{% load uni_form_tags %}
 
 {% block titleextra %}{{ object }}{% endblock %}
 
 <h1>{{ object }}</h1>
 <div class="white-box normal-text">
 
-{% funding object add_class="standalone-funding" %}
+{% funding object %}
 
 {% if object.state == 'running' %}
 
 
 <h3>Wpłać:</h3>
-<form>
+<form action="" method="post">
+    {% csrf_token %}
+    {{ form|as_uni_form }}
+<!--
     {% for perk in object.get_perks %}
         <br/><label><input type="radio" name="amount" value="{{ perk.price }}" /> {{perk.price}} zł: {{perk.name}}</label>
     {% endfor %}
     <br/><label><input type="radio" name="amount" value="" /> Dowolną kwotę:</label>
         <input name="amount" size="5" /> zł
     </label>
-
-
+-->
     <br/><button type="submit">Wpłać!</button>
 </form>
 {% endif %}
+
+<p><a href="{% url 'funding' %}">Zobacz wszystkie zbiórki.</a></p>
 </div>
 
 
index 926d1d3..6114e37 100755 (executable)
@@ -1,4 +1,5 @@
 {% extends "base.html" %}
+{% load url from future %}
 {% load funding_tags %}
 
 {% block titleextra %}Funding{% endblock %}
@@ -19,7 +20,7 @@
             {% endif %}
         {% elif funding.state == "lose" %}
             <p>Cel finansowania nie został osiągnięty. Zgromadzone środki
-            zostały przekazane na Fundusz Wolnych Lektur.</p>
+            zostały przekazane na <a href="{% url 'funding_wlfund' %}">Fundusz Wolnych Lektur</a>.</p>
         {% endif %}
     </li>
 {% endfor %}
diff --git a/apps/funding/templates/funding/thanks.html b/apps/funding/templates/funding/thanks.html
new file mode 100755 (executable)
index 0000000..baf2275
--- /dev/null
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+{% load funding_tags %}
+
+{% block titleextra %}Dziękujemy!{% endblock %}
+
+{% block body %}
+
+<h1>Dziękujemy!</h1>
+<div class="white-box normal-text">
+
+Dziękujemy za wsparcie!
+
+<p><a href="{{ object.get_absolute_url }}">Wróć do strony aktualnej zbiórki.</a></p>
+</div>
+
+
+{% endblock %}
index eb33257..5f3ca65 100755 (executable)
@@ -5,16 +5,35 @@
 {% block body %}
 
 <h1>Fundusz Wolnych Lektur</h1>
-<p>Suma: {{ amount }}</p>
-
-<ul class="plain">
-    {% for entry in log %}
-    <li class="white-box normal-text funding-plus">
-        {{ entry.end }} +{{ entry.sum }}
-        <span style="float:right">Częściowa zbiórka na: {{ entry }}</span>
-    </li>
+
+
+<table class="normal-text wlfund">
+
+    <tr class=""><td><div>Bilans:</div></td><td><div>{{ amount }}</div></td><td colspan="2"><div>&nbsp;</div></td></tr>
+
+
+
+    {% for tag, entry in log %}
+    {% if tag == 'spent' %}
+        <tr class="funding-minus">
+            <td><div>{{ entry.timestamp }}</div></td>
+            <td><div>{{ entry.total }}</div></td>
+            <td><div>-{{ entry.amount }}&nbsp;zł</div></td>
+            <td><div>Książka: <a href="{{ entry.book.get_absolute_url }}">
+                {{ entry.book }}</a></div></td>
+        </tr>
+    {% else %}
+        <tr class="funding-plus">
+            <td><div>{{ entry.end }}</div></td>
+            <td><div>{{ entry.total }}</div></td>
+            <td><div>+{{ entry.sum }}&nbsp;zł</div></td>
+            <td><div>Pieniądze z częściowej zbiórki: 
+                <a href="{{ entry.get_absolute_url }}">
+                {{ entry }}</a></div></td>
+        </tr>
+    {% endif %}
     {% endfor %}
-</ul>
+</table>
 
 
 {% endblock %}
index c712b5e..dc79f31 100755 (executable)
@@ -3,14 +3,15 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf.urls import patterns, url
-from django.views.generic import DetailView, ListView, FormView
+from django.views.generic import ListView, FormView, TemplateView
 
 from .models import Offer
-from .views import WLFundView
+from .views import WLFundView, OfferDetailView, ThanksView
 
 
 urlpatterns = patterns('',
     url(r'^$', ListView.as_view(queryset=Offer.public()), name='funding'),
-    url(r'lektura/^(?P<slug>[^/]+)/$', DetailView.as_view(queryset=Offer.public()), name='funding_offer'),
-    url(r'fundusz/$', WLFundView.as_view(), name='funding_wlfund'),
+    url(r'^lektura/(?P<slug>[^/]+)/$', OfferDetailView.as_view(), name='funding_offer'),
+    url(r'^dziekujemy/$', ThanksView.as_view(), name='funding_thanks'),
+    url(r'^fundusz/$', WLFundView.as_view(), name='funding_wlfund'),
 )
index dab996a..05bf398 100644 (file)
@@ -1,21 +1,24 @@
 # Create your views here.
-from django.views.generic import TemplateView
-from .models import Offer
+from django.core.urlresolvers import reverse
+from django.shortcuts import redirect, get_object_or_404
+from django.views.generic import TemplateView, FormView, DetailView
+from .forms import DummyForm
+from .models import Offer, Spent
 
 
 def mix(*streams):
     substreams = []
-    for stream, read_date in streams:
+    for stream, read_date, tag in streams:
         iterstream = iter(stream)
         try:
             item = next(iterstream)
         except StopIteration:
             pass
         else:
-            substreams.append([read_date(item), item, iterstream, read_date])
+            substreams.append([read_date(item), item, iterstream, read_date, tag])
     while substreams:
         i, substream = max(enumerate(substreams), key=lambda x: x[0])
-        yield substream[1]
+        yield substream[4], substream[1]
         try:
             item = next(substream[2])
         except StopIteration:
@@ -28,12 +31,56 @@ class WLFundView(TemplateView):
     template_name = "funding/wlfund.html"
 
     def get_context_data(self):
+        def add_total(total, it):
+            for tag, e in it:
+                e.total = total
+                if tag == 'spent':
+                    total += e.amount
+                else:
+                    total -= e.sum()
+                yield tag, e
+
         ctx = super(WLFundView, self).get_context_data()
         offers = [o for o in Offer.objects.all() if o.state() == 'lose' and o.sum()]
-        amount = sum(o.sum() for o in offers)
+        amount = sum(o.sum() for o in offers) - sum(o.amount for o in Spent.objects.all())
         print offers
 
-        #offers = (o for o in Offer.objects.all() if o.state() == 'lose' and o.sum())
         ctx['amount'] = amount
-        ctx['log'] = mix((offers, lambda x: x.end))
+        ctx['log'] = add_total(amount, mix(
+            (offers, lambda x: x.end, 'offer'),
+            (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
+        ))
+        return ctx
+
+
+class OfferDetailView(FormView):
+    form_class = DummyForm
+    template_name = "funding/offer_detail.html"
+
+    def dispatch(self, request, slug):
+        self.object = get_object_or_404(Offer.public(), slug=slug)
+        return super(OfferDetailView, self).dispatch(request, slug)
+
+    def get_form(self, form_class):
+        if self.request.method == 'POST':
+            return form_class(self.object, self.request.POST)
+        else:
+            return form_class(self.object)
+
+    def get_context_data(self, *args, **kwargs):
+        ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
+        ctx['object'] = self.object
+        return ctx
+
+    def form_valid(self, form):
+        form.save()
+        return redirect(reverse("funding_thanks"))
+
+
+class ThanksView(TemplateView):
+    template_name = "funding/thanks.html"
+
+    def get_context_data(self, *args, **kwargs):
+        ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
+        ctx['object'] = Offer.current()
         return ctx