/
site.py
207 lines (177 loc) · 7.2 KB
/
site.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
import cherrypy
from reprconf import Config
from couchdbkit import *
from couchdbkit_mapper import *
from genshi.template import TemplateLoader
import auth
from auth import current_user
def make_couchdb_server(url, username=None, password=None):
# TODO: implement authentication
return Server(url)
class _Empty(object):
pass
class ZT(object):
def __init__(self, data, comment="", solved=False):
self.data = data
self.comment = comment
self.solved = solved
def __repr__(self):
return "<ZT(%s,%s,%s)>" % (repr(self.data), repr(self.comment), repr(self.solved))
class Quest(object):
def __init__(self, name, ap, cl, level, mage, attr = None, solved = False, walkthrough = None):
self.name = name
self.points = ap
self.class_ = cl
self.level = level
self.mage = mage
self.attribute = attr
self.solved = solved
self.walkthrough = walkthrough
def __repr__(self):
return 'Quest(%s,%s,%s,%s,%s,%s,%s,%s)' % (repr(self.name), repr(self.points), repr(self.class_), repr(self.level), repr(self.mage), repr(self.attribute), repr(self.solved), repr(self.walkthrough))
class BaseSite(object):
config = Config({
'couch': {
'url': 'http://localhost:5984',
'user': None,
'password': None,
'database': {'quests': 'quests', 'zt': 'zt'}
}
})
def __init__(self, config_file):
another_config = Config(config_file)
try:
self.config['couch'].update(another_config['couch'])
except KeyError:
pass
self.config = self.config['couch']
self.server = make_couchdb_server(
self.config['url'],
self.config['user'],
self.config['password'])
self.db = _Empty()
self.db.quests = map(self.server[self.config['database']['quests']])
self.db.quests.add(Quest)
self.db.zt = map(self.server[self.config['database']['zt']])
self.db.zt.add(ZT)
self.loader = TemplateLoader(
os.path.join(os.path.dirname(__file__), 'templates'),
auto_reload=True
)
def content_type(self):
xhtml = "application/xhtml+xml"
if os.environ.get('HTTP_ACCEPT', '').find('application/xhtml+xml') == -1:
xhtml = "text/html"
cherrypy.response.headers['Content-Type'] = xhtml+"; Charset=UTF-8"
class InheritingSite(object):
def __init__(self, base):
self.base = base
class ZTSite(InheritingSite):
def zt_list(self, show_solved):
self.base.content_type()
tmpl = self.base.loader.load("zt.html")
return tmpl.generate(
zt=self.base.db.zt.all_docs(include_docs=True),
showsolved=show_solved
).render('xhtml', doctype='xhtml')
@cherrypy.expose
def index(self):
return self.zt_list(show_solved=False)
@cherrypy.expose
def showsolved(self):
return self.zt_list(show_solved=True)
def oneliner(self, id=None):
if id is None:
return "Which poem?"
try:
poem = self.base.db.zt[id]
except ResourceNotFound:
return "No such poem."
else:
return " ".join([s.strip() for s in poem.data.splitlines()])
oneliner.exposed = True
class QuestSite(InheritingSite):
class WalkthroughSite(object):
def __init__(self, super):
self.super = super
self.base = super.base
def is_authed(self):
"Return if the user doing the current request is authenticated"
try:
return auth.users[current_user()].privileges['walkthrough']
except (ResourceNotFound, KeyError, AttributeError):
return False
def get_quest(self, id):
try:
quest = self.base.db.quests[id]
except ResourceNotFound:
return False
else:
return quest
def backlink(self, id=None):
anchor = ""
if id:
anchor = "#quest_"+str(id)
return '<a href="/mg/quests/'+anchor+'">Back.</a>'
def edit(self, id=None, text=None, solved=False):
if not self.is_authed():
return "Not authenticated." # TODO: nicer
quest = self.get_quest(id)
if not quest: # None or False
return "No such quest." # TODO: nicer
walkthrough = ""
if hasattr(quest, 'walkthrough') and quest.walkthrough:
walkthrough = quest.walkthrough
if text:
if type(text) == str:
quest.walkthrough = text.decode("utf-8")
# must be utf8 as content_type() delivers the form as utf8
elif type(text) == unicode:
quest.walkthrough = text
else:
return "Data not entered. "+self.backlink(id) #TODO nicer
quest.solved = solved
self.base.db.quests.save_doc(quest)
return "Entered data. "+self.backlink(id) #TODO nicer
elif text == "":
if solved != quest.solved:
quest.solved = solved
try:
del(quest.walkthrough)
except AttributeError:
pass
self.base.db.quests.save_doc(quest)
return "Changed data. "+self.backlink(id) #TODO nicer
else:
self.base.content_type()
tmpl = self.base.loader.load("walkthrough/edit.html")
return tmpl.generate (quest=quest, walkthrough=walkthrough, authed=self.is_authed()).render('xhtml', doctype='xhtml')
edit.exposed = True
def default(self, id=None):
# if not self.is_authed():
# return "Not authenticated." # TODO: nicer
quest = self.get_quest(id)
if not quest or not quest.walkthrough:
return "No such walkthrough."
self.base.content_type()
tmpl = self.base.loader.load("walkthrough/view.html")
return tmpl.generate (quest=quest, walkthrough=quest.walkthrough, authed=self.is_authed()).render('xhtml', doctype='xhtml')
default.exposed = True
def __init__(self, base):
InheritingSite.__init__(self, base)
self.walkthrough = self.WalkthroughSite(self)
self.walkthrough.exposed = True
def index(self, order_by="level"):
# TODO: make views!
self.base.content_type()
try:
quests = self.base.db.quests.view('/'.join(['by', order_by]))
except ResourceNotFound:
quests = self.base.db.quests.all_docs(include_docs=True)
tmpl = self.base.loader.load("quest.html")
return tmpl.generate(quests=quests, authed=self.walkthrough.is_authed()).render('xhtml', doctype='xhtml')
index.exposed = True
site = BaseSite(os.path.join(os.path.dirname(__file__), "../conf/mg.conf"))
site.zt = ZTSite(site)
site.quests = QuestSite(site)