Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

make some changes

  • Loading branch information...
commit 21cd9bc3c72c78d13c13f2ac35db4467cd63c285 1 parent 43a04e6
David Xie authored
8 davidblog/admin.py
@@ -9,10 +9,10 @@
9 9 '/$', 'adminviews.index',
10 10 '/login/', 'adminviews.login',
11 11 '/logout/', 'adminviews.logout',
12   - '/category/list/', 'adminviews.category_list',
13   - '/category/add/', 'adminviews.category_add',
14   - '/category/del/(.*)/', 'adminviews.category_del',
15   - '/category/edit/(.*)/', 'adminviews.category_edit',
  12 + '/link/list/', 'adminviews.link_list',
  13 + '/link/add/', 'adminviews.link_add',
  14 + '/link/del/(.*)/', 'adminviews.link_del',
  15 + '/link/edit/(.*)/', 'adminviews.link_edit',
16 16 '/tag/list/', 'adminviews.tag_list',
17 17 '/tag/add/', 'adminviews.tag_add',
18 18 '/tag/del/(.*)/', 'adminviews.tag_del',
105 davidblog/adminviews.py
@@ -29,10 +29,11 @@ def GET(self):
29 29 class index(object):
30 30 @login_required
31 31 def GET(self):
32   - d['entry_num'] = web.ctx.orm.query(Entry).count()
33   - d['comment_num'] = web.ctx.orm.query(Comment).count()
34   - d['tag_num'] = web.ctx.orm.query(Tag).count()
35   - d['link_num'] = web.ctx.orm.query(Link).count()
  32 + d['entryNum'] = web.ctx.orm.query(Entry).count()
  33 + d['tagNum'] = web.ctx.orm.query(Tag).count()
  34 + d['linkNum'] = web.ctx.orm.query(Link).count()
  35 + d['pageNum'] = web.ctx.orm.query(Page).count()
  36 + d['commentNum'] = web.ctx.orm.query(Comment).count()
36 37 return render.index(**d)
37 38
38 39 class login(object):
@@ -175,72 +176,80 @@ def GET(self, id):
175 176 web.ctx.orm.delete(i)
176 177 return web.seeother('/entry/list/')
177 178
178   -class links(object):
  179 +class link_list(object):
179 180 @login_required
180 181 def GET(self):
181 182 page = web.input(page=1)
182   - page = int(page.page)
183   - linkNum = list(db.query("SELECT COUNT(id) AS num FROM links"))
184   - pages = float(linkNum[0]['num'] / 10)
185   - if pages > int(pages):
186   - pages = int(pages + 1)
187   - elif pages == 0:
188   - pages = 1
189   - else:
190   - pages = int(pages)
191   - if page > pages:
192   - page = pages
193   - links = list(db.query('SELECT * FROM links ORDER BY name ASC '
194   - 'LIMIT $start, 10', vars={'start': (page - 1) * 10}))
195   -
196   - para['page'] = page
197   - para['pages'] = pages
198   - para['links'] = links
199   -
200   - return render.tag(**para)
  183 + try:
  184 + page = int(page.page)
  185 + except:
  186 + page = 1
  187 + linkCount = web.ctx.orm.query(Link).count()
  188 + p = Pagination(linkCount, pageCount, page)
  189 + links = web.ctx.orm.query(Link).order_by('links.name ASC')[int(p.start):int(p.start+p.limit)]
  190 + d['p'] = p
  191 + d['links'] = links
  192 + return render.link_list(**d)
201 193
202 194 class link_add(object):
203 195 @login_required
204 196 def GET(self):
205 197 f = linkForm()
206   -
207   - para['f'] = f
208   -
209   - return render.link_add(**para)
  198 + d['f'] = f
  199 + return render.link_add(**d)
210 200
211 201 @login_required
212 202 def POST(self):
213 203 f = linkForm()
214 204 if f.validates():
215   - data = dict(**f.d)
216   - db.insert('links', name = data['name'], url = data['url'])
217   - return web.seeother('/links/')
  205 + link = Link()
  206 + link.name = f.name.value
  207 + link.description = f.description.value
  208 + link.url = f.url.value
  209 + web.ctx.orm.add(link)
  210 + try:
  211 + web.ctx.orm.commit()
  212 + except:
  213 + web.ctx.orm.rollback()
  214 + return web.seeother('/link/list/')
  215 + else:
  216 + d['f'] = f
  217 + return render.link_add(**d)
