Пример #1
0
def test_cache():
    cache.set('key1', 'value1')
    assert cache.get('key1') == 'value1'

    cache.set('key1', 'value2')
    assert cache.get('key1') == 'value2'

    assert cache.add('key1', 'value1') == 'value2'
    assert cache.add('key2', 'value1') == 'value1'

    cache.delete('key1')
    assert cache.get('key1') is None

    mapping = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
    cache.set_multi(mapping)
    assert cache.get_multi(['key1', 'key2', 'key3']) == mapping

    cache.delete_multi(['key1', 'key2', 'key3'])

    cache.set_multi(mapping)
    value = cache.get_multi(['1', '2', '3'], key_prefix='key')
    assert value == {'1': 'value1', '2': 'value2', '3': 'value3'}

    cache.flush_all()
    assert cache.get('key1') is None
Пример #2
0
    def post(self, id):
        # for topic reply
        content = self.get_argument('content', None)
        if not content:
            self.flash_message('Please fill the required fields', 'error')
            self.redirect('/topic/%s' % id)
            return

        topic = Topic.query.get_first(id=id)
        if not topic:
            self.send_error(404)
            return
        if topic.status == 'delete':
            self.send_error(404)
            return
        if topic.status == 'close':
            self.send_error(403)
            return
        key = hashlib.md5(utf8(content)).hexdigest()
        url = cache.get(key)
        # avoid double submit
        if url:
            self.redirect(url)
            return

        user = self.current_user

        #: create reply
        reply = Reply(topic_id=id, user_id=user.id, content=content)

        #: impact on topic
        topic.reply_count += 1
        topic.impact += reply_impact_for_topic(topic, user.reputation)

        #: update topic's last replyer
        topic.last_reply_by = self.current_user.id
        topic.last_reply_time = datetime.utcnow()

        db.session.add(reply)
        db.session.add(topic)
        db.session.commit()

        num = (topic.reply_count - 1) / 30 + 1
        url = '/topic/%s' % str(id)
        if num > 1:
            url += '?p=%s' % num
        cache.set(key, url, 100)
        self.redirect("%s#reply%s" % (url, topic.reply_count))

        refer = '<a href="/topic/%s#reply-%s">%s</a>' % \
                (topic.id, topic.reply_count, topic.title)
        #: reply notification
        self.create_notification(topic.user_id, content, refer, type='reply')
        #: mention notification
        for username in set(find_mention(content)):
            self.create_notification(username, content, refer,
                                     exception=topic.user_id)

        db.session.commit()
Пример #3
0
 def get(self):
     self.set_header('Content-Type', 'text/xml; charset=utf-8')
     html = cache.get('sitefeed')
     if html is not None:
         self.write(html)
         return
     topics = get_full_topics(Topic.query.order_by('-id')[:20])
     html = self.render_string('feed.xml', topics=topics)
     cache.set('sitefeed', html, 1800)
     self.write(html)
Пример #4
0
 def get(self):
     self.set_header('Content-Type', 'text/xml; charset=utf-8')
     html = cache.get('sitefeed')
     if html is not None:
         self.write(html)
         return
     topics = get_full_topics(Topic.query.order_by('-id')[:20])
     html = self.render_string('feed.xml', topics=topics)
     cache.set('sitefeed', html, 1800)
     self.write(html)
Пример #5
0
    def get(self, slug):
        node = Node.query.get_first(slug=slug)
        if not node:
            self.send_error(404)
            return

        self.set_header('Content-Type', 'text/xml; charset=utf-8')
        key = 'nodefeed:%s' % str(slug)
        html = cache.get(key)
        if html is not None:
            self.write(html)
            return

        topics = Topic.query.filter_by(node_id=node.id)[:20]
        topics = get_full_topics(topics)
        html = self.render_string('node_feed.xml', topics=topics, node=node)
        cache.set(key, html, 1800)
        self.write(html)
Пример #6
0
    def get(self, slug):
        node = Node.query.get_first(slug=slug)
        if not node:
            self.send_error(404)
            return

        self.set_header('Content-Type', 'text/xml; charset=utf-8')
        key = 'nodefeed:%s' % str(slug)
        html = cache.get(key)
        if html is not None:
            self.write(html)
            return

        topics = Topic.query.filter_by(node_id=node.id)[:20]
        topics = get_full_topics(topics)
        html = self.render_string('node_feed.xml', topics=topics, node=node)
        cache.set(key, html, 1800)
        self.write(html)
Пример #7
0
    def post(self, slug):
        node = Node.query.get_first(slug=slug)
        if not node:
            self.send_error(404)
            return
        title = self.get_argument('title', None)
        content = self.get_argument('content', None)
        if not (title and content):
            self.flash_message('Please fill the required fields', 'error')
            self.render('create_topic_form.html', node=node)
            return
        if not self._check_permission(node):
            self.render('create_topic_form.html', node=node)
            return
        #: avoid double submit
        key = hashlib.md5(utf8(content)).hexdigest()
        url = cache.get(key)
        if url:
            self.redirect(url)
            return

        topic = Topic(title=title, content=content)
        topic.node_id = node.id
        topic.user_id = self.current_user.id
        node.topic_count += 1
        db.session.add(topic)
        db.session.add(node)
        db.session.commit()

        url = '/topic/%d' % topic.id
        cache.set(key, url, 100)
        self.redirect(url)

        #: notification
        refer = '<a href="/topic/%s">%s</a>' % (topic.id, topic.title)
        for username in set(find_mention(content)):
            self.create_notification(username, content, refer,
                                     exception=topic.user_id)