Ejemplo n.º 1
0
    def post(self, id):
        # for topic reply
        content = self.get_argument('content', None)
        hidden = self.get_argument('hidden', 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
    
        digest = hashlib.md5(utf8(content)).hexdigest()
        key = "r:%d:%s" % (self.current_user.id, digest)
        url = complex_cache.get(key)
        # avoid double submit
        if url:
            self.redirect(url)
            self.flash_message("you send a same message", "message")
            return

        user = self.current_user

        index_key = 'topic:%d'%topic.id
        index_num = complex_cache.get(index_key)
        if index_num is None:
            index_num = topic.reply_count
            complex_cache.set(index_key, index_num)

        index_num = complex_cache.incr(index_key,1)
        #: create reply
        reply = TopicReply(topic_id=id, people_id=user.id, content=content)
        # if hidden == 'on':
        #     reply.hidden = 'y'
        # else:
        #     reply.hidden = 'n'

        # if topic.hidden == 'y' and topic.people_id == user.id:
        #     reply.hidden = 'y'
            
        #: impact on topic
        topic.reply_count = index_num
        reply.order = index_num
        #: 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
        complex_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.people_id, content, refer, type='reply')
        #: mention notification
        for username in set(find_mention(content)):
            self.create_notification(username, content, refer,
                                     exception=topic.people_id)

        db.session.commit()