218 218
219 219 class link_edit(object):
  220 + def getLink(self, id):
  221 + return web.ctx.orm.query(Link).filter_by(id=id).first()
  222 +
220 223 @login_required
221 224 def GET(self, id):
222 225 f = linkForm()
223   - links = list(db.query("SELECT * FROM links WHERE id = $id", vars = {'id':id}))
224   - f.get('name').value = links[0].name
225   - f.get('url').value = links[0].slug
226   -
227   - para['f'] = f
228   -
229   - return render.link_edit(**para)
  226 + link = self.getLink(id)
  227 + f.name.value = link.name
  228 + f.url.value = link.url
  229 + f.description.value = link.description
  230 + d['f'] = f
  231 + d['link'] = link
  232 + return render.link_edit(**d)
230 233
231 234 @login_required
232 235 def POST(self, id):
233 236 f = linkForm()
234 237 if f.validates():
235   - data = dict(**f.d)
236   - db.update('links', where = "id = %s" % id, name = data['name'], url = data['url'])
237   - return web.seeother('/links/')
  238 + link = self.getLink(id)
  239 + link.name = f.name.value
  240 + link.url = f.url.value
  241 + link.description = f.description.value
  242 + return web.seeother('/link/list/')
  243 + else:
  244 + d['f'] = f
  245 + return render.link_edit(**d)
238 246
239 247 class link_del(object):
240 248 @login_required
241 249 def GET(self, id):
242   - db.delete('links', where = 'id = %s' % id)
243   - return web.seeother('/links/')
  250 + link = web.ctx.orm.query(Link).filter_by(id=id).first()
  251 + web.ctx.orm.delete(link)
  252 + return web.seeother('/link/list/')
244 253
245 254 class page_list(object):
246 255 @login_required
@@ -262,7 +271,10 @@ def POST(self):
262 271 if f.validates():
263 272 page = Page(f.title.value, f.slug.value, f.content.value)
264 273 web.ctx.orm.add(page)
265   - return web.seeother('/page/list/')
  274 + return web.seeother('/page/list/')
  275 + else:
  276 + d['f'] = f
  277 + return render.page_add(**d)
266 278
267 279 class page_edit(object):
268 280 def getPage(self, id):
@@ -286,7 +298,10 @@ def POST(self, id):
286 298 page.slug = f.slug.value
287 299 page.content = f.content.value
288 300 page.modifiedTime= datetime.now()
289   - return web.seeother('/page/list/')
  301 + return web.seeother('/page/list/')
  302 + else:
  303 + d['f'] = f
  304 + return render.page_edit(**d)
290 305
291 306 class page_del(object):
292 307 @login_required
3  davidblog/forms.py
@@ -25,7 +25,8 @@
25 25
26 26 linkForm = form.Form(
27 27 form.Textbox('name', form.notnull),
28   - form.Textbox('url', form.notnull, urlValidator)
  28 + form.Textbox('url', form.notnull, urlValidator),
  29 + form.Textbox('description', form.notnull)
29 30 )
30 31
31 32 entryForm = form.Form(
3  davidblog/models.py
@@ -97,7 +97,8 @@ class Link(Base):
97 97 id = Column(Integer, primary_key=True)
98 98 name = Column(String(45))
99 99 url = Column(String(45))
100   - created_time = Column(DateTime)
  100 + description = Column(String(255))
  101 + created_time = Column(DateTime, default=datetime.now())
101 102
102 103 class Admin(Base):
103 104 __tablename__ = 'admins'
5 davidblog/static/admin/css/admin.css
@@ -10,4 +10,7 @@
10 10 #edit_entry form table td > span {
11 11 color: red;
12 12 padding: 5px;
13   -}
  13 +}
  14 +.actions {
  15 + text-align:center;
  16 +}
11 davidblog/templates/admin/base.html
@@ -16,7 +16,7 @@
16 16 <h2><a href="/">泥泞的沼泽</a><span>0.3 alpha</span></h2>
17 17 <div id="topmenu">
18 18 <ul>
19   - <li class="current"><a href="/admin/">管理首页</a></li>
  19 + <li><a href="/admin/">管理首页</a></li>
