class MenuService(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 page_add(): if request.method == 'GET': pages = Backend('page').dropdown(show_empty_option=True) fields = extend_service.get_fields_by_type('page') return render_template('admin/page/add.html', statuses=PAGE_STATUSES, pages=pages, fields=fields) f = request.form parent = f.get('parent') name = f.get('name') title = f.get('title') name = name or title slug = f.get('slug') content = f.get('content') status = f.get('status') pid = f.get('pid', type=int) show_in_menu = f.get('show_in_menu', type=int) show_in_menu = 1 if show_in_menu else 0 redirect_ = f.get('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: flash(validator.errors, 'error') pages = Backend('page').dropdown(show_empty_option=True) fields = extend_service.get_fields_by_type('page') return render_template('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(page, 'page') return redirect(url_for('admin.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)
class StorageService(object): def __init__(self): self.pair_repo = Backend('storage') def site_meta(self): return self.pair_repo.find('system') def update_site_meta(self, sitename, description, site_page, posts_per_page, auto_published_comments, comment_moderation_keys): meta = self.site_meta() config = meta.json_value() try: sitename = sitename or sitename.strip() if sitename: config['sitename'] = sitename description = description or description.strip() if description: config['description'] = description site_page = int(site_page) if site_page >= 0: config['site_page'] = site_page posts_per_page = int(posts_per_page) if posts_per_page: config['posts_per_page'] = posts_per_page auto_published_comments = bool(auto_published_comments) config['auto_published_comments'] = auto_published_comments if comment_moderation_keys is not None: keys = [key.strip() for key in re.split(' +', comment_moderation_keys) if key.strip()] config['comment_moderation_keys'] = keys meta.value = dumps(config) self.pair_repo.update(meta) return True except: return False
def init_user(): """Load user if the auth session validates.""" try: user = _guest if 'auth' in session: uid = session['auth'] user = Backend('user').find(uid) if user is None: session.pop('auth', None) user = _guest except: user = _guest g.user = user
def page_edit(page_id): if request.method == 'GET': pages = Backend('page').dropdown(show_empty_option=True) page = Backend('page').find(page_id) fields = extend_service.get_fields_by_type('page', page_id) return render_template('admin/page/edit.html', statuses=PAGE_STATUSES, pages=pages, page=page, fields=fields) f = request.form parent = f.get('parent') name = f.get('name') title = f.get('title') name = name or title slug = f.get('slug') content = f.get('content') status = f.get('status') show_in_menu = f.get('show_in_menu', type=int, default=0) show_in_menu = 1 if show_in_menu else 0 redirect_ = f.get('redirect') validator = Validator() (validator.check(title, 'min', text('page.title_missing'), 3) #.check(redirect, 'url', text('page.redirect_missing')) ) if validator.errors: flash(validator.errors, 'error') return redirect(url_for('admin.page_edit', page_id=page_id)) page = page_service.update_page(parent, name, title, slug, content, status, redirect_, show_in_menu, page_id) extend_service.prcoess_field(page, 'page') return redirect(url_for('admin.page_edit', page_id=page_id))
class CommentService(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)
class CategoryService(object): def __init__(self): self.category_repo = Backend('category') 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.pair_repo = Backend('storage')
def categories(): return Backend('category').categories()
class PageService(object): def __init__(self): self.page_repo = Backend('page') 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 menus(): return Backend('page').menu(True)
def cached_user(uid): return Backend('user').find(uid)
def category_count(self): return Backend('post').category_count(self.cid)
def __init__(self): self.repo = Backend('user')
def __init__(self): self.page_repo = Backend('page')
def __init__(self): self.extend_repo = Backend('extend') self.meta_repo = Backend('meta')
class ExtendService(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): total = self.extend_repo.count() pages = self.extend_repo.paginate(page, perpage) pagination = Paginator(pages, total, page, perpage, '/admin/extend/field') 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, node, type='post'): FieldMananger(node, type).process()
class UserService(object): def __init__(self): self.repo = Backend('user') 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 login(self, user): session.permanent = True session['auth'] = user.uid def logout(self): session.pop('auth', None) 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 User.STATUSES: status = 'inactive' if role not in User.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, bio, status, role) user.uid = self.repo.create(user) return {'status': 'ok', 'msg': 'saved', 'user': user} def update_user(self, 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'} me = g.user 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.password = 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} if me.is_root() or me.uid == uid: if me.is_root() and not user.is_root(): if role in (User.ADMIN, User.USER, User.EDITOR): user.role = role if user.status != status and status in User.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, user_id): me = g.user user = self.repo.find(user_id) if not user: return if user.is_root(): return if me.is_root(): return self.repo.delete(user)
def __init__(self): self.category_repo = Backend('category') self.post_repo = Backend('post')
def _config(self): return Backend('storage').find('system').json_value()
def custom_field(self, key): extend = Backend('extend').field('post', key) return extend.value(self.pid, type='post')
class ExtendService(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): total = self.extend_repo.count() pages = self.extend_repo.paginate(page, perpage) pagination = Paginator( pages, total, page, perpage, '/admin/extend/field') 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, node, type='post'): FieldMananger(node, type).process()
def value(self, node_id, type='post'): meta = Backend('meta').find(type, node_id, self.eid) meta = meta or Meta(node_id, type, self.eid) return Field(self, meta)
class UserService(object): def __init__(self): self.repo = Backend('user') 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 login(self, user): session.permanent = True session['auth'] = user.uid def logout(self): session.pop('auth', None) 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 User.STATUSES: status = 'inactive' if role not in User.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, bio, status, role) user.uid = self.repo.create(user) return {'status': 'ok', 'msg': 'saved', 'user': user} def update_user(self, 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'} me = g.user 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.password = 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} if me.is_root() or me.uid == uid: if me.is_root() and not user.is_root(): if role in (User.ADMIN, User.USER, User.EDITOR): user.role = role if user.status != status and status in User.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, user_id): me = g.user user = self.repo.find(user_id) if not user: return if user.is_root(): return if me.is_root(): return self.repo.delete(user)
def comments(self): return Backend('comment').find_by_post_id(self.pid)
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')