def get(self, id): id = int(id) # 转换成数字 cur = self.db.cursor() cur.execute('SELECT posttime FROM articles WHERE id = %s', (id,)) posttime = fetchone(cur).posttime cur.execute('UPDATE tags SET count = count - 1 ' 'WHERE tag in (SELECT tag FROM link_tags_articles WHERE article_id = %s)', (id,)) cur.execute('DELETE FROM link_tags_articles WHERE article_id = %s', (id,)) cur.execute('DELETE FROM tags WHERE count = 0') cur.execute('DELETE FROM link_files_articles WHERE article_id = %s', (id,)) cur.execute('DELETE FROM articles WHERE id = %s', (id,))#删除文章 date_add(self.db, posttime, add=-1) # 减去一个日期计数 search.delete_document(id) # 删除索引 search.flush() return self.write(u'ID:%s 的文章已删除' % id)
def post(self): cur = self.db.cursor() title = self.get_argument('title', '') # 标题 content = self.get_argument('content', '') # 内容 render = html_body(content) # 渲染rst结果到缓存 posttime = datetime.now() tags = self.get_argument('tags', '').split(',') # tag集合 tags = tuple(set(tags)) # 去除重复记录 files = self.get_argument('files', '').split(',') # files集合 if '' in files: files.remove('')#去掉空文件 cur.execute('SELECT tag FROM tags WHERE tag in %s', (tags,)) exists_tags = fetchall(cur) insert_tags = list(tags) for t in exists_tags: insert_tags.remove(t.tag) cur.execute('INSERT INTO articles(title,content,render,posttime) VALUES(%s,%s,%s,%s) RETURNING id', (title, content, render, posttime))#插入文章 article_id = cur.fetchone()[0]#取得插入的文章id for t in insert_tags:#插入新的tag cur.execute('INSERT INTO tags VALUES(%s,%s)', (t, 0)) for t in tags: cur.execute('INSERT INTO link_tags_articles(tag,article_id) VALUES(%s,%s)', (t, article_id))#插入tag关系 cur.execute('UPDATE tags SET count = count+1 WHERE tag in %s', (tags,))#更新count值 for f in files: cur.execute('INSERT INTO link_files_articles(filename,article_id) VALUES(%s,%s)', (f, article_id))#插入附件关系 date_add(self.db, posttime) # 添加日期计数 #加到xapian全文索引中 terms = title+content+(' '.join(tag for tag in tags)) if type(terms) == unicode: terms = terms.encode('utf-8') search.index(article_id, terms, values={1:xapian.sortable_serialise(mktime(posttime.timetuple()))}) search.flush() self.redirect(self.reverse_url('index'))