20 20 <li><a href="/admin/entry/list/">文章管理</a></li>
21 21 <li><a href="/admin/comment/list/">评论管理</a></li>
22 22 <li><a href="/admin/page/list/">页面管理</a></li>
@@ -44,15 +44,6 @@
44 44 <div id="credits">
45 45 <p>版权所有©2009 - 2010 <a href="http://davidx.me/">David Xie</a>
46 46 </div>
47   - <div id="styleswitcher">
48   - <ul>
49   - <li><a href="javascript: document.cookie='theme='; window.location.reload();" title="Default" id="defswitch">d</a></li>
50   - <li><a href="javascript: document.cookie='theme=1'; window.location.reload();" title="Blue" id="blueswitch">b</a></li>
51   - <li><a href="javascript: document.cookie='theme=2'; window.location.reload();" title="Green" id="greenswitch">g</a></li>
52   - <li><a href="javascript: document.cookie='theme=3'; window.location.reload();" title="Brown" id="brownswitch">b</a></li>
53   - <li><a href="javascript: document.cookie='theme=4'; window.location.reload();" title="Mix" id="mixswitch">m</a></li>
54   - </ul>
55   - </div>
56 47 <br />
57 48 </div>
58 49 </div>
5 davidblog/templates/admin/entry_add.html
... ... @@ -1,6 +1,11 @@
1 1 {% extends 'base.html' %}
2 2 {% block title %}添加新日志{% endblock %}
3 3 {% block content %}
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(1)').addClass('current');
  7 + });
  8 +</script>
4 9 <script type="text/javascript" src="/static/markitup/jquery.markitup.pack.js"></script>
5 10 <script src="/static/markitup/sets/markdown/set.js" type="text/javascript"></script>
6 11 <link href="/static/markitup/sets/markdown/style.css" rel="stylesheet" type="text/css"/>
5 davidblog/templates/admin/entry_edit.html
... ... @@ -1,6 +1,11 @@
1 1 {% extends 'base.html' %}
2 2 {% block title %}修改日志{% endblock %}
3 3 {% block content %}
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(1)').addClass('current');
  7 + });
  8 +</script>
4 9 <script type="text/javascript" src="/static/markitup/jquery.markitup.pack.js"></script>
5 10 <script src="/static/markitup/sets/markdown/set.js" type="text/javascript"></script>
6 11 <link href="/static/markitup/sets/markdown/style.css" rel="stylesheet" type="text/css"/>
7 davidblog/templates/admin/entry_list.html
... ... @@ -1,6 +1,11 @@
1 1 {% extends 'base.html' %}
2 2 {% block title %}日志列表{% endblock %}
3 3 {% block content %}
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(1)').addClass('current');
  7 + });
  8 +</script>
4 9 <table>
5 10 <tr>
6 11 <th>名称</th>
@@ -11,7 +16,7 @@
11 16 <tr>
12 17 <td>{{ one.title }}</td>
13 18 <td>{{ one.created_time }}</td>
14   - <td>
  19 + <td class="actions">
15 20 <a href="/admin/entry/edit/{{ one.id }}/">修改</a> |
16 21 <a href="/admin/entry/del/{{ one.id }}/">删除</a>
17 22 </td>
29 davidblog/templates/admin/index.html
... ... @@ -1,11 +1,26 @@
1 1 {% extends "base.html" %}
2 2 {% block title %}首页{% endblock %}
3 3 {% block content %}
4   -<ul>
5   - <li><a href="/admin/entry/list/">现在共有 {{ entryNum }} 篇文章</a></li>
6   - <li><a href="/admin/comment/list/">现在共有 {{ commentNum }} 条评论</a></li>
7   - <li><a href="/admin/category/list/">现在共有 {{ categoryNum }} 个分类</a></li>
8   - <li><a href="/admin/tag/list/">现在共有 {{ tagNum }} 个标签</a></li>
9   - <li><a href="/admin/link/list/">现在共有 {{ linkNum }} 个链接</a></li>
10   -</ul>
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(0)').addClass('current');
  7 + });
  8 +</script>
  9 +<table>
  10 + <tr>
  11 + <td><a href="/admin/entry/list/">现在共有 {{ entryNum }} 篇文章</a></td>
  12 + </tr>
  13 + <tr>
  14 + <td><a href="/admin/comment/list/">现在共有 {{ commentNum }} 条评论</a></td>
  15 + </tr>
  16 + <tr>
  17 + <td><a href="/admin/tag/list/">现在共有 {{ tagNum }} 个标签</a></td>
  18 + </tr>
  19 + <tr>
  20 + <td><a href="/admin/link/list/">现在共有 {{ linkNum }} 个链接</a></td>
  21 + </tr>
  22 + <tr>
  23 + <td><a href="/admin/page/list/">现在共有 {{ pageNum }} 个链接</a></td>
  24 + </tr>
  25 +</table>
