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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
import cgi
import MySQLdb
DB_USER = 'codelove'
DB_PASS = ''
DB_HOST = 'localhost'
DB_BASE = 'codelove'
DB_TABLE_PAGES = 'codelove_pages'
DB_TABLE_POSTS = 'codelove_posts'
def a(attrs='', inner=[]):
"""
Returns a <a> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <a></a> tag set.
"""
if attrs:
if '_start' in attrs:
attrs = ' %s' % attrlist(attrs)
return '<a%s>' % attrs
elif '_end' in attrs:
return '</a>'
attrs = " %s" % attrlist(attrs)
return '<a%s>%s</a>' % (attrs, " ".join(inner))
def body(attrs='', inner=[]):
"""
Returns a <body> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <body></body> tag set.
"""
if attrs:
if '_start' in attrs:
return '<body>'
elif '_end' in attrs:
return '</body>'
return '<body>%s</body>' % (" ".join(inner))
def div(attrs='', inner=[]):
"""
Returns a <div> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <div></div> tag set.
"""
if attrs:
if '_start' in attrs:
attrs = ' %s' % attrlist(attrs)
return '<div%s>' % attrs
elif '_end' in attrs:
return '</div>'
attrs = ' %s' % attrlist(attrs)
else:
attrs = ''
return '<div%s>%s</div>' % (attrs, " ".join(inner))
def li(attrs='', inner=[]):
"""
Returns a <li> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <li></li> tag set.
"""
if attrs:
if '_start' in attrs:
attrs = ' %s' % attrlist(attrs)
return '<li%s>' % attrs
elif '_end' in attrs:
return '</li>'
attrs = ' %s' % attrlist(attrs)
else:
attrs = ''
return '<li%s>%s</li>' % (attrs, " ".join(inner))
def span(attrs='', inner=[]):
"""
Returns a <span> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <span></span> tag set.
"""
if attrs:
if '_start' in attrs:
attrs = ' %s' % attrlist(attrs)
return '<span%s>' % attrs
elif '_end' in attrs:
return '</span>'
attrs = ' %s' % attrlist(attrs)
return '<span%s>%s</span>' % (attrs, " ".join(inner))
def ul(attrs='', inner=[]):
"""
Returns a <ul> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <ul></ul> tag set.
"""
if attrs:
if '_start' in attrs:
attrs = ' %s' % attrlist(attrs)
return '<ul%s>' % attrs
elif '_end' in attrs:
return '</ul>'
attrs = ' %s' % attrlist(attrs)
return '<ul%s>%s</ul>' % (attrs, " ".join(inner))
def header(content_type="text/html"):
return "Content-type: %s\n\n" % str(content_type)
def doctype():
"""
Returns a doctype statement for XHTML 1.0 Transitional.
"""
return (
"\n".join([
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"',
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\n"
])
)
def html(attrs={}, inner=[]):
"""
Returns an <html> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <html></html> tag set.
"""
if attrs:
if '_start' in attrs:
return '<html>'
elif '_end' in attrs:
return '</html>'
return '<html>%s</html>' % (" ".join(inner))
def head(attrs={}, inner=[]):
"""
Returns a <head> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <head></head> tag set.
"""
if attrs:
if '_start' in attrs:
return '<head>'
elif '_end' in attrs:
return '</head>'
return '<head>%s</head>' % (" ".join(inner))
def title(attrs={}, inner=[]):
"""
Returns a <title> tag set.
Expects a dictionary, attrs, containing tag attributes; a list, inner,
containing the contents of the tag. If ['_start'] is passed as an
attribute, only the first half of the tag will be returned; similarly,
if '_end' is passed as an attribute, only the second half will be
returned.
Returns a standard <title></title> tag set.
"""
if attrs:
if '_start' in attrs:
return '<title>'
elif '_end' in attrs:
return '</title>'
return '<title>%s</title>' % (" ".join(inner))
def link(attrs={}):
"""
Returns a <link /> tag set.
Expects a dictionary, attrs, containing tag attributes.
Returns a standard <link /> tag.
"""
if attrs:
attrs = ' %s' % ' '.join(['%s="%s"' % (k, v) for k, v in attrs.items()])
else:
attrs = ''
return '<link%s />' % attrs
def attrlist(attrs={}):
"""
This function prints a list of keys and values in standard html attribute
format. Example: 'attr1="key1"'
Expects a dictionary, attrs, of key/value pairs.
Returns a string.
"""
if attrs:
if '_start' in attrs:
del attrs['_start']
if '_end' in attrs:
del attrs['_end']
attrs = '%s' % (' '.join(
['%s="%s"' % (k, v) for k, v in attrs.items()])
)
else:
attrs = ''
return attrs
class Site:
'Manage site creation.'
pass
class Page:
'Manage page creation.'
def __init__(self):
'Constructor for standard Page.'
self.content = ''
self.db = ''
def add(self, string):
'Add content to the Page.'
self.content += string
def display(self):
'Display the content of the Page.'
return self.content
def generate_linkbar(self):
"""
This function uses rows from DB_TABLE_PAGES to create a navigation bar.
Expects zero arguments.
Returns html code of navigation bar.
"""
output = ''
if not self.db:
self.db = DBSession()
self.db.connect()
self.db.cursor.execute(
"""
SELECT name, url FROM %s
ORDER BY page_id ASC
""" % DB_TABLE_PAGES)
results = self.db.cursor.fetchall()
output += ul({'_start':1, 'class':'linkbar'})
for row in results:
output += li('', [a({'href':row['url']},
['[%s]' % row['name']])])
output += ul({'_end':1})
return output
def generate_posts(self,page_id=1):
"""
Generates HTML code for a series of posts.
Returns a string containing the code.
"""
if not self.db:
self.db = DBSession()
self.db.connect()
output = ''
self.db.cursor.execute(
"""
SELECT title, post_date, author, post FROM %s
ORDER BY post_date DESC
""" % DB_TABLE_POSTS)
results = self.db.cursor.fetchall()
for row in results:
output += div({'class':'entry'}, [
span({'class':'subtitle'}, [row['title']]),
span({'class':'date'},
['posted by',
span({'class':'author'}, [row['author']]),
'on', row['post_date'].strftime('%m/%d/%Y')]),
span({'class':'post'}, [row['post']])
])
return output
class Session:
'Manages session data.'
def __init__(self):
pass
class DBSession:
"""
Manages a database connection for a Page or Site.
"""
def __init__(self, user=DB_USER, passwd=DB_PASS, host=DB_HOST,
base=DB_BASE):
"""
Initializes a database connection.
"""
self.host = host
self.user = user
self.passwd = passwd
self.base = base
def connect(self):
"""
Connect to the database.
"""
self.conn = MySQLdb.connect(self.host, self.user, self.passwd,
self.base)
self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
def disconnect(self):
"""
Disconnect from the database.
"""
self.cursor.close()
self.conn.close()
|