# Path: doodle/core/models/comment.py
# class Comment(PublicModel):
#     article_id = IntegerProperty()
#     user_id = IntegerProperty()
#     content = StringProperty()
#     format = IntegerProperty()
#     ua = ListProperty()
#     time = DateTimeProperty(auto_now=True)
# 
#     HTML_PATTERN = re.compile('<.*?>|\&.*?\;', re.UNICODE)
#     ROOT_LINK_PATTERN = re.compile(r'<a href="/([^"]*)">')
#     ANCHOR_LINK_PATTERN = re.compile(r'<a href="#([^"]*)">')
#     REPLY_LINK_PATTERN = re.compile(r'<a href="[^"]*#comment-id-(\d+)">')
# 
#     def html_content(self):
#         return format_content(self.content, self.format)
# 
#     def html_content_with_full_url(self, article_url): # for email and ATOM
#         content = self.html_content()
#         content = self.ROOT_LINK_PATTERN.sub(r'<a href="%s/\1">' % CONFIG.MAJOR_HOST_URL, content)
#         content = self.ANCHOR_LINK_PATTERN.sub(r'<a href="%s#\1">' % article_url, content)
#         return content
# 
#     def striped_html_content(self, length=CONFIG.LATEST_COMMENTS_LENGTH):
#         result = self.HTML_PATTERN.sub(' ', self.html_content())
#         return result[:length].strip()
# 
#     @classmethod
#     def get_comments_of_article(cls, article_id, order, page, page_size=CONFIG.COMMENTS_PER_PAGE, public_only=True):
#         comment_ids = ArticleComments.get_by_article_id(article_id, order, page, page_size)
#         if comment_ids:
#             has_next_page = len(comment_ids) == page_size
#             return Comment.get_by_ids(comment_ids, filter_empty=True, public_only=public_only), has_next_page
#         return [], False
# 
#     @classmethod
#     def get_latest_comments(cls, limit=CONFIG.LATEST_COMMENTS_FOR_SIDEBAR):
#         comments_json = cls.redis_client.lrange(cls.KEY, -limit, -1)
#         if comments_json:
#             comments = []
#             for comment_json in reversed(comments_json):
#                 comment = Comment.from_json(comment_json)
#                 if comment.public:
#                     comments.append(comment)
#             if comments:
#                 article_ids = set()
#                 user_ids = set()
#                 for comment in comments:
#                     article_ids.add(comment.article_id)
#                     user_ids.add(comment.user_id)
#                 articles = Article.get_by_ids(article_ids, public_only=True)
#                 if articles:
#                     article_dict = {article.id: article for article in articles}
#                     users = User.get_by_ids(user_ids, filter_empty=True)
#                     if users:
#                         user_dict = {user.id: user for user in users}
#                         return comments, article_dict, user_dict
#         return [], {}, {}
# 
#     def _get_relative_keys(self, inserting=False):
#         if inserting:
#             return [ArticleComments.KEY % self.article_id]
#         return []
# 
#     def _save_relative(self, redis_client, inserting=False):
#         if inserting:
#             ArticleComments.append_comment_to_article(redis_client, self.id, self.article_id)
# 
# Path: doodle/core/models/fragment_cache.py
# class FragmentCache(object):
#     KEY = 'fragment_cache:%s'
# 
#     @classmethod
#     def get(cls, key):
#         return redis_cache_client.get(cls.KEY % key)
# 
#     @classmethod
#     def set(cls, key, value, lifetime=0):
#         redis_cache_client.set(cls.KEY % key, value, ex=lifetime)
#         return True
# 
#     @classmethod
#     def delete(cls, key):
#         return redis_cache_client.delete(cls.KEY % key) == 1
# 
#     @classmethod
#     def has(cls, key):
#         return redis_cache_client.exists(cls.KEY % key)
# 
# Path: doodle/core/redis_client.py

# 
# Path: doodle/views/base_handler.py
# class AdminHandler(UserHandler):
#     def prepare(self):
#         super(AdminHandler, self).prepare()
#         self.set_cache(is_public=False)
# 
#         if CONFIG.ENABLE_HTTPS and not self.is_https:
#             request = self.request
#             if request.version == 'HTTP/1.0':
#                 if request.method in ('GET', 'HEAD'):
#                     self.redirect('https://%s%s' % (request.host, request.uri))
#                 else:
#                     raise HTTPError(403)
#             else:
#                 self.redirect('https://%s%s' % (request.host, request.uri), status=307)
#             return
# 
#         if not self.is_admin:
#             if not self.current_user_id:
#                 request = self.request
#                 if request.method in ('GET', 'HEAD'):
#                     state = Auth.generate(request.uri)
#                     self.set_state_cookie(state)
#                     self.redirect(self.get_login_url(), status=302 if request.version == 'HTTP/1.0' else 303)
#                     return
#                 self.set_session_time_cookie()  # force check user status
#             raise HTTPError(403)


from tornado.web import HTTPError
from doodle.core.models.comment import Comment
from doodle.core.models.fragment_cache import FragmentCache
from doodle.core.redis_client import redis_main_client
from ..base_handler import AdminHandler
# -*- coding: utf-8 -*-





class CommentHandler(AdminHandler):
    def delete(self, comment_id):
        comment = Comment.get_by_id(comment_id)
        if not comment:
            raise HTTPError(404)

        if comment.public:
            comment.public = False
            comment.save(relative=False, transactional=False)
            FragmentCache.delete('sidebar')



