-
Notifications
You must be signed in to change notification settings - Fork 5
/
webserver_utils.py
103 lines (81 loc) · 3.09 KB
/
webserver_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
import Cookie
import os
import sys
import string
import simplejson
import re
from platformer_config import TEMPLATE_DIR
from database_tables import Player
def render_template_file( filename, substitutionDict ):
# find everything that matches ${_stuff}
template_file = open( os.path.join( TEMPLATE_DIR, filename ), "r")
template_file_contents = template_file.read()
template_file.close()
localizations = re.findall(r"\$\{(\_[^}]*)\}", template_file_contents)
player = verify_id()
loc_strings = getStrings(player)
for key in localizations:
substitutionDict[key] = loc_strings[key].encode("utf-8")
if re.search(r"\$\{localized\_strings\}", template_file_contents) != None:
substitutionDict["localized_strings"] = simplejson.dumps(loc_strings)
template = string.Template(template_file_contents)
return template.safe_substitute( substitutionDict )
def print_redirect(url, cookie = None):
print "Status: 302" # temporary redirect
if cookie:
print cookie
print "Location: " + url
print
def logout():
artist = verify_id()
artist.session = ""
antimatter_cookie = Cookie.SimpleCookie()
antimatter_cookie["email"] = artist.email
antimatter_cookie["email"]["expires"] = 0
antimatter_cookie["session"] = artist.session
antimatter_cookie["session"]["expires"] = 0
print_redirect("index.html", antimatter_cookie)
def verify_id():
if os.environ.has_key('HTTP_COOKIE'):
cookie = Cookie.SimpleCookie(os.environ['HTTP_COOKIE'])
if cookie.has_key("email") and cookie.has_key("session"):
matches = Player.selectBy(email = cookie["email"].value,
session = cookie["session"].value)
if matches.count() > 0:
if matches[0].session != "":
return matches[0]
# If verification fails, kick 'em back out to index.html
print_redirect("index.html")
sys.exit(1)
m_all_strings = False
def _loadStringDict():
global m_all_strings
# Localization - read srings.json only once
string_file = open( "strings.json", "r")
m_all_strings= simplejson.loads(string_file.read().decode("utf-8"))
string_file.close()
def getStrings(player):
global m_all_strings
if m_all_strings == False:
_loadStringDict()
lang = player.langPref
if lang == "" or (not lang in m_all_strings.keys()):
lang = "en"
return m_all_strings[lang]
def getStr(player, key):
strings = getStrings(player)
return strings[key].encode("utf-8")
def make_lang_settings(selectedLang):
global m_all_strings
if m_all_strings == False:
_loadStringDict()
settingsHtml = ""
for key in m_all_strings.keys():
dict = {"lang_code": key.encode("utf-8"),
"checked": "",
"language": (m_all_strings[key]["_this_language"]).encode("utf-8")}
if selectedLang == key.encode("utf-8"):
dict["checked"] = "checked=\"checked\""
settingsHtml += render_template_file("lang-radio-button.html", dict)
return settingsHtml