def category_list(category_id, category_name=None): """Category list page. show 5 diaries in this page. Args: category_id: categoryObjectID category_name: only for SEO Return: next_page: bool True or False page_num: 1 category: category_name used for title diaries: listed 5 diaries in each page categories: used in sidebar pages: used for top-nav profile: user object """ next_page = False diary_num = len(Category.objects(pk=category_id)[0].diaries) if diary_num > 5: next_page = True profile = User.objects.first() categories = Category.objects.order_by('-publish_time') pages = StaticPage.objects.all() diaries = sorted(Category.objects(pk=category_id)[0].diaries, key=attrgetter('publish_time'), reverse=True)[:5] return render_template('frontend/category/list.html', category=category_name, diaries=diaries, categories=categories, next_page=next_page, page_num=1, category_id=category_id, pages=pages, profile=profile)
def del_diary_by_id(self, diary_id): """Diary delete. Also delete diary link from category collection Args: diary_id: objectID Return: None """ diary = Diary.objects(pk=diary_id) Category.objects(name=diary[0].category).update_one(pull__diaries=diary[0]) return diary.delete()
def del_diary_by_id(self, diary_id): """Diary delete. Also delete diary link from category collection Args: diary_id: objectID Return: None """ diary = Diary.objects(pk=diary_id) Category.objects(name=diary[0].category).update_one( pull__diaries=diary[0]) return diary.delete()
def add_place_to_db(page): db.session.add(page) categories = get_all_categories() if page.category not in categories: category_obj = Category(page.category) db.session.add(category_obj) db.session.commit()
def add_category(category_name): categories = get_all_categories() if category_name not in categories: category_obj = Category(category_name) db.session.add(category_obj) """
def add_new_category(self, cat_name): """Category add new. Will check if the cat_name is unique, otherwise will return an error. Args: cat_name: string category name. Return: None """ cat_name = SiteHelpers().secure_filename(cat_name) try: category = Category(name=cat_name) return category.save() except NotUniqueError: return 'category name not unique'
def add_new_category(self, cat_name): """Category add new. Will check if the cat_name is unique, otherwise will return an error. Args: cat_name: string category name. Return: None """ cat_name = SiteHelpers().secure_filename(cat_name) try: category = Category(name=cat_name) return category.save() except NotUniqueError: return "category name not unique"
def diary_del(diary_id): """Admin Diary Delete Action Used for delete Diary.Also del reference Tag and Category. Methods: GET Args: diary_id: diary ObjectID Returns: none """ diary = Diary.objects(pk=diary_id) Category.objects(name=diary[0].category).update_one(pull__diaries=diary[0]) diary.delete() return redirect(url_for("admin.diary_list"))
def add_place_to_db_new_page(title, category = "Other"): page = Page(title, category) db.session.add(page) categories = get_all_categories() if category not in categories: category_obj = Category(category) db.session.add(category_obj) db.session.commit()
def get_category_detail(self, cat_id): """Category detail. will return category detail by category id. Args: cat_name: string category name. Return: category: category object """ return Category.objects(pk=cat_id).first()
def category_paging(category_id, page_num, category_name=None): """Category list page. show 5 diaries in each page. Args: category_id: categoryObjectID category_name: only for SEO page_num: page_num Return: next_page: bool True or False page_num: now page_num category: category_name used for title diaries: listed 5 diaries in each page categories: used in sidebar pages: used for top-nav profile: user object """ next_page = False diary_num = len(Category.objects(pk=category_id)[0].diaries) if diary_num > (int(page_num) - 1) * 5 + 5: next_page = True profile = User.objects.first() categories = Category.objects.order_by('-publish_time') pages = StaticPage.objects.all() diaries = sorted(Category.objects(pk=category_id)[0].diaries, key=attrgetter('publish_time'), reverse=True)[(int(page_num) - 1) * 5:int(page_num) * 5] return render_template('frontend/category/list.html', category=category_name, diaries=diaries, categories=categories, next_page=next_page, page_num=page_num, category_id=category_id, pages=pages, profile=profile)
def create(name, category_id=None): """Creates a Category given the provided values :param name: The string name of the Category object :param category_id: The id to assign to the Category. If not provided, a uuid will be generated :return The created Category object :rtype Category """ if category_id is None: category_id = str(uuid4()) new_category = Category(id=category_id, name=name) db.session.add(new_category) db.session.commit() return new_category
def edit_diary(self, diary_id, title, html, category, tags): """ Edit diary from admin receives title, content(html), tags and cagetory save title, content(html), pure content(further use), tags and cagetory also auto save author as current_user. this method will auto save new Category or Tag if not exist otherwise save in existed none with push only diary_object Args: diary_id: diary_id title: string html: string cagetory: string tags: list Save: title: string html: string content: string without html tags category: string tags: list summary: first 80 characters in content with 3 dots in the end author: current_user_object """ title = SiteHelpers().secure_filename(title) category = SiteHelpers().secure_filename(category) content = SiteHelpers().strip_html_tags(html) splited_tags = tags.split(",") author = UserDispatcher().get_profile() try: diary = Diary.objects(pk=diary_id).first() except: diary = Diary(title=title) old_cat = diary.category old_tags = diary.tags diary.title = title diary.content = content diary.category = category diary.summary = content[0:80] + "..." diary.html = html diary.author = author diary.tags = splited_tags diary.save() a, cat = Category.objects.get_or_create(name=category, defaults={"diaries": [diary]}) if not cat: Category.objects(name=category).update_one(push__diaries=diary) if old_cat is not None: Category.objects(name=old_cat).update_one(pull__diaries=diary) for t in old_tags: Tag.objects(name=t).update_one(pull__diaries=diary) for i in splited_tags: b, tag = Tag.objects.get_or_create(name=i, defaults={"diaries": [diary]}) if not tag: Tag.objects(name=i).update_one(push__diaries=diary) return
def save(self, name): category = Category(name) db.session.add(category) db.session.commit()
def diary_edit(diary_id=None): """ Edit diary from admin receives title, content(html), tags and cagetory save title, content(html), pure content(further use), tags and cagetory also auto save author as current_user. this method will auto save new Category or Tag if not exist otherwise save in existed none with push only diary_object Args: diary_id: diary_id title: string html: string cagetory: string tags: list Save: title: string html: string content: string without html tags category: string tags: list summary: first 80 characters in content with 3 dots in the end author: current_user_object """ if request.method == 'POST' and 'title' and 'content' in request.form: re_helper = ReHelper() title = re_helper.r_slash(request.form["title"]) html = request.form["content"] category = re_helper.r_slash(request.form["category"]) tags = request.form["tags"] ''' save simple data for further use''' parser = MyHTMLParser() parser.feed(html) content = parser.html # the pure content without html tags splited_tags = tags.split(',') author = UserModel.objects.first() try: diary = Diary.objects(pk=diary_id).first() except: diary = Diary(title=title) old_cat = diary.category old_tags = diary.tags diary.title = title diary.content = content diary.category = category diary.summary = content[0:80] + '...' diary.html = html diary.author = author diary.tags = splited_tags diary.save() a, cat = Category.objects.get_or_create(name=category, defaults={'diaries': [diary]}) if not cat: Category.objects(name=category).update_one(push__diaries=diary) if old_cat is not None: Category.objects(name=old_cat).update_one(pull__diaries=diary) for t in old_tags: Tag.objects(name=t).update_one(pull__diaries=diary) for i in splited_tags: b, tag = Tag.objects.get_or_create(name=i, defaults={'diaries': [diary]}) if not tag: Tag.objects(name=i).update_one(push__diaries=diary) return redirect(url_for("admin.diary_list")) else: try: diary = Diary.objects(pk=diary_id).first() except: diary = None categories = Category.objects.all() return render_template('admin/diary/edit.html', diary=diary, categories=categories)
def edit_diary(self, diary_id, title, html, category, tags): """ Edit diary from admin receives title, content(html), tags and cagetory save title, content(html), pure content(further use), tags and cagetory also auto save author as current_user. this method will auto save new Category or Tag if not exist otherwise save in existed none with push only diary_object Args: diary_id: diary_id title: string html: string cagetory: string tags: list Save: title: string html: string content: string without html tags category: string tags: list summary: first 80 characters in content with 3 dots in the end author: current_user_object """ title = SiteHelpers().secure_filename(title) category = SiteHelpers().secure_filename(category) content = SiteHelpers().strip_html_tags(html) splited_tags = tags.split(',') author = UserDispatcher().get_profile() try: diary = Diary.objects(pk=diary_id).first() except: diary = Diary(title=title) old_cat = diary.category old_tags = diary.tags diary.title = title diary.content = content diary.category = category diary.summary = content[0:80] + '...' diary.html = html diary.author = author diary.tags = splited_tags diary.save() a, cat = Category.objects.get_or_create(name=category, defaults={'diaries': [diary]}) if not cat: Category.objects(name=category).update_one(push__diaries=diary) if old_cat is not None: Category.objects(name=old_cat).update_one(pull__diaries=diary) for t in old_tags: Tag.objects(name=t).update_one(pull__diaries=diary) for i in splited_tags: b, tag = Tag.objects.get_or_create(name=i, defaults={'diaries': [diary]}) if not tag: Tag.objects(name=i).update_one(push__diaries=diary) return
def add_category(name, user_id, description=None): if name == '': raise ValueError('Category has to have name') return Category(name=name, user_id=user_id, description=description)