]> git.ipfire.org Git - ipfire.org.git/blob - src/web/handlers.py
Move download handlers into new module
[ipfire.org.git] / src / web / handlers.py
1 #!/usr/bin/python
2
3 #import httplib
4 import iso3166
5 #import logging
6 #import markdown2
7 import operator
8 import os
9 #import random
10 #import re
11 #import socket
12 #import time
13 #import tornado.database
14 #import tornado.locale
15 import tornado.gen
16 import tornado.web
17 #import unicodedata
18
19 from handlers_accounts import *
20 from handlers_admin import *
21 from handlers_base import *
22 from handlers_boot import *
23 from handlers_fireinfo import *
24 from handlers_iuse import *
25 from handlers_mirrors import *
26 from handlers_nopaste import *
27 from handlers_talk import *
28
29 class LangCompatHandler(BaseHandler):
30 """
31 Redirect links in the old format to current site:
32
33 E.g. /en/index -> /index
34 """
35 def get(self, lang, page):
36 self.redirect("/%s" % page)
37
38
39 class IndexHandler(BaseHandler):
40 """
41 This handler displays the welcome page.
42 """
43 def get(self):
44 # Get the latest release.
45 latest_release = self.releases.get_latest()
46
47 # Get a list of the most recent news items and put them on the page.
48 posts = self.backend.blog.get_newest(limit=5)
49
50 return self.render("index.html", posts=posts,
51 latest_release=latest_release,
52 fireinfo_country=self.fireinfo.get_random_country_penetration())
53
54
55 class DonateHandler(BaseHandler):
56 def get(self):
57 location = self.get_remote_location()
58
59 if location:
60 country = location.country
61 else:
62 country = None
63
64 # Get defaults
65 amount = self.get_argument_int("amount", None)
66 currency = self.get_argument("currency", None)
67 frequency = self.get_argument("frequency", None)
68
69 # Set default currency
70 if not currency in ("EUR", "USD"):
71 currency = "EUR"
72
73 # Default to USD for the US only
74 if country == "US":
75 currency = "USD"
76
77 # Set default frequency
78 if not frequency in ("one-time", "monthly"):
79 frequency = "one-time"
80
81 self.render("donate.html", countries=iso3166.countries,
82 country=country, amount=amount, currency=currency, frequency=frequency)
83
84 @tornado.gen.coroutine
85 def post(self):
86 amount = self.get_argument("amount")
87 currency = self.get_argument("currency", "EUR")
88 frequency = self.get_argument("frequency")
89
90 # Get form inputs
91 args = {
92 "amount" : amount,
93 "currency" : currency,
94
95 # Is this a recurring donation?
96 "recurring" : frequency == "monthly",
97
98 # Address
99 "email" : self.get_argument("email"),
100 "title" : self.get_argument("title"),
101 "first_name" : self.get_argument("first_name"),
102 "last_name" : self.get_argument("last_name"),
103 "company_name" : self.get_argument("company_name", None),
104 "street1" : self.get_argument("street1"),
105 "street2" : self.get_argument("street2", None),
106 "post_code" : self.get_argument("post_code"),
107 "city" : self.get_argument("city"),
108 "state" : self.get_argument("state", None),
109 "country_code" : self.get_argument("country_code"),
110 }
111
112 # Add URLs to redirect the user back
113 args.update({
114 "success_url" : "https://%s/donate/thank-you" % self.request.host,
115 "error_url" : "https://%s/donate/error" % self.request.host,
116 "back_url" : "https://%s/donate?amount=%s&currency=%s&frequency=%s" %
117 (self.request.host, amount, currency, frequency),
118 })
119
120 # Send request to Zeiterfassung
121 try:
122 response = yield self.backend.zeiterfassung.send_request(
123 "/api/v1/donations/create/ipfire-project", **args)
124
125 except Exception:
126 raise # XXX handle any problems when Zeiterfassung is unreachable
127
128 # Redirect the user to the payment page
129 redirect_url = response.get("redirect_url")
130 if not redirect_url:
131 raise tornado.web.HTTPError(500, "Did not receive a redirect URL")
132
133 self.redirect(redirect_url)
134
135
136 class DownloadHandler(BaseHandler):
137 def get(self):
138 release = self.releases.get_latest()
139
140 self.render("download.html", release=release)
141
142
143 class StaticHandler(BaseHandler):
144 def initialize(self, template):
145 self._template = template
146
147 def get(self):
148 self.render("static/%s" % self._template)