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.paginator import Paginator, InvalidPage
6 from django.core.urlresolvers import reverse
7 from django.http import Http404
8 from django.shortcuts import get_object_or_404, redirect, render
9 from django.views.decorators.csrf import csrf_exempt
10 from django.views.generic import TemplateView, FormView, ListView
11 from getpaid.models import Payment
12 from ssify import ssi_included
13 from ssify.utils import ssi_cache_control
14 from . import app_settings
15 from .forms import FundingForm
16 from .models import Offer, Spent, Funding
21 for stream, read_date, tag in streams:
22 iterstream = iter(stream)
24 item = next(iterstream)
28 substreams.append([read_date(item), item, iterstream, read_date, tag])
30 i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
31 yield substream[4], substream[1]
33 item = next(substream[2])
37 substream[0:2] = [substream[3](item), item]
40 class WLFundView(TemplateView):
41 template_name = "funding/wlfund.html"
43 def get_context_data(self):
44 def add_total(total, it):
53 ctx = super(WLFundView, self).get_context_data()
56 for o in Offer.past():
58 o.wlfund = o.sum() - o.target
65 amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
67 ctx['amount'] = amount
68 ctx['log'] = add_total(amount, mix(
69 (offers, lambda x: x.end, 'offer'),
70 (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'),
75 class OfferDetailView(FormView):
76 form_class = FundingForm
77 template_name = "funding/offer_detail.html"
78 backend = 'getpaid.backends.payu'
81 def dispatch(self, request, slug=None):
82 if getattr(self, 'object', None) is None:
84 self.object = get_object_or_404(Offer.public(), slug=slug)
86 self.object = Offer.current()
87 if self.object is None:
89 return super(OfferDetailView, self).dispatch(request, slug)
91 def get_form(self, form_class):
92 if self.request.method == 'POST':
93 return form_class(self.object, self.request.POST)
95 return form_class(self.object, initial={'amount': app_settings.DEFAULT_AMOUNT})
97 def get_context_data(self, *args, **kwargs):
98 ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
99 ctx['object'] = self.object
100 ctx['page'] = self.request.GET.get('page', 1)
101 if self.object.is_current():
102 ctx['funding_no_show_current'] = True
105 def form_valid(self, form):
106 funding = form.save()
107 # Skip getpaid.forms.PaymentMethodForm, go directly to the broker.
108 payment = Payment.create(funding, self.backend)
109 gateway_url_tuple = payment.get_processor()(payment).get_gateway_url(self.request)
110 payment.change_status('in_progress')
111 return redirect(gateway_url_tuple[0])
114 class CurrentView(OfferDetailView):
116 def dispatch(self, request, slug=None):
117 self.object = Offer.current()
118 if self.object is None:
119 return redirect(reverse('funding'))
120 elif slug != self.object.slug:
121 return redirect(reverse('funding_current', args=[self.object.slug]))
122 return super(CurrentView, self).dispatch(request, slug)
125 class OfferListView(ListView):
126 queryset = Offer.public()
128 def get_context_data(self, *args, **kwargs):
129 ctx = super(OfferListView, self).get_context_data(*args, **kwargs)
130 ctx['funding_no_show_current'] = True
134 class ThanksView(TemplateView):
135 template_name = "funding/thanks.html"
137 def get_context_data(self, *args, **kwargs):
138 ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
139 ctx['offer'] = Offer.current()
140 ctx['funding_no_show_current'] = True
144 class NoThanksView(TemplateView):
145 template_name = "funding/no_thanks.html"
148 class DisableNotifications(TemplateView):
149 template_name = "funding/disable_notifications.html"
152 def dispatch(self, request):
153 self.object = get_object_or_404(Funding,
154 email=request.GET.get('email'), notify_key=request.GET.get('key'))
155 return super(DisableNotifications, self).dispatch(request)
157 def post(self, *args, **kwargs):
158 self.object.disable_notifications()
159 return redirect(self.request.get_full_path())
162 def offer_bar(request, pk, link=False, closeable=False, show_title=True, show_title_calling=True, add_class=""):
163 offer = get_object_or_404(Offer, pk=pk)
164 offer_sum = offer.sum()
166 return render(request, "funding/includes/funding.html", {
169 'is_current': offer.is_current(),
170 'is_win': offer_sum >= offer.target,
171 'missing': offer.target - offer_sum,
172 'percentage': 100 * offer_sum / offer.target,
174 'closeable': closeable,
175 'show_title': show_title,
176 'show_title_calling': show_title_calling,
177 'add_class': add_class,
181 @ssi_included(patch_response=[ssi_cache_control(must_revalidate=True)])
182 def top_bar(request, pk):
183 return offer_bar(request, pk,
184 link=True, closeable=True, add_class="funding-top-header")
187 @ssi_included(patch_response=[ssi_cache_control(must_revalidate=True)])
188 def list_bar(request, pk):
189 return offer_bar(request, pk,
190 link=True, show_title_calling=False)
193 @ssi_included(patch_response=[ssi_cache_control(must_revalidate=True)])
194 def detail_bar(request, pk):
195 return offer_bar(request, pk,
199 @ssi_included(patch_response=[ssi_cache_control(must_revalidate=True)])
200 def offer_status(request, pk):
201 offer = get_object_or_404(Offer, pk=pk)
202 return render(request, "funding/includes/offer_status.html", {
207 @ssi_included(patch_response=[ssi_cache_control(must_revalidate=True)])
208 def offer_status_more(request, pk):
209 offer = get_object_or_404(Offer, pk=pk)
210 return render(request, "funding/includes/offer_status_more.html", {
215 @ssi_included(patch_response=[ssi_cache_control(must_revalidate=True)])
216 def offer_fundings(request, pk, page):
217 offer = get_object_or_404(Offer, pk=pk)
218 fundings = offer.funding_payed()
219 paginator = Paginator(fundings, 10, 2)
221 page_obj = paginator.page(int(page))
224 return render(request, "funding/includes/fundings.html", {
225 "paginator": paginator,
226 "page_obj": page_obj,
227 "fundings": page_obj.object_list,