Example #1
0
    def save(self, user, topic=None):
        data = self.data

        node_name = data.pop('node_name', None)
        if not node_name:
            logging.info('no node_name in form data, data: %s', data)

        if not self.node:
            self.node = Node.get(name=node_name)

        if not self.node:
            logging.info('node is None in form instance, self: %s', self)

        content = unicode(data.get('content'))
        data.update({
            'user_id': user.id,
            'node_id': self.node.id,
            'content': strip_xss_tags(content)
        })
        if topic:
            category = 'edit'
            pre_node_id = topic.node_id
            pre_title = topic.title
            pre_content = topic.content
            cur_node_id = data.get('node_id')
            cur_title = data.get('title')
            cur_content = data.get('content')
            changed = 0
            if pre_node_id != cur_node_id:
                topic.node.topic_count -= 1
                self.node.topic_count += 1
                diff_content = '主题节点从' + '<a class="node" href="' +\
                    topic.node.url + '">' + topic.node.name +\
                    '</a>移动到<a class="node" href="' + self.node.url + '">' +\
                    self.node.name + '</a>'
                changed = 1
            if pre_title != cur_title or pre_content != cur_content:
                content1 = '<p><h2>' + pre_title + '</h2></p>' + pre_content
                content2 = '<p><h2>' + cur_title + '</h2></p>' + cur_content
                diff_content = ghdiff.diff(content1, content2, css=None)
                changed = 1
            if changed == 1:
                topic.node_id = cur_node_id
                topic.title = cur_title
                topic.content = cur_content
                History(user_id=user.id,
                        content=diff_content,
                        topic_id=topic.id).save()
            else:
                return topic
        else:
            category = 'create'
            topic = Topic(**data)
        return topic.save(category=category)
Example #2
0
    def save(self, user, topic=None):
        data = self.data

        node_name = data.pop('node_name', None)
        if not node_name:
            logging.info('no node_name in form data, data: %s', data)

        if not self.node:
            self.node = Node.get(name=node_name)

        if not self.node:
            logging.info('node is None in form instance, self: %s', self)

        content = str(data.get('content'))
        data.update({'user_id': user.id, 'node_id': self.node.id,
                     'content': strip_xss_tags(content)})
        if topic:
            category = 'edit'
            pre_node_id = topic.node_id
            pre_title = topic.title
            pre_content = topic.content
            cur_node_id = data.get('node_id')
            cur_title = data.get('title')
            cur_content = data.get('content')
            changed = 0
            if pre_node_id != cur_node_id:
                topic.node.topic_count -= 1
                self.node.topic_count += 1
                diff_content = '主题节点从' + '<a class="node" href="' +\
                    topic.node.url + '">' + topic.node.name +\
                    '</a>移动到<a class="node" href="' + self.node.url + '">' +\
                    self.node.name + '</a>'
                changed = 1
            if pre_title != cur_title or pre_content != cur_content:
                content1 = '<p><h2>' + pre_title + '</h2></p>' + pre_content
                content2 = '<p><h2>' + cur_title + '</h2></p>' + cur_content
                diff_content = ghdiff.diff(content1, content2, css=None)
                changed = 1
            if changed == 1:
                topic.node_id = cur_node_id
                topic.title = cur_title
                topic.content = cur_content
                History(user_id=user.id, content=diff_content,
                        topic_id=topic.id).save()
            else:
                return topic
        else:
            category = 'create'
            topic = Topic(**data)
        return topic.save(category=category)
Example #3
0
 def put(self, topic_id):
     topic_id = int(topic_id)
     topic = Topic.get(id=topic_id)
     if not topic:
         raise tornado.web.HTTPError(404)
     action = self.get_argument('action', None)
     user = self.current_user
     if not action:
         result = {'status': 'info', 'message': '需要 action 参数'}
     if action == 'up':
         if topic.user_id != user.id:
             result = user.up(topic_id=topic_id)
         else:
             result = {'status': 'info', 'message': '不能为自己的主题投票'}
     if action == 'down':
         if topic.user_id != user.id:
             result = user.down(topic_id=topic_id)
         else:
             result = {'status': 'info', 'message': '不能为自己的主题投票'}
     if action == 'collect':
         result = user.collect(topic_id=topic_id)
     if action == 'thank':
         result = user.thank(topic_id=topic_id)
     if action == 'report':
         result = user.report(topic_id=topic_id)
     if self.is_ajax:
         return self.write(result)
     else:
         self.flash_message(**result)
         return self.redirect_next_url()
