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