Beispiel #1
0
def tag_paging(tag_name, page_num):
    """ TagList Paging.

    used for list diaries with the same tag_name with 5 diaries each page.

    Args:
        tag_name: string
        page_num: page_num

    Return:
        categories: used for sidebar list
        next_page: bool True or False
        diaries: sorted diaries_object by publish_time with 5 each page
        page_num: now page_num
        tag: tag_name used for title
        pages: used for top-nav
        profile: user object
    """
    next_page = False
    diary_num = len(Tag.objects(name=tag_name)[0].diaries)
    if diary_num > int(page_num) * 5:
        next_page = True

    profile = User.objects.first()
    categories = Category.objects.order_by('-publish_time')
    pages = StaticPage.objects.all()
    diaries = sorted(Tag.objects(name=tag_name)[0].diaries,
                     key=attrgetter('publish_time'),
                     reverse=True)[(int(page_num) - 1) * 5 :int(page_num) * 5]

    return render_template('frontend/tag/list.html', diaries=diaries,
                           categories=categories, tag=tag_name,
                           next_page=next_page, page_num=page_num, pages=pages,
                           profile=profile)
Beispiel #2
0
def tag_list(tag_name):
    """ TagList Page.

    used for list diaries with the same tag_name with 5 diaries each page.

    Args:
        tag_name: string

    Return:
        categories: used for sidebar list
        pages: used for top-nav
        diaries: sorted diaries_object by publish_time
        page_num: 1
        tag: tag_name used for title
        profile: user object
    """
    tags = Tag.objects.get_or_404(name=tag_name)
    profile = User.objects.first()
    next_page = False
    diary_num = len(tags.diaries)
    if diary_num > 5:
        next_page = True

    categories = Category.objects.order_by('-publish_time')
    pages = StaticPage.objects.all()
    diaries = sorted(Tag.objects(name=tag_name)[0].diaries,
                     key=attrgetter('publish_time'),
                     reverse=True)[:5]

    return render_template('frontend/tag/list.html', diaries=diaries,
                           categories=categories, tag=tag_name,
                           next_page=next_page, page_num=1, pages=pages,
                           profile=profile)
Beispiel #3
0
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)