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.views.decorators.cache import never_cache
 
   6 from django.conf import settings
 
   7 from django.core.urlresolvers import reverse
 
   8 from django.http import Http404
 
   9 from django.shortcuts import redirect, get_object_or_404
 
  10 from django.utils.decorators import method_decorator
 
  11 from django.views.generic import TemplateView, FormView, DetailView, ListView
 
  12 import getpaid.backends.payu
 
  13 from getpaid.models import Payment
 
  14 from .forms import FundingForm
 
  15 from .models import Offer, Spent, Funding
 
  20     for stream, read_date, tag in streams:
 
  21         iterstream = iter(stream)
 
  23             item = next(iterstream)
 
  27             substreams.append([read_date(item), item, iterstream, read_date, tag])
 
  29         i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
 
  30         yield substream[4], substream[1]
 
  32             item = next(substream[2])
 
  36             substream[0:2] = [substream[3](item), item]
 
  39 class WLFundView(TemplateView):
 
  40     template_name = "funding/wlfund.html"
 
  42     def get_context_data(self):
 
  43         def add_total(total, it):
 
  52         ctx = super(WLFundView, self).get_context_data()
 
  54         for o in Offer.objects.all():
 
  55             if o.state() == 'lose':
 
  59             elif o.state() == 'win':
 
  60                 o.wlfund = o.sum() - o.target
 
  63         amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
 
  66         ctx['amount'] = amount
 
  67         ctx['log'] = add_total(amount, mix(
 
  68             (offers, lambda x: x.end, 'offer'),
 
  69             (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
 
  74 class OfferDetailView(FormView):
 
  75     form_class = FundingForm
 
  76     template_name = "funding/offer_detail.html"
 
  77     backend = 'getpaid.backends.payu'
 
  79     def dispatch(self, request, slug=None):
 
  81             self.object = get_object_or_404(Offer.public(), slug=slug)
 
  83             self.object = Offer.current()
 
  84             if self.object is None:
 
  86         return super(OfferDetailView, self).dispatch(request, slug)
 
  88     def get_form(self, form_class):
 
  89         if self.request.method == 'POST':
 
  90             return form_class(self.object, self.request.POST)
 
  92             return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
 
  94     def get_context_data(self, *args, **kwargs):
 
  95         ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
 
  96         ctx['object'] = self.object
 
  97         if self.object.is_current():
 
  98             ctx['funding_no_show_current'] = True
 
 101     def form_valid(self, form):
 
 102         funding = form.save()
 
 103         # Skip getpaid.forms.PaymentMethodForm, go directly to the broker.
 
 104         payment = Payment.create(funding, self.backend)
 
 105         gateway_url = payment.get_processor()(payment).get_gateway_url(self.request)
 
 106         payment.change_status('in_progress')
 
 107         return redirect(gateway_url)
 
 110 class OfferListView(ListView):
 
 111     queryset = Offer.public()
 
 113     def get_context_data(self, *args, **kwargs):
 
 114         ctx = super(OfferListView, self).get_context_data(*args, **kwargs)
 
 115         ctx['funding_no_show_current'] = True
 
 119 class ThanksView(TemplateView):
 
 120     template_name = "funding/thanks.html"
 
 122     def get_context_data(self, *args, **kwargs):
 
 123         ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
 
 124         ctx['offer'] = Offer.current()
 
 125         ctx['funding_no_show_current'] = True
 
 128 class NoThanksView(TemplateView):
 
 129     template_name = "funding/no_thanks.html"