Example #1
0
File: topic.py Project: benmao/june
    def post(self, id):
        # for topic reply
        content = self.get_argument('content', None)
        if not content:
            self.redirect('/topic/%s' % id)
            return

        topic = self.db.query(Topic).filter_by(id=id).first()
        if not topic:
            self.send_error(404)
            return
        if topic.status == 'close':
            self.send_error(403)
            return

        key = hashlib.md5(utf8(content)).hexdigest()
        url = self.cache.get(key)
        # avoid double submit
        if url:
            self.redirect(url)
            return

        reply = Reply(topic_id=id, user_id=self.current_user.id,
                      content=content)

        # impact on topic
        topic.reply_count += 1
        topic.impact += self._calc_impact(topic)

        # last reply
        topic.last_reply_by = self.current_user.id
        topic.last_reply_time = datetime.utcnow()

        #TODO impact on creator

        self.db.add(reply)
        self.db.add(topic)

        # notifications
        self.create_reply_notify(topic.user_id, topic, content)
        for username in set(find_mention(content)):
            self.create_mention(username, topic, content)

        self.db.commit()
        num = (topic.reply_count - 1) / 30 + 1
        url = '/topic/%s' % str(id)
        if num > 1:
            url += '?p=%s' % num
        self.cache.set(key, url, 100)
        #TODO calculate page, delete the right cache
        self.cache.delete('ReplyListModule:%s:%s' % (str(id), num))
        self.redirect("%s#reply%s" % (url, topic.reply_count))

        # register social services
        content = "%s %s%s#reply-%s" % \
                (content[:70], options.siteurl, url, topic.reply_count)
        networks = self.get_user_social(self.current_user.id)
        register_social(networks, content)
Example #2
0
File: topic.py Project: benmao/june
    def post(self, slug):
        node = self.db.query(Node).filter_by(slug=slug).first()
        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.create_message('Form Error', 'Please fill the required field')
            self.render('topic_form.html', topic=None, node=node)
            return
        if not self._check_permission(node):
            self.create_message(
                'Warning',
                "You have no permission to create a topic in this node")
            self.render('topic_form.html', topic=None, node=node)
            return

        key = hashlib.md5(utf8(content)).hexdigest()
        url = self.cache.get(key)
        # avoid double submit
        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
        self.db.add(topic)
        self.db.add(node)
        self.db.commit()
        url = '/topic/%d' % topic.id
        self.cache.set(key, url, 100)
        key1 = 'TopicListModule:0:1:-impact'
        key2 = 'NodeTopicsModule:%s:1:-impact' % node.id
        key3 = 'UserTopicsModule:%s:1:-impact' % self.current_user.id
        self.cache.delete_multi(['status', key1, key2, key3])
        self.redirect(url)

        # social networks
        content = "%s %s%s" % (title[:70], options.siteurl, url)
        networks = self.get_user_social(self.current_user.id)
        register_social(networks, content)