11 26 {% endblock %}
34 davidblog/templates/admin/link_add.html
... ... @@ -1,6 +1,28 @@
34 davidblog/templates/admin/link_edit.html
... ... @@ -1,6 +1,28 @@
29 davidblog/templates/admin/page_add.html
... ... @@ -1,6 +1,11 @@
1   -{% extends "base.html" %}
  1 +{% extends 'base.html' %}
2 2 {% block title %}添加新页面{% endblock %}
3 3 {% block content %}
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(3)').addClass('current');
  7 + });
  8 +</script>
4 9 <script type="text/javascript" src="/static/markitup/jquery.markitup.pack.js"></script>
5 10 <script src="/static/markitup/sets/markdown/set.js" type="text/javascript"></script>
6 11 <link href="/static/markitup/sets/markdown/style.css" rel="stylesheet" type="text/css"/>
@@ -36,14 +41,22 @@
36 41 });
37 42 });
38 43 </script>
39   -<div id="entry_edit">
  44 +<div id="edit_entry">
40 45 <form action="/admin/page/add/" method="POST">
41   - <p><input type="text" name="title" value="{{ f.title.value | notnull }}" />{{ f.title.note | notnull }}
42   - <label>标题</label></p>
43   - <p><input type="text" name="slug" value="{{ f.slug.value | notnull }}" />{{ f.slug.note | notnull }}
44   - <label>Slug</label></p>
45   - <p><textarea name="content" id="markItUp">{{ f.content.value | notnull }}</textarea>{{ f.content.note | notnull }}</p>
46   - <p><input type="submit" value="发表日志" /></p>
  46 + <table>
  47 + <tr>
  48 + <th><label>标题</label></th>
  49 + <td><input type="text" name="title" value="{{ f.title.value | notnull }}" /><span>{{ f.title.note | notnull }}</span></td>
  50 + </tr>
  51 + <tr>
  52 + <th><label>Slug</label></th>
  53 + <td><input type="text" name="slug" value="{{ f.slug.value | notnull }}" /><span>{{ f.slug.note | notnull }}</span></td>
  54 + </tr>
  55 + <tr>
  56 + <td colspan="2"><textarea name="content" id="markItUp">{{ f.content.value | notnull }}</textarea><span>{{ f.content.note | notnull }}</span></td>
  57 + </tr>
  58 + </table>
  59 + <p><input type="submit" value="发表页面" /></p>
47 60 </form>
48 61 </div>
49 62 {% endblock %}
33 davidblog/templates/admin/page_edit.html
... ... @@ -1,12 +1,17 @@
1   -{% extends "base.html" %}
2   -{% block title %}添加新页面{% endblock %}
  1 +{% extends 'base.html' %}
  2 +{% block title %}修改页面{% endblock %}
3 3 {% block content %}
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(3)').addClass('current');
  7 + });
  8 +</script>