# Path: doodle/core/models/comment.py
# class Comment(PublicModel):
#     article_id = IntegerProperty()
#     user_id = IntegerProperty()
#     content = StringProperty()
#     format = IntegerProperty()
#     ua = ListProperty()
#     time = DateTimeProperty(auto_now=True)
# 
#     HTML_PATTERN = re.compile('<.*?>|\&.*?\;', re.UNICODE)
#     ROOT_LINK_PATTERN = re.compile(r'<a href="/([^"]*)">')
#     ANCHOR_LINK_PATTERN = re.compile(r'<a href="#([^"]*)">')
#     REPLY_LINK_PATTERN = re.compile(r'<a href="[^"]*#comment-id-(\d+)">')
# 
#     def html_content(self):
#         return format_content(self.content, self.format)
# 
#     def html_content_with_full_url(self, article_url): # for email and ATOM
#         content = self.html_content()
#         content = self.ROOT_LINK_PATTERN.sub(r'<a href="%s/\1">' % CONFIG.MAJOR_HOST_URL, content)
#         content = self.ANCHOR_LINK_PATTERN.sub(r'<a href="%s#\1">' % article_url, content)
#         return content
# 
#     def striped_html_content(self, length=CONFIG.LATEST_COMMENTS_LENGTH):
#         result = self.HTML_PATTERN.sub(' ', self.html_content())
#         return result[:length].strip()
# 
#     @classmethod
#     def get_comments_of_article(cls, article_id, order, page, page_size=CONFIG.COMMENTS_PER_PAGE, public_only=True):
#         comment_ids = ArticleComments.get_by_article_id(article_id, order, page, page_size)
#         if comment_ids:
#             has_next_page = len(comment_ids) == page_size
#             return Comment.get_by_ids(comment_ids, filter_empty=True, public_only=public_only), has_next_page
#         return [], False
# 
#     @classmethod
#     def get_latest_comments(cls, limit=CONFIG.LATEST_COMMENTS_FOR_SIDEBAR):
#         comments_json = cls.redis_client.lrange(cls.KEY, -limit, -1)
#         if comments_json:
#             comments = []
#             for comment_json in reversed(comments_json):
#                 comment = Comment.from_json(comment_json)
#                 if comment.public:
#                     comments.append(comment)
#             if comments:
#                 article_ids = set()
#                 user_ids = set()
#                 for comment in comments:
#                     article_ids.add(comment.article_id)
#                     user_ids.add(comment.user_id)
#                 articles = Article.get_by_ids(article_ids, public_only=True)
#                 if articles:
#                     article_dict = {article.id: article for article in articles}
#                     users = User.get_by_ids(user_ids, filter_empty=True)
#                     if users:
#                         user_dict = {user.id: user for user in users}
#                         return comments, article_dict, user_dict
#         return [], {}, {}
# 
#     def _get_relative_keys(self, inserting=False):
#         if inserting:
#             return [ArticleComments.KEY % self.article_id]
#         return []
# 
#     def _save_relative(self, redis_client, inserting=False):
#         if inserting:
#             ArticleComments.append_comment_to_article(redis_client, self.id, self.article_id)
# 
# Path: doodle/core/models/fragment_cache.py
# class FragmentCache(object):
#     KEY = 'fragment_cache:%s'
# 
#     @classmethod
#     def get(cls, key):
#         return redis_cache_client.get(cls.KEY % key)
# 
#     @classmethod
#     def set(cls, key, value, lifetime=0):
#         redis_cache_client.set(cls.KEY % key, value, ex=lifetime)
#         return True
# 
#     @classmethod
#     def delete(cls, key):
#         return redis_cache_client.delete(cls.KEY % key) == 1
# 
#     @classmethod
#     def has(cls, key):
#         return redis_cache_client.exists(cls.KEY % key)
# 
# Path: doodle/core/redis_client.py

# 
# Path: doodle/views/base_handler.py
# class AdminHandler(UserHandler):
#     def prepare(self):
#         super(AdminHandler, self).prepare()
#         self.set_cache(is_public=False)
# 
#         if CONFIG.ENABLE_HTTPS and not self.is_https:
#             request = self.request
#             if request.version == 'HTTP/1.0':
#                 if request.method in ('GET', 'HEAD'):
#                     self.redirect('https://%s%s' % (request.host, request.uri))
#                 else:
#                     raise HTTPError(403)
#             else:
#                 self.redirect('https://%s%s' % (request.host, request.uri), status=307)
#             return
# 
#         if not self.is_admin:
#             if not self.current_user_id:
#                 request = self.request
#                 if request.method in ('GET', 'HEAD'):
#                     state = Auth.generate(request.uri)
#                     self.set_state_cookie(state)
#                     self.redirect(self.get_login_url(), status=302 if request.version == 'HTTP/1.0' else 303)
#                     return
#                 self.set_session_time_cookie()  # force check user status
#             raise HTTPError(403)


# Path: doodle/views/admin/comment.py
from tornado.web import HTTPError
from doodle.core.models.comment import Comment
from doodle.core.models.fragment_cache import FragmentCache
from doodle.core.redis_client import redis_main_client
from ..base_handler import AdminHandler

# -*- coding: utf-8 -*-





class CommentHandler(AdminHandler):
    def delete(self, comment_id):
        comment = Comment.get_by_id(comment_id)
        if not comment:
            raise HTTPError(404)

        if comment.public:
            comment.public = False
            comment.save(relative=False, transactional=False)
            FragmentCache.delete('sidebar')



class UserCommentsHandler(AdminHandler):