Exemple #1
0
def call():
    """运行此任务
    :returns: none

    """
    cur = db.cursor()
    cur.execute(
        'SELECT id, title, content, posttime, '
        'ARRAY('
        'SELECT tag FROM link_tags_articles WHERE link_tags_articles.article_id = articles.id'
        ') as tags '
        'FROM articles WHERE id != %s ',
        (0,))

    for row in fetchall(cur):
        search.index(row['id'], row['title']+row['content']+(' '.join(tag for tag in row['tags'])), values={1:xapian.sortable_serialise(time.mktime(row['posttime'].timetuple()))})

    search.flush()
Exemple #2
0
    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'))