Permalink
Browse files

modify some bugs in admin part

  • Loading branch information...
1 parent c91d039 commit 95aba60063d2f24581d0035d88693d7141c71c65 David Shieh committed Mar 7, 2010
View
@@ -79,17 +79,21 @@ def GET(self):
@login_required
def POST(self):
i = web.input(tags = None)
+ f = entryForm()
if f.validates():
entry = Entry(f.title.value, f.slug.value, f.content.value)
web.ctx.orm.add(Entry)
- if i.get('tags') is not None:
- tags = [i.lstrip().rstrip() for i in i['tags'].split(',')]
- for tag in tags:
- t = web.ctx.orm.query(Tag).filter('LOWER(name)=:name').params(name=tag.lower()).first()
- if t:
- entry.tags.append(t)
- else:
- entry.tags.append(Tag(tag))
+ if i.get('tags') is not None:
+ tags = [i.lstrip().rstrip() for i in i['tags'].split(',')]
+ for tag in tags:
+ t = web.ctx.orm.query(Tag).filter('LOWER(name)=:name').params(name=tag.lower()).first()
+ if t:
+ entry.tags.append(t)
+ else:
+ entry.tags.append(Tag(tag))
+ else:
+ d['f'] = f
+ return render.entry_add(**d)
return web.seeother('/entry/list/')
class entry_edit(object):
@@ -153,9 +157,16 @@ def GET(self, id):
if entry:
if len(entry.tags) > 0:
for tag in entry.tags:
- tag.entryNum = tag.entryNum - 1
+ tagsToDel = list()
+ if tag.entryNum == 1:
+ tagsToDel.append(tag)
+ else:
+ tag.entryNum = tag.entryNum - 1
entry.tags.remove(tag)
web.ctx.orm.delete(entry)
+ if len(tagsToDel) > 0:
+ for i in tagsToDel:
+ web.ctx.orm.delete(i)
return web.seeother('/entry/list/')
class links(object):
@@ -225,45 +236,57 @@ def GET(self, id):
db.delete('links', where = 'id = %s' % id)
return web.seeother('/links/')
-class page(object):
+class page_list(object):
@login_required
def GET(self):
- pages = list(db.select('pages'))
-
- para['pages'] = pages
-
- return render.page(**para)
+ pages = web.ctx.orm.query(Page).all()
+ d['pages'] = pages
+ return render.page_list(**d)
class page_add(object):
@login_required
def GET(self):
- return render.page_add(**para)
+ d['f'] = pageForm()
+ return render.page_add(**d)
@login_required
def POST(self):
- data = web.input()
- db.insert('pages', title = data['title'], slug = data['slug'],
- createdTime = datetime.now(), content = data['content'])
- return web.seeother('/page/')
+ i = web.input()
+ f = pageForm()
+ if f.validates():
+ page = Page(f.title.value, f.slug.value, f.content.value)
+ web.ctx.orm.add(page)
+ return web.seeother('/page/list/')
class page_edit(object):
+ def getPage(self, id):
+ return web.ctx.orm.query(Page).filter_by(id=id).first()
+
@login_required
def GET(self, id):
- page = list(db.select('pages', where='id = %s' % id))
- if not page:
- para['page'] = page[0]
- return render.page_edit(**para)
+ page = self.getPage(id)
+ f = pageForm()
+ if page:
+ d['page'] = page
+ d['f'] = f
+ return render.page_edit(**d)
@login_required
def POST(self, id):
- data = web.input(title=None, slug=None, content=None)
- if not title and not slug and not content:
- db.update('pages', where="id = %s" % id, title=data.title, slug=data.slug, content=data.content)
- return web.seeother('/page/')
+ page = self.getPage(id)
+ f = pageForm()
+ if f.validates():
+ page.title = f.title.value
+ page.slug = f.slug.value
+ page.content = f.content.value
+ page.modifiedTime= datetime.now()
+ return web.seeother('/page/list/')
class page_del(object):
@login_required
def GET(self, id):
- db.delete('pages', where='id = %s' % id)
+ page = web.ctx.orm.query(Page).filter_by(id=id).first()
+ if page:
+ web.ctx.orm.delete(page)
return web.seeother('/page/')
View
@@ -4,7 +4,7 @@
from settings import db
__all__ = [
- 'commentForm', 'linkForm', 'entryForm'
+ 'commentForm', 'linkForm', 'entryForm', 'pageForm',
]
username_validate = form.regexp(r".{3,15}$", u"请输入3-15位的用户名")
@@ -34,3 +34,9 @@
form.Textbox('content', form.notnull),
form.Textbox('slug', form.notnull),
)
+
+pageForm = form.Form(
+ form.Textbox('title', form.notnull),
+ form.Textbox('slug', form.notnull),
+ form.Textbox('content', form.notnull),
+ )
View
@@ -79,11 +79,13 @@ class Page(Base):
title = Column(String)
slug = Column(String, unique=True)
content = Column(Text)
- createdTime = Column(DateTime)
- modifiedTime = Column(DateTime)
+ createdTime = Column(DateTime, default=datetime.now())
+ modifiedTime = Column(DateTime, default=datetime.now())
- def __init__(self, title):
+ def __init__(self, title, slug, content):
self.title = title
+ self.slug = slug
+ self.content = content
def __repr__(self):
return "<Page ('%s')>" % (self.title,)
@@ -38,13 +38,13 @@
</script>
<div id="edit_entry">
<form action="/admin/entry/add/" method="POST">
- <p>{{ f.title.render() }}
+ <p><input type="text" name="title" value="{{ f.title.value | notnull }}" />{{ f.title.note | notnull }}
<label>标题</label></p>
- <p>{{ f.slug.render() }}
+ <p><input type="text" name="slug" value="{{ f.slug.value | notnull }}" />{{ f.slug.note | notnull }}
<label>Slug</label></p>
<p><input type="text" name="tags" />
<label>标签</label></p>
- <p><textarea name="content" id="markItUp"></textarea></p>
+ <p><textarea name="content" id="markItUp">{{ f.content.value | notnull }}</textarea>{{ f.content.note | notnull }}</p>
<p><input type="submit" value="发表日志" /></p>
</form>
</div>
@@ -38,13 +38,13 @@
</script>
<div id="entry_edit">
<form action="/admin/entry/edit/{{ entry.id }}/" method="POST" id="entry_form">
- <p><input type="text" name="title" value="{{ entry.title }}" />{{ f.title.note | notnull }}
+ <p><input type="text" name="title" value="{{ entry.title | notnull }}" />{{ f.title.note | notnull }}
<label>标题</label></p>
- <p><input type="text" name="slug" value="{{ entry.slug }}" />{{ f.slug.note | notnull }}
+ <p><input type="text" name="slug" value="{{ entry.slug | notnull }}" />{{ f.slug.note | notnull }}
<label>Slug</label></p>
<p><input type="text" name="tags" value="{{ entry.tagList }}" />
<label>标签</label></p>
- <p><textarea name="content" id="markItUp">{{ entry.content }}</textarea>{{ f.content.note | notnull }}</p>
+ <p><textarea name="content" id="markItUp">{{ entry.content | notnull }}</textarea>{{ f.content.note | notnull }}</p>
<p><input type="submit" value="更新日志" /></p>
</form>
</div>
@@ -12,7 +12,7 @@
<td>{{ one.title }}</td>
<td>{{ one.categoryName }}</td>
<td>
- <a href="/admin/entry/edit/{{ one.id }}/">修改</a>
+ <a href="/admin/entry/edit/{{ one.id }}/">修改</a> |
<a href="/admin/entry/del/{{ one.id }}/">删除</a>
</td>
</tr>
View
@@ -1,22 +0,0 @@
-<%inherit file="base.html" />
-<%def name="title()">页面管理</%def>
-
-<table>
- <tr>
- <th>No.</th>
- <th>名称</th>
- <th>Slug</th>
- <th>操作</th>
- </tr>
- % for i, one in enumerate(pages):
- <tr>
- <td>${ i + 1 }</td>
- <td>${ one.title }</td>
- <td>${ one.slug }</td>
- <td>
- <a href="/admin/page/edit/${ one.id }/">修改</a>
- <a href="/admin/page/del/${ one.id }/">删除</a>
- </td>
- </tr>
- % endfor
-</table>
@@ -1,41 +1,49 @@
-<%inherit file="base.html" />
-<%def name="title()">添加新日志</%def>
- <script type="text/javascript" src="/static/ckeditor/ckeditor.js"></script>
- <script src="/static/ckeditor/_samples/sample.js" type="text/javascript"></script>
- <link href="/static/ckeditor/_samples/sample.css" rel="stylesheet" type="text/css"/>
-<div id="edit_entry">
- <form action="/admin/entry/add/" method="POST">
- <table>
- <tr>
- <td>标题</td>
- <td><input type="text" name="title" /></td>
- </tr>
- <tr>
- <td>Slug</td>
- <td><input type="text" name="slug" /></td>
- </tr>
- <tr>
- <td>分类</td>
- <td>
- <select name="categoryId">
- % for one in categories:
- <option value="${ one.id }">${ one.name }(${ one.entryNum })</option>
- % endfor
- </select>
- </td>
- </tr>
- <tr>
- <td>标签</td>
- <td><input type="text" name="tags" /></td>
- </tr>
- <tr>
- <td>正文</td>
- <td><textarea name="content"></textarea></td>
- </tr>
- <tr>
- <td>&nbsp;</td>
- <td><input type="submit" value="发表日志" /></td>
- </tr>
- </table>
+{% extends "base.html" %}
+{% block title %}添加新页面{% endblock %}
+{% block content %}
+<script type="text/javascript" src="/static/markitup/jquery.markitup.pack.js"></script>
+<script src="/static/markitup/sets/markdown/set.js" type="text/javascript"></script>
+<link href="/static/markitup/sets/markdown/style.css" rel="stylesheet" type="text/css"/>
+<link href="/static/markitup/skins/markitup/style.css" rel="stylesheet" type="text/css"/>
+<script type="text/javascript">
+$(document).ready(function() {
+ // Add markItUp! to your textarea in one line
+ // $('textarea').markItUp( { Settings }, { OptionalExtraSettings } );
+ $('#markItUp').markItUp(mySettings);
+
+ // You can add content from anywhere in your page
+ // $.markItUp( { Settings } );
+ $('.add').click(function() {
+ $.markItUp( { openWith:'<opening tag>',
+ closeWith:'<\/closing tag>',
+ placeHolder:"New content"
+ }
+ );
+ return false;
+ });
+
+ // And you can add/remove markItUp! whenever you want
+ // $(textarea).markItUpRemove();
+ $('.toggle').click(function() {
+ if ($("#markItUp.markItUpEditor").length === 1) {
+ $("#markItUp").markItUpRemove();
+ $("span", this).text("get markItUp! back");
+ } else {
+ $('#markItUp').markItUp(mySettings);
+ $("span", this).text("remove markItUp!");
+ }
+ return false;
+ });
+});
+</script>
+<div id="entry_edit">
+ <form action="/admin/page/add/" method="POST">
+ <p><input type="text" name="title" value="{{ f.title.value | notnull }}" />{{ f.title.note | notnull }}
+ <label>标题</label></p>
+ <p><input type="text" name="slug" value="{{ f.slug.value | notnull }}" />{{ f.slug.note | notnull }}
+ <label>Slug</label></p>
+ <p><textarea name="content" id="markItUp">{{ f.content.value | notnull }}</textarea>{{ f.content.note | notnull }}</p>
+ <p><input type="submit" value="发表日志" /></p>
</form>
</div>
+{% endblock %}
Oops, something went wrong.

0 comments on commit 95aba60

Please sign in to comment.