Example #4
0
 def get(self, topic_id):
     topic_id = int(topic_id)
     page = force_int(self.get_argument('page', 0), 0)
     topic = Topic.get(id=topic_id)
     if not topic:
         raise tornado.web.HTTPError(404)
     category = self.get_argument('category', None)
     if not category:
         category = 'all'
     if category == 'all':
         reply_count = topic.reply_count
         url = topic.url
     elif category == 'hot':
         reply_count = orm.count(
             topic.get_replies(page=None, category=category))
         url = topic.url + '?category=hot'
     page_count = (reply_count + config.reply_paged -
                   1) // config.reply_paged
     if page == 0:
         page = page_count
     replies = topic.get_replies(page=page, category=category)
     form = ReplyForm()
     return self.render("topic/index.html",
                        topic=topic,
                        replies=replies,
                        form=form,
                        category=category,
                        page=page,
                        page_count=page_count,
                        url=url)
Example #5
0
 def put(self, topic_id):
     topic_id = int(topic_id)
     topic = Topic.get(id=topic_id)
     if not topic:
         raise tornado.web.HTTPError(404)
     action = self.get_argument('action', None)
     user = self.current_user
     if not action:
         result = {'status': 'info', 'message':
                   '需要 action 参数'}
     if action == 'up':
         if topic.user_id != user.id:
             result = user.up(topic_id=topic_id)
         else:
             result = {'status': 'info', 'message':
                       '不能为自己的主题投票'}
     if action == 'down':
         if topic.user_id != user.id:
             result = user.down(topic_id=topic_id)
         else:
             result = {'status': 'info', 'message':
                       '不能为自己的主题投票'}
     if action == 'collect':
         result = user.collect(topic_id=topic_id)
     if action == 'thank':
         result = user.thank(topic_id=topic_id)
     if action == 'report':
         result = user.report(topic_id=topic_id)
     if self.is_ajax:
         return self.write(result)
     else:
         self.flash_message(**result)
         return self.redirect_next_url()
Example #6
0
 def get(self, topic_id):
     topic = Topic.get(id=topic_id)
     if not topic:
         return self.redirect_next_url()
     if not topic.histories:
         return self.redirect(topic.url)
     return self.render("topic/history.html",
                        topic=topic, histories=topic.histories)
Example #7
0
 def get(self, topic_id):
     topic = Topic.get(id=topic_id)
     if not topic:
         return self.redirect_next_url()
     if not topic.histories:
         return self.redirect(topic.url)
     return self.render("topic/history.html",
                        topic=topic, histories=topic.histories)
Example #8
0
 def get(self, topic_id):
     topic = Topic.get(id=topic_id)
     if topic and\
        (topic.author == self.current_user or self.current_user.is_admin):
         selected = topic.node.name
     else:
         return self.redirect_next_url()
     choices = Node.get_node_choices()
     kwargs = {'node_name': [selected], 'title': [topic.title],
               'content': [topic.content]}
     form = TopicForm.init(choices=choices, selected=selected, **kwargs)
     return self.render("topic/create.html", form=form, node=topic.node)
Example #9
0
 def get(self, topic_id):
     topic = Topic.get(id=topic_id)
     if topic and\
        (topic.author == self.current_user or self.current_user.is_admin):
         selected = topic.node.name
     else:
         return self.redirect_next_url()
     choices = Node.get_node_choices()
     kwargs = {'node_name': [selected], 'title': [topic.title],
               'content': [topic.content]}
     form = TopicForm.init(choices=choices, selected=selected, **kwargs)
     return self.render("topic/create.html", form=form, node=topic.node)
