forked from JoneXiong/YouMd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
331 lines (290 loc) · 9.17 KB
/
model.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
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
# coding=utf-8
import calendar
from datetime import datetime
import config
from tool import Dict2Object
class Models:
"""
Models
"""
def params(self):
"""
parameters model for rendering templates
reference:
template/*/*
"""
model ={
'entries':None,
'entry':None,
'pager':None,
'archive':None,
'search':None,
'subscribe':None,
'error':None,
'primary':{
'abouts':None,
'tags':None,
'recently_entries':None,
},
'secondary':{
'categories':None,
'archive':None
}
}
return Dict2Object(model)
def entry(self, entry_type):
"""
entry model for both entry and page
args:
entry_type: entry or page
entry is the one you'll post usually
page is just the one fulfills other parts of the blog, such as /about.html page
reference:
tempate/entry.html, templage/modules/entry.html
"""
model = {
'author':{
'name':config.author,
'url':config.home
},
'path':'the path of the entry',
'name':'the displayed name of the entry',
'raw_url':'the url of the raw format of this entey',
'update_url':'the url of the raw update',
'header':'the header of the raw',
'url':'the url of the entry in this blog',
'type':entry_type,
'status':'published',
'time':None,
'date':None,
'excerpt':'the excerpt of the entry',
'content':'the content of the entry',
'html':'the html content of the entry',
'tags':[],
'categories':[],
'count':0,
'raw_count':0,
'private': False
}
return Dict2Object(model)
def search(self, search_type, value, total):
"""
search model for queriery
args:
serach_type: seach by tag, category, keyword, and or just main index of the blog
value: the keyword to be searched
total: the number of result matching this search
reference:
template/search.html
"""
value_show = value[2:] if value.startswith('__') else value
model = {
'type':search_type,
'value':value,
'title': '"' + value_show + '" 相关的信息如下,共 ' + str(total) + '条'
#'title':str(total)+ ' ' + self.plurals('result', total) + ' matching "' + value + '" of ' + search_type
}
return Dict2Object(model)
def pager(self, pager_type, value, total=0, pages=1, start=config.start, limit=config.limit):
"""
pager model for pagerbar
args:
pager_type: the type of this pagerbar, serach or main index
value: current search value
total: the number of total results
pages: the number of pages
start: current start page number
limit: current page size, that is how many results displayed in one page
reference:
template/index.html, template/search.html
"""
model ={
'type':pager_type,
'value':value,
'total':total,
'pages':pages,
'start':start,
'limit':limit,
'pagination':[i for i in xrange(1, pages + 1)]
}
return Dict2Object(model)
def archive(self, archive_type, archive_url, display, url, count=1):
"""
archve model
args:
archive_type: the type of this archive, entry or raw
archive_url: the url of this archive
display: the title displayed
url: current url of the archived item
count: the number of archived items
reference:
template/archive.html, template/modules/archive.html
"""
title = display + ' 共' + str(count) + '篇'
#title = 'Archive ' + str(count) + ' ' + self.plurals(archive_type, count) + ' of ' + display
model = {
'type':archive_type,
'url':archive_url,
'display':display,
'title':title,
'urls':[url],
'count':count
}
return Dict2Object(model)
def subscribe(self, time):
"""
subscribe model
args:
time: the last updated time of this blog
reference:
template/atom.xml
"""
model = {
'updated': time
}
return Dict2Object(model)
def error(self, code='404', url=''):
"""
error model
args:
code: error code
url: the requested url brings out this error
reference:
template/error.html, template/modules/error.html
"""
model = {
'title': code + ' Not Found',
'url':url,
'statusCode':code,
'message':'Oops! The requested url "' + url + '" could not be found...'
}
return Dict2Object(model)
def about(self, about_type, prev_url=None, prev_name=None, next_url=None, next_name=None):
"""
about model
args:
about_type: the type of this about, entry, or archive
prev_url: the previous url
prev_name: the name of previous url
next_url: the next url
next_name: the name of next url
reference:
template/widgets/about.html
"""
model = {
'type':about_type,
'display':about_type.title(),
'prev_url':prev_url,
'prev_name':prev_name,
'next_url':next_url,
'next_name':next_name
}
return Dict2Object(model)
def tag(self, tag, url):
"""
tag model
args:
tag: the tag
url: the entry url tagged by this tag
reference:
template/modules/tag.html, template/widgets/tag.html
"""
model = {
'name':tag,
'count':1,
'rank':config.ranks,
'urls':[url]
}
return Dict2Object(model)
def category(self, category, url):
"""
category model
args:
category: the category
url: the entry url in this category
reference:
template/modules/category.html, template/widgets/category.html
"""
model = {
'name':category,
'count':1,
'rank':1,
'subs':[],
'urls':[url]
}
return Dict2Object(model)
def calendar(self, date):
"""
calendar widget
args:
date: the date of current calendar
reference:
template/widgets/calendar.html
"""
calendar.setfirstweekday(calendar.SUNDAY)
ym = date[:len('yyyy-mm')]
y, m, _ = [int(i) for i in date.split('-')]
_, n = calendar.monthrange(y, m)
urls = [None for _ in range(0, n + 1)]
urls[0] = ''
model = {
'month':ym,
'display':datetime(int(y), int(m), 1).strftime('%B %Y'),
'days':calendar.monthcalendar(y, m),
'urls':urls,
'counts':[0 for _ in range(0, n+1)]
}
return Dict2Object(model)
def monthly_archive(self, archive_type, month, url):
"""
monthly archive model
args:
archive_type: the type of this archive
month: current archived month
url: the entry url of the archived item
reference:
template/widgets/archive.html
"""
y, m , _ = month.split('/')
display = datetime(int(y), int(m), 1).strftime('%B %Y')
archive_url = config.archive_url + '/' + month
return self.archive(archive_type, archive_url, display, url, 1)
def plurals(self, key, count=0):
"""
words model for its plural or singular form
args:
key: singular word
count: 0, 1 or any number bigger than 1
"""
words = {
'entry':'entries',
'raw':'raws',
'tag':'tags',
'category':'categories',
'result':'results'
}
if count > 1 and not words.get(key) == None:
return words.get(key)
return key
def types(self):
"""
types model for miscellanies
"""
model = {
'blog':'blog',
'entry':'entry',
'page':'page',
'raw':'raw',
'query':'query',
'tag':'tag',
'category':'category',
'index':'index',
'add':'add',
'delete':'delete',
'archive':'archive',
'all':'全部'
}
return Dict2Object(model)
if __name__ == '__main__':
import doctest
doctest.testmod()