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