Example #10
0
    def post(self):
        page = int(self.get_argument('page', 1))
        category = self.get_argument('category', 'index')
        topic_id = int(self.get_argument('topic_id', 0))
        topic = Topic.get(id=topic_id)
        if not topic_id:
            result = {'status': 'error', 'message': '无此主题,不能创建评论'}
            if self.is_ajax:
                return self.write(result)
            else:
                self.flash_message(**result)
                return self.redirect_next_url()
        user = self.current_user
        form = ReplyForm(self.request.arguments)
        if form.validate():
            reply = form.save(user=user, topic=topic)
            reply.put_notifier()
            result = {
                'status': 'success',
                'message': '评论创建成功',
                'content': reply.content,
                'name': reply.author.name,
                'nickname': reply.author.nickname,
                'author_avatar': reply.author.get_avatar(size=48),
                'author_url': reply.author.url,
                'author_name': reply.author.name,
                'author_nickname': reply.author.nickname,
                'reply_url': reply.url,
                'created': reply.created,
                'id': reply.id,
                'floor': reply.floor
            }
            if self.is_ajax:
                return self.write(result)
            self.flash_message(**result)
            return self.redirect(topic.url)

        reply_count = topic.reply_count
        page_count = (reply_count + config.reply_paged -
                      1) // config.reply_paged
        replies = topic.get_replies(page=page, category=category)
        data = dict(form=form,
                    topic=topic,
                    replies=replies,
                    category=category,
                    page=page,
                    page_count=page_count,
                    url=topic.url)
        return self.send_result_and_render(form.result, "topic/index.html",
                                           data)
Example #11
0
 def delete(self, topic_id):
     topic = Topic.get(id=topic_id)
     if not topic:
         return self.redirect_next_url()
     subject = "主题删除通知 - " + config.site_name
     template = (
         '<p>尊敬的 <strong>{nickname}</strong> 您好!</p>'
         '<p>您的主题 <strong>「{topic_title}」</strong>'
         '由于违反社区规定而被删除,我们以邮件的形式给您进行了备份,备份数据如下:</p>'
         '<div class="content">{content}</div>'
     )
     content = template.format(
         nickname=topic.author.nickname,
         topic_title=topic.title,
         content=topic.content
     )
     self.send_email(self, topic.author.email, subject, content)
     replies = topic.replies
     users = []
     content_dict = {}
     for reply in replies:
         if reply.author not in users:
             users.append(reply.author)
             content = '<li>' + reply.content + '</li>'
         else:
             content = content_dict.get(reply.author.name)
             content += '<li>' + reply.content + '</li>'
         content_dict.update({reply.author.name: content})
     for name, content in content_dict.iteritems():
         user = User.get(name=name)
         subject = "评论删除通知 - " + config.site_name
         template = (
             '<p>尊敬的 <strong>{nickname}</strong> 您好!</p>'
             '<p>主题 <strong>「{topic_title}」</strong>'
             '由于某些原因被删除,您在此主题下的评论收到了牵连,遂给您以邮件的形式进行了备份,备份数据如下:</p>'
             '<ul class="content">{content}</ul>'
         )
         content = template.format(
             nickname=user.nickname,
             topic_title=topic.title,
             content=content
         )
         self.send_email(self, user.email, subject, content)
     topic.delete()
     result = {'status': 'success', 'message': '已成功删除'}
     return self.write(result)
Example #12
0
 def delete(self, topic_id):
     topic = Topic.get(id=topic_id)
     if not topic:
         return self.redirect_next_url()
     subject = "主题删除通知 - " + config.site_name
     template = (
         '<p>尊敬的 <strong>{nickname}</strong> 您好!</p>'
         '<p>您的主题 <strong>「{topic_title}」</strong>'
         '由于违反社区规定而被删除,我们以邮件的形式给您进行了备份,备份数据如下:</p>'
         '<div class="content">{content}</div>'
     )
     content = template.format(
         nickname=topic.author.nickname,
         topic_title=topic.title,
         content=topic.content
     )
     self.send_email(self, topic.author.email, subject, content)
     replies = topic.replies
     users = []
     content_dict = {}
     for reply in replies:
         if reply.author not in users:
             users.append(reply.author)
             content = '<li>' + reply.content + '</li>'
         else:
             content = content_dict.get(reply.author.name)
             content += '<li>' + reply.content + '</li>'
         content_dict.update({reply.author.name: content})
     for name, content in content_dict.items():
         user = User.get(name=name)
         subject = "评论删除通知 - " + config.site_name
         template = (
             '<p>尊敬的 <strong>{nickname}</strong> 您好!</p>'
             '<p>主题 <strong>「{topic_title}」</strong>'
             '由于某些原因被删除,您在此主题下的评论收到了牵连,遂给您以邮件的形式进行了备份,备份数据如下:</p>'
             '<ul class="content">{content}</ul>'
         )
         content = template.format(
             nickname=user.nickname,
             topic_title=topic.title,
             content=content
         )
         self.send_email(self, user.email, subject, content)
     topic.delete()
     result = {'status': 'success', 'message': '已成功删除'}
     return self.write(result)
