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)
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