Ejemplo n.º 1
0
    def post(self):
        node_name = self.get_argument('node_name')
        people_id = self.get_argument('people_id')
        node = Node.query.get_first(name=node_name)
        people = People.query.filter_by(id=people_id).first_or_404()
        if not node:
            self.send_error(404)
            return
        
        title = self.get_argument('title', None)
        content = self.get_argument('content', None)
        hidden =  self.get_argument('hidden', None)
        topic = Topic()
        if not title:
            self.flash_message('Please fill the title field', 'error')
            self.render('admin/topic/create_topic.html', topic=topic)
            return
        
        #: avoid double submit
         
        digest = hashlib.md5(utf8(title)).hexdigest()
        key = "r:%d:%s" % (people.id, digest)
        url = autocache_get(key)
        if url:
            self.redirect(url)
            return

        if hidden == 'on':
            hidden = "y"
        else:
            hidden = "n"

        topic.title = title
        topic.content = content
        topic.node_id = node.id
        topic.people_id = people.id
        topic.hidden = hidden
        node.topic_count += 1
        db.session.add(topic)
        db.session.add(node)
        db.session.commit()

        url = '/topic/%d' % topic.id
        # autocache_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(title)):
            self.create_notification(username, title, refer,
                                     exception=topic.people_id)
        for username in set(find_mention(content)):
            self.create_notification(username, content, refer,
                                     exception=topic.people_id)
Ejemplo n.º 2
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()
Ejemplo n.º 3
0
    def post(self, id):
        content = self.get_argument('content', None)
        hidden = self.get_argument('hidden', None)
        people_id = self.get_argument('people_id', None)
        if not content:
            self.flash_message('Please fill the required fields', 'error')
            self.redirect('/topic/%s' % id)
            return

        user = People.query.filter_by(id=people_id).first_or_404()

        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 = "reply:%d:%s" % (user.id, digest)
        url = autocache_get(key)
        # avoid double submit
        if url:
            self.redirect(url)
            return

        

        index_key = 'topic:%d'%topic.id
        index_num = autocache_get(index_key)
        if index_num is None:
            index_num = topic.reply_count
            autocache_set(index_key, index_num)

        index_num = autocache_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'

        #: impact on topic
        topic.reply_count = index_num
        reply.order = index_num

        #: update topic's last replyer
        topic.last_reply_by = 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 = '/admin/topic/%s' % str(id)
        if num > 1:
            url += '?p=%s' % num
        autocache_set(key, url, 100)
        

        refer = '<a href="/topic/%d#reply-%d">%s</a>' % \
                (topic.id, reply.order, 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()
        #TODO: social networks
        self.redirect("%s#reply%s" % (url, topic.reply_count))