Example #13
0
 def post(self, topic_id):
     topic = Topic.get(id=topic_id)
     if not topic or (topic.author != self.current_user and not self.current_user.is_admin):
         return self.redirect_next_url()
     user = self.current_user
     form = TopicForm(self.request.arguments)
     if form.validate():
         topic = form.save(user=user, topic=topic)
         topic.put_notifier()
         result = {'status': 'success', 'message': '主题修改成功',
                   'topic_url': topic.url}
         if self.is_ajax:
             return self.write(result)
         self.flash_message(**result)
         return self.redirect(topic.url)
     if self.is_ajax:
         return self.write(form.result)
     return self.render("topic/create.html", form=form, node=topic.node)
Example #14
0
 def post(self, topic_id):
     topic = Topic.get(id=topic_id)
     if not topic or (topic.author != self.current_user and not self.current_user.is_admin):
         return self.redirect_next_url()
     user = self.current_user
     form = TopicForm(self.request.arguments)
     if form.validate():
         topic = form.save(user=user, topic=topic)
         topic.put_notifier()
         result = {'status': 'success', 'message': '主题修改成功',
                   'topic_url': topic.url}
         if self.is_ajax:
             return self.write(result)
         self.flash_message(**result)
         return self.redirect(topic.url)
     if self.is_ajax:
         return self.write(form.result)
     return self.render("topic/create.html", form=form, node=topic.node)
Example #15
0
    def post(self):
        page = int(self.get_argument('page', 1))
        category = self.get_argument('category', 'index')
        topic_id = int(self.get_argument('topic_id', 0))
        topic = Topic.get(id=topic_id)
        if not topic_id:
            result = {'status': 'error', 'message': '无此主题,不能创建评论'}
            if self.is_ajax:
                return self.write(result)
            else:
                self.flash_message(**result)
                return self.redirect_next_url()
        user = self.current_user
        form = ReplyForm(self.request.arguments)
        if form.validate():
            reply = form.save(user=user, topic=topic)
            reply.put_notifier()
            result = {'status': 'success', 'message': '评论创建成功',
                      'content': reply.content, 'name': reply.author.name,
                      'nickname': reply.author.nickname, 'author_avatar':
                      reply.author.get_avatar(size=48), 'author_url':
                      reply.author.url, 'author_name': reply.author.name,
                      'author_nickname': reply.author.nickname,
                      'reply_url': reply.url, 'created': reply.created,
                      'id': reply.id, 'floor': reply.floor}
            if self.is_ajax:
                return self.write(result)
            self.flash_message(**result)
            return self.redirect(topic.url)

        reply_count = topic.reply_count
        page_count = (reply_count + config.reply_paged - 1) // config.reply_paged
        replies = topic.get_replies(page=page, category=category)
        data = dict(form=form, topic=topic, replies=replies, category=category, page=page, page_count=page_count,
                    url=topic.url)
        return self.send_result_and_render(form.result, "topic/index.html", data)
Example #16
0
 def get(self, topic_id):
     topic_id = int(topic_id)
     page = force_int(self.get_argument('page', 0), 0)
     topic = Topic.get(id=topic_id)
     if not topic:
         raise tornado.web.HTTPError(404)
     category = self.get_argument('category', None)
     if not category:
         category = 'all'
     if category == 'all':
         reply_count = topic.reply_count
         url = topic.url
     elif category == 'hot':
         reply_count = orm.count(topic.get_replies(page=None,
                                                   category=category))
         url = topic.url + '?category=hot'
     page_count = (reply_count + config.reply_paged - 1) // config.reply_paged
     if page == 0:
         page = page_count
     replies = topic.get_replies(page=page, category=category)
     form = ReplyForm()
     return self.render("topic/index.html", topic=topic, replies=replies,
                        form=form, category=category, page=page,
                        page_count=page_count, url=url)