# Path: doodle/core/models/category.py
# class Category(SimpleModel):
#     @classmethod
#     def get_all(cls):
#         return cls.redis_client.hgetall(cls.KEY)
# 
#     @classmethod
#     def get_all_paths(cls):
#         category_paths = []
#         categories = Category.get_all()
#         if categories:
#             for category_name, category_parent_path in categories.iteritems():
#                 if category_parent_path:
#                     category_paths.append(u'%s:%s' % (category_parent_path, category_name))
#                 else:
#                     category_paths.append(category_name)
#             category_paths.sort()
#         return category_paths
# 
#     @classmethod
#     def get_all_names_with_paths(cls):
#         category_list = []
#         categories = Category.get_all()
#         if categories:
#             for category_name, category_parent_path in categories.iteritems():
#                 category_name = unicode(category_name, 'utf-8')
#                 if category_parent_path:
#                     category_parent_path = unicode(category_parent_path, 'utf-8')
#                     category_list.append((category_name, u'%s:%s' % (category_parent_path, category_name)))
#                 else:
#                     category_list.append((category_name, category_name))
#             category_list.sort(key=lambda category: category[1])
#         return category_list
# 
#     @classmethod
#     def get_sub_category_names(cls, name):
#         category_names = []
#         categories = Category.get_all()
#         if categories:
#             pattern = re.compile(r'^(.+:)?%s(:.+)?$' % re.escape(name))
#             for category_name, category_parent_path in categories.iteritems():
#                 if category_parent_path and pattern.match(unicode(category_parent_path, 'utf-8')):
#                     category_names.append(category_name)
#         return category_names
# 
#     @classmethod
#     def exists(cls, name):
#         return cls.redis_client.hexists(cls.KEY, name)
# 
#     @classmethod
#     def get_parent_path(cls, name):
#         return cls.redis_client.hget(cls.KEY, name)
# 
#     @classmethod
#     def get_parent_path_and_name(cls, path):
#         parts = path.rsplit(':', 1)
#         if len(parts) == 1:
#             return '', path
#         else:
#             return parts
# 
#     @classmethod
#     def add(cls, path):
#         parent_path, name = cls.get_parent_path_and_name(path)
# 
#         with cls.redis_client.pipeline(True) as pipe:
#             pipe.watch(cls.KEY)
#             existing_path = pipe.hget(cls.KEY, name)
#             if existing_path is not None:
#                 if existing_path != parent_path:
#                     raise IntegrityError('category %s already exists' % name, category_name=name, category_parent_path=existing_path)
#                 else:
#                     return
#             pipe.multi()
#             pipe.hset(cls.KEY, name, parent_path)
#             pipe.execute()
# 
# Path: doodle/common/errors.py
# class IntegrityError(BaseError):
#     pass
# 
# 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 doodle.core.models.category import Category
from doodle.common.errors import IntegrityError
from ..base_handler import AdminHandler
# -*- coding: utf-8 -*-




class CreateCategoryHandler(AdminHandler):
    def get(self):
        self.render('admin/create_category.html', {
            'title': '添加新分类',
            'page': 'create_category'
        })

    def post(self):
        path = self.get_argument('path', None)

# Path: doodle/core/models/category.py
# class Category(SimpleModel):
#     @classmethod
#     def get_all(cls):
#         return cls.redis_client.hgetall(cls.KEY)
# 
#     @classmethod
#     def get_all_paths(cls):
#         category_paths = []
#         categories = Category.get_all()
#         if categories:
#             for category_name, category_parent_path in categories.iteritems():
#                 if category_parent_path:
#                     category_paths.append(u'%s:%s' % (category_parent_path, category_name))
#                 else:
#                     category_paths.append(category_name)
#             category_paths.sort()
#         return category_paths
# 
#     @classmethod
#     def get_all_names_with_paths(cls):
#         category_list = []
#         categories = Category.get_all()
#         if categories:
#             for category_name, category_parent_path in categories.iteritems():
#                 category_name = unicode(category_name, 'utf-8')
#                 if category_parent_path:
#                     category_parent_path = unicode(category_parent_path, 'utf-8')
#                     category_list.append((category_name, u'%s:%s' % (category_parent_path, category_name)))
#                 else:
#                     category_list.append((category_name, category_name))
#             category_list.sort(key=lambda category: category[1])
#         return category_list
# 
#     @classmethod
#     def get_sub_category_names(cls, name):
#         category_names = []
#         categories = Category.get_all()
#         if categories:
#             pattern = re.compile(r'^(.+:)?%s(:.+)?$' % re.escape(name))
#             for category_name, category_parent_path in categories.iteritems():
#                 if category_parent_path and pattern.match(unicode(category_parent_path, 'utf-8')):
#                     category_names.append(category_name)
#         return category_names
# 
#     @classmethod
#     def exists(cls, name):
#         return cls.redis_client.hexists(cls.KEY, name)
# 
#     @classmethod
#     def get_parent_path(cls, name):
#         return cls.redis_client.hget(cls.KEY, name)
# 
#     @classmethod
#     def get_parent_path_and_name(cls, path):
#         parts = path.rsplit(':', 1)
#         if len(parts) == 1:
#             return '', path
#         else:
#             return parts
# 
#     @classmethod
#     def add(cls, path):
#         parent_path, name = cls.get_parent_path_and_name(path)
# 
#         with cls.redis_client.pipeline(True) as pipe:
#             pipe.watch(cls.KEY)
#             existing_path = pipe.hget(cls.KEY, name)
#             if existing_path is not None:
#                 if existing_path != parent_path:
#                     raise IntegrityError('category %s already exists' % name, category_name=name, category_parent_path=existing_path)
#                 else:
#                     return
#             pipe.multi()
#             pipe.hset(cls.KEY, name, parent_path)
#             pipe.execute()
# 
# Path: doodle/common/errors.py
# class IntegrityError(BaseError):
#     pass
# 
# 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/category.py
from doodle.core.models.category import Category
from doodle.common.errors import IntegrityError
from ..base_handler import AdminHandler

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




class CreateCategoryHandler(AdminHandler):
    def get(self):
        self.render('admin/create_category.html', {
            'title': '添加新分类',
            'page': 'create_category'
        })

    def post(self):
        path = self.get_argument('path', None)

        if not path: