def get(self, page_id): page_id = int(page_id) pages = Backend('Page').dropdown(show_empty_option=True) page = Backend('Page').find(page_id) fields = extend_service.get_fields_by_type('page', page_id) self.render('admin/page/edit.html', statuses=PAGE_STATUSES, pages=pages, page=page, fields=fields)
class MenuThing(object): def __init__(self): self.page_repo = Backend('Page') def menu(self, page=1): pages = self.page_repo.menu(True) return pages def update(self, sort): for menu_order, pid in enumerate(sort): self.page_repo.update_menu_order(pid, menu_order)
def url(self): segments = [self.slug] parent = self.parent Store = Backend('page') while parent: page = Store.find(parent) if page: segments.insert(0, page.slug) parent = page.parent else: break return '/' + '/'.join(segments)
def get(self): if request.method == 'GET': pages = Backend('Page').dropdown(show_empty_option=True) fields = extend_service.get_fields_by_type('page') self.render('admin/page/add.html', statuses=PAGE_STATUSES, pages=pages, fields=fields)
class CommentThing(object): comment_repo = Backend('Comment') get = lambda self, cid: self.comment_repo.find(cid) def get_by_post_id(self, post_id): return self.comment_repo.find_by_post_id(post_id) def page(self, status, page=1, perpage=10): total = self.comment_repo.count() pages = self.comment_repo.paginate(page, perpage, status) pagination = Paginator(pages, total, page, perpage, '/admin/comment') return pagination def add_comment(self, name, email, content, status, post): comment = Comment(post.pid, name, email, content, status) if self.is_spam(comment): comment.status = 'spam' cid = self.comment_repo.create(comment) comment.cid = cid return comment @classmethod def is_spam(self, comment): for word in site.comment_moderation_keys(): if word.strip() and re.match(word, comment.content, re.I): return True domain = comment.email.split('@')[1] if self.comment_repo.spam_count(domain): return True return False def update_comment(self, comment_id, name, email, content, status): comment = self.get(comment_id) if not comment: return None comment.status = status comment.name = name comment.content = content comment.email = email self.comment_repo.save(comment) return comment def delete(self, comment_id): comment = self.comment_repo.find(comment_id) if not comment: return None return self.comment_repo.delete(comment.cid)
def post(self): _ = self.get_argument parent = _('parent') name = _('name') title = _('title') name = name or title slug = _('slug') content = _('content') status = _('status') pid = int(_('pid')) show_in_menu = int(_('show_in_menu')) show_in_menu = 1 if show_in_menu else 0 redirect_ = _('redirect') validator = Validator() validator.add('duplicate', lambda key: page_service.is_exist_slug(key) == False) (validator.check(title, 'min', text('page.title_missing'), 3).check(slug, 'min', text('page.slug_missing'), 3).check(slug, 'duplicate', text('page.slug_duplicate')).check( slug, 'regex', text('page.slug_invalid'), r'^[0-9_A-Za-z-]+$') #.check(redirect, 'url', text('page.redirect_missing')) ) if validator.errors: self.flash(validator.errors, 'error') pages = Backend('Page').dropdown(show_empty_option=True) fields = extend_service.get_fields_by_type('page') self.render('admin/page/add.html', statuses=PAGE_STATUSES, pages=pages, fields=fields) page = page_service.add_page(parent, name, title, slug, content, status, redirect_, show_in_menu) extend_service.prcoess_field(self, page, 'page') self.redirect(self.reverse_url('page_page'))
def process(self): type = self.type item_id = self.node.pid data = None for extend in Backend('Extend').find_by_type(type): process = getattr(self, 'process_' + extend.field, None) # if process: data = process(extend) if data: meta = Backend('meta').find(type, item_id, extend.eid) if meta: meta.data = data Backend('meta').save(meta) else: meta = Meta(item_id, type, extend.eid, data) Backend('meta').create(meta)
def __init__(self): self.repo = Backend(User.__name__)
class UserThing(object): def __init__(self): self.repo = Backend(User.__name__) get = lambda self, uid: self.repo.find(uid) def auth(self, username, password): user = self.repo.find_by_username(username) if not user: return {'status': 404, 'msg': 'not found'} if user and not user.inactive() and user.check(password): return {'status': 200, 'msg': 'auth success', 'user': user} return {'status': 403, 'msg': 'username or password is invaild'} def page(self, page, perpage=5): total = self.repo.count() users = self.repo.take(page, perpage) page = Paginator(users, total, page, perpage, '/admin/user') return page def get_user_page(self, user): return Paginator([user], 1, 1, 5, '/admin/user') def user_count(self): return self.repo.count() def check_email(self, email): return email_validator(email) def add_user(self, username, email, real_name, password, bio, status='', role='user'): username, real_name = username.strip(), real_name.strip() errors = [] if not re.match(r'^[A-Za-z0-9_]{4,16}$', username): errors.append(text('user.username_missing')) if not re.match(r'^[A-Za-z0-9_]{4,16}$', password): errors.append(text('user.password_invalid')) if not self.check_email(email): errors.append(text('user.email_missing')) if errors: return {'status': 'error', 'errors': errors} if status not in Account.STATUSES: status = 'inactive' if role not in Account.ROLES: role = 'user' if self.repo.find_by_username(username): errors.append(text('user.username_used')) if errors: return {'status': 'error', 'errors': errors} user = User(username, email, real_name, password, None, bio, status, role) user.uid = self.repo.create(user) return {'status': 'ok', 'msg': 'saved', 'user': user} def update_user(self, me, uid, email, real_name, password, newpass1, newpass2, bio, status, role='user'): real_name, newpass1, newpass2, bio = real_name.strip(), newpass1.strip( ), newpass2.strip(), bio.strip() errors = [] if not self.check_email(email): errors.append(text('user.email_missing')) if errors: return {'status': 'error', 'errors': errors} user = self.repo.find(uid) if not user: return {'status': 'error', 'errors': 'User not Found'} if me.uid == user.uid: if re.match(r'[A-Za-z0-9@#$%^&+=]{4,16}', newpass1): if password and newpass1 and newpass1 == newpass2 and user.check( password): user.secure_pass = newpass1 elif newpass1: errors.append(text('users.password_missing')) if self.check_email(email): user_ = self.repo.find_by_email(email) if user_ and user_.uid != user.uid: errors.append(text('user.email_used')) else: user.email = email if errors: return {'status': 'error', 'errors': errors} account = Account.fromUser(user) if me.is_root() or me.uid == uid: if me.is_root() and not account.is_root(): if role in (Account.ADMIN, Account.USER, Account.EDITOR): user.role = role if user.status != status and status in Account.STATUSES: user.status = status if user.real_name != real_name: user.real_name = real_name if user.bio != bio: user.bio = bio self.repo.save(user) return {'status': 'ok', 'msg': 'updated', 'user': user} def delete(self, me, user_id): user = self.repo.find(user_id) if not user: return account = Account.fromUser(user) if account.is_root(): return if me.is_root(): return self.repo.delete(user)
def __init__(self): self.extend_repo = Backend('Extend') self.meta_repo = Backend('Meta')
class ExtendThing(object): def __init__(self): self.extend_repo = Backend('Extend') self.meta_repo = Backend('Meta') def get_by_eid(self, extend_id): return self.extend_repo.find(extend_id) def field_page(self, page=1, perpage=10, url= '/admin/extend/field'): total = self.extend_repo.count() pages = self.extend_repo.paginate(page, perpage, url) pagination = Paginator( pages, total, page, perpage,) return pagination def get_fields_by_type(self, type='post', node_id=None): extends = self.extend_repo.find_by_type(type) if node_id is None: load_meta = lambda extend: Meta(0, type, extend) else: load_meta = lambda extend: self.meta_repo.find( type, node_id, extend.eid) or Meta(0, type, extend) return [Field(extend, load_meta(extend)) for extend in extends] def count(self, key, type): return self.extend_repo.count(key=key, type=type) def create_extend(self, type, key, label, field, attributes): extend = Extend(type, key, label, field, attributes) self.extend_repo.create(extend) return extend def update_extend(self, type, key, label, field, attributes, extend_id): extend = self.get_by_eid(extend_id) if not extend: return None extend.attributes = attributes extend.label = label self.extend_repo.save(extend) return extend def delete_extend(self, extend_id): field = self.extend_repo.find(extend_id) if not field: return None return self.extend_repo.delete(field) def prcoess_field(self, handler, node, type='post'): FieldMananger(handler, node, type).process()
def menus(): return Backend('Page').menu(True)
class PageThing(object): def __init__(self): self.page_repo = Backend(Page.__name__) get = lambda self, pid: self.page_repo.find(pid) def get_by_redirect(self, redirect): return self.page_repo.find_by_redirect(redirect) def get_by_slug(self, slug): return self.page_repo.find_by_slug(slug) def dropdown(self, show_in_menu=True): return self.page_repo.dropdown(show_in_menu) def page(self, status, page=1, perpage=10): total = self.page_repo.count(status) pages = self.page_repo.paginate(page, perpage, status) if status: url = '/admin/page/status/' + status else: url = '/admin/page' pagination = Paginator(pages, total, page, perpage, url) return pagination def delete(self, page_id): page = self.page_repo.find(page_id) if not page: return None return self.page_repo.delete(page.pid) def add_page(self, parent, name, title, slug, content, status, redirect, show_in_menu): redirect = redirect.strip() show_in_menu = 1 if show_in_menu else 0 page = Page(parent, name, title, slug, content, status, redirect, show_in_menu) pid = self.page_repo.create(page) page.pid = pid return page def is_exist_slug(self, slug): return self.page_repo.count_slug(slug) == 1 def update_page(self, parent, name, title, slug, content, status, redirect, show_in_menu, pid): show_in_menu = 1 if show_in_menu else 0 redirect = redirect.strip() page = Page(parent, name, title, slug, content, status, redirect, show_in_menu, pid) self.page_repo.save(page) return page
def _config(self): return Backend('Pair').find('system').json_value()
def category_count(self): return Backend('Post').category_count(self.cid)
class UserThing(object): def __init__(self): self.repo = Backend(User.__name__) get = lambda self, uid: self.repo.find(uid) def auth(self, username, password): user = self.repo.find_by_username(username) if not user: return {'status': 404, 'msg': 'not found'} if user and not user.inactive() and user.check(password): return {'status': 200, 'msg': 'auth success', 'user': user} return {'status': 403, 'msg': 'username or password is invaild'} def page(self, page, perpage=5): total = self.repo.count() users = self.repo.take(page, perpage) page = Paginator(users, total, page, perpage, '/admin/user') return page def get_user_page(self, user): return Paginator([user], 1, 1, 5, '/admin/user') def user_count(self): return self.repo.count() def check_email(self, email): return email_validator(email) def add_user(self, username, email, real_name, password, bio, status='', role='user'): username, real_name = username.strip(), real_name.strip() errors = [] if not re.match(r'^[A-Za-z0-9_]{4,16}$', username): errors.append(text('user.username_missing')) if not re.match(r'^[A-Za-z0-9_]{4,16}$', password): errors.append(text('user.password_invalid')) if not self.check_email(email): errors.append(text('user.email_missing')) if errors: return {'status': 'error', 'errors': errors} if status not in Account.STATUSES: status = 'inactive' if role not in Account.ROLES: role = 'user' if self.repo.find_by_username(username): errors.append(text('user.username_used')) if errors: return {'status': 'error', 'errors': errors} user = User(username, email, real_name, password, None, bio, status, role) user.uid = self.repo.create(user) return {'status': 'ok', 'msg': 'saved', 'user': user} def update_user(self, me, uid, email, real_name, password, newpass1, newpass2, bio, status, role='user'): real_name, newpass1, newpass2, bio = real_name.strip(), newpass1.strip(), newpass2.strip(), bio.strip() errors = [] if not self.check_email(email): errors.append(text('user.email_missing')) if errors: return {'status': 'error', 'errors': errors} user = self.repo.find(uid) if not user: return {'status': 'error', 'errors': 'User not Found'} if me.uid == user.uid: if re.match(r'[A-Za-z0-9@#$%^&+=]{4,16}', newpass1): if password and newpass1 and newpass1 == newpass2 and user.check(password): user.secure_pass = newpass1 elif newpass1: errors.append(text('users.password_missing')) if self.check_email(email): user_ = self.repo.find_by_email(email) if user_ and user_.uid != user.uid: errors.append(text('user.email_used')) else: user.email = email if errors: return {'status': 'error', 'errors': errors} account = Account.fromUser(user) if me.is_root() or me.uid == uid: if me.is_root() and not account.is_root(): if role in (Account.ADMIN, Account.USER, Account.EDITOR): user.role = role if user.status != status and status in Account.STATUSES: user.status = status if user.real_name != real_name: user.real_name = real_name if user.bio != bio: user.bio = bio self.repo.save(user) return {'status': 'ok', 'msg': 'updated', 'user': user} def delete(self, me, user_id): user = self.repo.find(user_id) if not user: return account = Account.fromUser(user) if account.is_root(): return if me.is_root(): return self.repo.delete(user)
def custom_field(self, key): extend = Backend('extend').field('post', key) return extend.value(self.pid, type='post')
def __init__(self): self.page_repo = Backend(Page.__name__)
def cached_user(uid): return Backend('User').find(uid)
def comments(self): return Backend('Comment').find_by_post_id(self.pid)
def __init__(self): self.page_repo = Backend('Page')
def __init__(self, post_repo=None, category_repo=None): self.post_repo = post_repo or Backend('Post') self.category_repo = category_repo or Backend('Category')
class CategoryThing(object): def __init__(self): self.category_repo = Backend(Category.__name__) self.post_repo = Backend('Post') def get_by_cid(self, category_id): return self.category_repo.find(category_id) def dropdown(self): return self.category_repo.dropdown() def page(self, page=1, perpage=10): total = self.category_repo.count() pages = self.category_repo.paginate(page, perpage) pagination = Paginator(pages, total, page, perpage, '/admin/category') return pagination def add_category(self, title, slug, description): category = Category(title, slug, description) cid = self.category_repo.create(category) category.cid = cid return category def update_category(self, category_id, title, slug, description): slug = slug or title category = Category(title, slug, description, category_id) self.category_repo.save(category) return category def delete(self, category_id): if category_id == 1: return category = self.category_repo.find(category_id) if category and self.category_repo.delete(category_id): self.post_repo.reset_post_category(category_id)
def __init__(self): self.category_repo = Backend(Category.__name__) self.post_repo = Backend('Post')
def categories(): return Backend('Category').categories()