4 9 <script type="text/javascript" src="/static/markitup/jquery.markitup.pack.js"></script>
5 10 <script src="/static/markitup/sets/markdown/set.js" type="text/javascript"></script>
6 11 <link href="/static/markitup/sets/markdown/style.css" rel="stylesheet" type="text/css"/>
7 12 <link href="/static/markitup/skins/markitup/style.css" rel="stylesheet" type="text/css"/>
8 13 <script type="text/javascript">
9   -$(document).ready(function() {
  14 +$(document).ready(function() {
10 15 // Add markItUp! to your textarea in one line
11 16 // $('textarea').markItUp( { Settings }, { OptionalExtraSettings } );
12 17 $('#markItUp').markItUp(mySettings);
@@ -37,13 +42,21 @@
37 42 });
38 43 </script>
39 44 <div id="entry_edit">
40   - <form action="/admin/page/edit/{{ page.id }}/" method="POST">
41   - <p><input type="text" name="title" value="{{ page.title | notnull }}" />{{ f.title.note | notnull }}
42   - <label>标题</label></p>
43   - <p><input type="text" name="slug" value="{{ page.slug | notnull }}" />{{ f.slug.note | notnull }}
44   - <label>Slug</label></p>
45   - <p><textarea name="content" id="markItUp">{{ page.content | notnull }}</textarea>{{ f.content.note | notnull }}</p>
46   - <p><input type="submit" value="更新日志" /></p>
  45 + <form action="/admin/page/edit/{{ page.id }}/" method="POST" id="entry_form">
  46 + <table>
  47 + <tr>
  48 + <th><label>标题</label></th>
  49 + <td><input type="text" name="title" value="{{ f.title.value | notnull }}" /><span>{{ f.title.note | notnull }}</span></td>
  50 + </tr>
  51 + <tr>
  52 + <th><label>Slug</label></th>
  53 + <td><input type="text" name="slug" value="{{ f.slug.value | notnull }}" /><span>{{ f.slug.note | notnull }}</span></td>
  54 + </tr>
  55 + <tr>
  56 + <td colspan="2"><textarea name="content" id="markItUp">{{ f.content.value | notnull }}</textarea><span>{{ f.content.note | notnull }}</span></td>
  57 + </tr>
  58 + </table>
  59 + <p><input type="submit" value="更新页面" /></p>
47 60 </form>
48 61 </div>
49 62 {% endblock %}
25 davidblog/templates/admin/page_list.html
... ... @@ -1,22 +1,27 @@
1   -{% extends "base.html" %}
2   -{% block title %}页面管理{% endblock %}
  1 +{% extends 'base.html' %}
  2 +{% block title %}页面列表{% endblock %}
3 3 {% block content %}
  4 +<script type="text/javascript">
  5 + $(document).ready(function() {
  6 + $('#topmenu').find('li:eq(3)').addClass('current');
  7 + });
  8 +</script>
4 9 <table>
5 10 <tr>
6   - <th>No.</th>
7 11 <th>名称</th>
  12 + <th>发表时间</th>
8 13 <th>操作</th>
9 14 </tr>
10   - {% for i in pages %}
  15 + {% for one in entries %}
11 16 <tr>
12   - <td>{{ loop.index }}</td>
13   - <td>{{ i.title }}</td>
14   - <td>
15   - <a href="/admin/page/edit/{{ i.id }}/">修改</a> |
16   - <a href="/admin/page/del/{{ i.id }}/">删除</a>
  17 + <td>{{ one.title }}</td>
  18 + <td>{{ one.created_time }}</td>
  19 + <td class="actions">
  20 + <a href="/admin/page/edit/{{ one.id }}/">修改</a> |
  21 + <a href="/admin/page/del/{{ one.id }}/">删除</a>
17 22 </td>
18 23 </tr>
19   - {% endfor %}
  24 + {% endfor %}
20 25 </table>
21 26 <p><a href="/admin/page/add/">添加新页面</a></p>
22 27 {% endblock %}
9 davidblog/views.py
@@ -51,13 +51,16 @@ def GET(self):
51 51 # 读取当前页的文章
52 52 i = web.input(page=1)
53 53 ids = [int(one.id) for one in web.ctx.orm.query(Entry.id).all()]
54   - #randomEntries = [web.ctx.orm.query(Entry).filter_by(id=id).first() for id in random.sample(ids, 5)]
  54 + if len(ids) < 5:
  55 + randomEntries = web.ctx.orm.query(Entry).all()
  56 + else:
  57 + randomEntries = web.ctx.orm.query(Entry).filter(Entry.id.in_(random.sample(ids, 5)))
55 58 entryCount = web.ctx.orm.query(Entry).count()
56 59 p = Pagination(entryCount, 5, int(i.page))
57 60 d['entries'] = web.ctx.orm.query(Entry).order_by('entries.created_time DESC')[p.start:p.start + p.limit]
58 61 d['p'] = p
59 62 d['usedTime'] = time.time() - d['startTime']
60   - #d['randomEntries'] = randomEntries
  63 + d['randomEntries'] = randomEntries
61 64 return render.index(**d)
62 65
63 66 class entry(object):
@@ -96,7 +99,7 @@ def POST(self, slug):
96 99 for e in set(emails):
97 100 try:
98 101 web.sendmail('admin@davidx.me', e,
99   - u'您在"泥泞的沼泽"上回复的日志又有新的回复了!', message,
  102 + '您在"泥泞的沼泽"上回复的日志又有新的回复了!'.encode('utf-8'), message,
100 103 headers={'Content-Type':'text/html;charset=utf-8'})
101 104 except:
102 105 pass

0 comments on commit 21cd9bc

Please sign in to comment.
Something went wrong with that request. Please try again.