Example #1
0
def diary_prev_or_next(prev_or_next, diary_id):
    """ Diary Next_Or_Prev page function.

    show next or previous diary details.

    Args:
        prev_or_next: string 'next' or 'prev'
        diary_id: ObjectedId

    Return:
        redirect: diary_detail_page
    """
    if prev_or_next == 'next':
        diary = Diary.objects(pk=diary_id).first()
        next_diary = Diary.objects(publish_time__lt=diary.publish_time
                                   ).order_by('-publish_time').first()
    elif prev_or_next == 'prev':
        diary = Diary.objects(pk=diary_id).first()
        next_diary = Diary.objects(publish_time__gt=diary.publish_time).first()

    try:
        return redirect(
            url_for('frontend.diary_detail',
                    diary_id=next_diary.pk,
                    diary_title=next_diary.title))
    except:
        abort(404)
Example #2
0
def diary_prev_or_next(prev_or_next, diary_id):
    """ Diary Next_Or_Prev page function.

    show next or previous diary details.

    Args:
        prev_or_next: string 'next' or 'prev'
        diary_id: ObjectedId

    Return:
        redirect: diary_detail_page
    """
    if prev_or_next == 'next':
        diary = Diary.objects(pk=diary_id).first()
        next_diary = Diary.objects(publish_time__lt=diary.publish_time
                                   ).order_by('-publish_time').first()
    elif prev_or_next == 'prev':
        diary = Diary.objects(pk=diary_id).first()
        next_diary = Diary.objects(publish_time__gt=diary.publish_time
                                   ).first()

    try:
        return redirect(
            url_for('frontend.diary_detail', diary_id=next_diary.pk,
                    diary_title=next_diary.title))
    except:
        abort(404)
Example #3
0
    def reply_comment(self, author, diary_id, email, content):
        diary = Diary.objects(pk=diary_id)
        diary_title = diary.first().title
        comment_em = CommentEm(author=u"博主回复", content=content)
        diary.update_one(push__comments=comment_em)

        """ Save in Comment model for admin manage"""
        comment = Comment(content=content)
        comment.diary = diary.first()
        comment.author = UserDispatcher().get_profile().name
        comment.save(validate=False)

        try:
            send_email_task(
                email,
                u"您评论的文章《"
                + diary_title
                + u"》收到了来自\
                            博主的回复, 请查收",
                content,
                diary_id,
                author,
                diary_title,
            )

            return json.dumps({"success": "true"})
        except Exception as e:
            return json.dumps({"success": "false", "reason": str(e)})
Example #4
0
    def get_diary_list_with_navi(self, tag_name, start=0, end=10, order="-publish_time"):
        """Tag Diary list.
        default query 10 diaries and return if there should be next or prev
        page.

        Args:
            tag_name: string
            start: num defalut 0
            end: num defalut 10
            order: str defalut '-publish_time'

        Return:
            next: boolean
            prev: boolean
            diaries: diaries list
        """

        size = end - start
        prev = next = False
        diaries = Diary.objects(tags=tag_name).order_by(order)[start : end + 1]
        if len(diaries) - size > 0:
            next = True
        if start != 0:
            prev = True

        return prev, next, diaries[start:end]
Example #5
0
    def get_diary_list_with_navi(self,
                                 tag_name,
                                 start=0,
                                 end=10,
                                 order='-publish_time'):
        """Tag Diary list.
        default query 10 diaries and return if there should be next or prev
        page.

        Args:
            tag_name: string
            start: num defalut 0
            end: num defalut 10
            order: str defalut '-publish_time'

        Return:
            next: boolean
            prev: boolean
            diaries: diaries list
        """

        size = end - start
        prev = next = False
        diaries = Diary.objects(tags=tag_name).order_by(order)[start:end + 1]
        if len(diaries) - size > 0:
            next = True
        if start != 0:
            prev = True

        return prev, next, diaries[start:end]
Example #6
0
    def add_comment(self, author, diary_id, email, content):
        diary = Diary.objects(pk=diary_id)
        diary_title = diary.first().title
        comment_em = CommentEm(
            author=author,
            content=content,
            email=email
        )
        diary.update_one(push__comments=comment_em)

        comment = Comment(content=content)
        comment.diary = diary.first()
        comment.email = email
        comment.author = author
        comment.save(validate=False)

        try:
            send_email_task(Config.EMAIL,
                            Config.MAIN_TITLE + u'收到了新的评论, 请查收',
                            content, diary_id, author, diary_title)

            response = make_response(json.dumps({'success': 'true'}))
            response.set_cookie('guest_name', author)
            response.set_cookie('guest_email', email)

            return response
        except Exception as e:
            return str(e)
Example #7
0
    def get_or_create_diary(self, diary_id):
        try:
            diary = Diary.objects(pk=diary_id).first()
        except:
            diary = None
        categories = Category.objects.all()

        return diary, categories
Example #8
0
    def get_or_create_diary(self, diary_id):
        try:
            diary = Diary.objects(pk=diary_id).first()
        except:
            diary = None
        categories = Category.objects.all()

        return diary, categories
Example #9
0
    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()
Example #10
0
    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()
Example #11
0
def diary_detail(diary_id, diary_title=None):
    """ Diary Detail Page.

    show diary details.

    Args:
        diary_id: ObjectedId
        diary_title: string used for SEO Only

    Return:
        diary_detail: diary_object
        categories: used for sidebar
        guest_name: string cookie for guest comment auto complete filed
        guest_email: string cookie for guest comment auto complete filed
        pages: used for top-nav
        profile: user object
        prev: if has previous diary
        next: if has next diary
    """
    profile = User.objects.first()
    diary = Diary.objects(pk=diary_id).first()
    categories = Category.objects.order_by('-publish_time')
    pages = StaticPage.objects.all()

    diary_first = Diary.objects.order_by('-publish_time').first()
    diary_last = Diary.objects.order_by('publish_time').first()

    if diary_first == diary:
        prev = False
    else:
        prev = True

    if diary_last == diary:
        next = False
    else:
        next = True

    guest_name = request.cookies.get('guest_name')
    guest_email = request.cookies.get('guest_email')

    return render_template('frontend/diary/detail.html',
                           diary=diary,
                           categories=categories,
                           guest_name=guest_name,
                           guest_email=guest_email,
                           pages=pages,
                           profile=profile,
                           prev=prev,
                           next=next)
Example #12
0
    def get_by_id(self, diary_id):
        """Diary detail.
        Only return diary detail by diary_id.

        Args:
            diary_id: objectID

        Return:
            diary: diary object
        """
        try:
            diary = Diary.objects(pk=diary_id).first()
        except ValidationError:
            diary = None

        return diary
Example #13
0
    def get_by_id(self, diary_id):
        """Diary detail.
        Only return diary detail by diary_id.

        Args:
            diary_id: objectID

        Return:
            diary: diary object
        """
        try:
            diary = Diary.objects(pk=diary_id).first()
        except ValidationError:
            diary = None

        return diary
Example #14
0
def comment_add():
    """ Comment Add AJAX Post Action.

    designed for ajax post and send reply email for admin

    Args:
        username: guest_name
        did: diary ObjectedId
        email: guest_email
        content: comment content

    Return:
        email_status: success
    """
    if request.method == 'POST':
        name = request.form['username']
        did = request.form['did']
        email = request.form['email']
        content = request.form['comment']

        post = Diary.objects(pk=did)
        diary_title = post[0].title

        commentEm = CommentEm(
            author=name,
            content=content,
            email=email
        )
        post.update_one(push__comments=commentEm)

        comment = Comment(content=content)
        comment.diary = post[0]
        comment.email = email
        comment.author = name
        comment.save(validate=False)

        try:
            send_email_task(Config.EMAIL,
                            Config.MAIN_TITLE + u'收到了新的评论, 请查收',
                            content, did, name, diary_title)

            response = make_response(json.dumps({'success': 'true'}))
            response.set_cookie('guest_name', name)
            response.set_cookie('guest_email', email)
            return response
        except Exception as e:
            return str(e)
Example #15
0
    def del_comment_by_id(self, comment_id):
        """Comment delete by id.
        Also remove comment from diary detail

        Args:
            comment_id: Object_id.

        Return:
            None
        """
        comment = Comment.objects.get_or_404(pk=comment_id)

        diary = Diary.objects(pk=comment.diary.pk)

        diary.update_one(pull__comments={"content": comment.content})

        return comment.delete()
Example #16
0
    def del_comment_by_id(self, comment_id):
        """Comment delete by id.
        Also remove comment from diary detail

        Args:
            comment_id: Object_id.

        Return:
            None
        """
        comment = Comment.objects.get_or_404(pk=comment_id)

        diary = Diary.objects(pk=comment.diary.pk)

        diary.update_one(pull__comments={'content': comment.content})

        return comment.delete()
Example #17
0
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"))
Example #18
0
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"))
Example #19
0
def diary_detail(diary_id, diary_title=None):
    """ Diary Detail Page.

    show diary details.

    Args:
        diary_id: ObjectedId
        diary_title: string used for SEO Only

    Return:
        diary_detail: diary_object
        categories: used for sidebar
        guest_name: string cookie for guest comment auto complete filed
        guest_email: string cookie for guest comment auto complete filed
        pages: used for top-nav
        profile: user object
        prev: if has previous diary
        next: if has next diary
    """
    profile = User.objects.first()
    diary = Diary.objects(pk=diary_id).first()
    categories = Category.objects.order_by('-publish_time')
    pages = StaticPage.objects.all()

    diary_first = Diary.objects.order_by('-publish_time').first()
    diary_last = Diary.objects.order_by('publish_time').first()

    if diary_first == diary:
        prev = False
    else:
        prev = True

    if diary_last == diary:
        next = False
    else:
        next = True

    guest_name = request.cookies.get('guest_name')
    guest_email = request.cookies.get('guest_email')

    return render_template('frontend/diary/detail.html', diary=diary,
                           categories=categories, guest_name=guest_name,
                           guest_email=guest_email, pages=pages, profile=profile,
                           prev=prev, next=next)
Example #20
0
def comment_reply():
    """Comment Reply Action.

    Used for reply guests comment and send notification email.

    Methods:
        POST

    Args:
        author: guest_name
        did: diaryObjectID
        title: diary_title
        email: guest_email
        content: reply content

    Returns:
        status: {success: true/false , reason: Exception}
    """
    if request.method == 'POST':
        author = request.form['author']
        did = request.form['did']
        title = request.form['title']
        email = request.form['email']
        content = request.form['content']

        post = Diary.objects(pk=did)
        commentEm = CommentEm(
            author=u'博主回复',
            content=content,
        )
        post.update_one(push__comments=commentEm)
        ''' Save in Comment model for admin manage'''
        comment = Comment(content=content)
        comment.diary = post[0]
        comment.author = current_user.name
        comment.save(validate=False)

        try:
            send_email_task(
                email, u'您评论的文章《' + title + u'》收到了来自\
                            博主的回复, 请查收', content, did, author, title)
            return json.dumps({'success': 'true'})
        except Exception as e:
            return json.dumps({'success': 'false', 'reason': str(e)})
Example #21
0
def comment_reply():
    """Comment Reply Action.

    Used for reply guests comment and send notification email.

    Methods:
        POST

    Args:
        author: guest_name
        did: diaryObjectID
        title: diary_title
        email: guest_email
        content: reply content

    Returns:
        status: {success: true/false , reason: Exception}
    """
    if request.method == 'POST':
        author = request.form['author']
        did = request.form['did']
        title = request.form['title']
        email = request.form['email']
        content = request.form['content']

        post = Diary.objects(pk=did)
        commentEm = CommentEm(
            author=u'博主回复',
            content=content,
        )
        post.update_one(push__comments=commentEm)

        ''' Save in Comment model for admin manage'''
        comment = Comment(content=content)
        comment.diary = post[0]
        comment.author = current_user.name
        comment.save(validate=False)

        try:
            send_email_task(email, u'您评论的文章《' + title + u'》收到了来自\
                            博主的回复, 请查收', content, did, author, title)
            return json.dumps({'success': 'true'})
        except Exception as e:
            return json.dumps({'success': 'false', 'reason': str(e)})
Example #22
0
def comment_add():
    """ Comment Add AJAX Post Action.

    designed for ajax post and send reply email for admin

    Args:
        username: guest_name
        did: diary ObjectedId
        email: guest_email
        content: comment content

    Return:
        email_status: success
    """
    if request.method == 'POST':
        name = request.form['username']
        did = request.form['did']
        email = request.form['email']
        content = request.form['comment']

        post = Diary.objects(pk=did)
        diary_title = post[0].title

        commentEm = CommentEm(author=name, content=content, email=email)
        post.update_one(push__comments=commentEm)

        comment = Comment(content=content)
        comment.diary = post[0]
        comment.email = email
        comment.author = name
        comment.save(validate=False)

        try:
            send_email_task(Config.EMAIL, Config.MAIN_TITLE + u'收到了新的评论, 请查收',
                            content, did, name, diary_title)

            response = make_response(json.dumps({'success': 'true'}))
            response.set_cookie('guest_name', name)
            response.set_cookie('guest_email', email)
            return response
        except Exception as e:
            return str(e)
Example #23
0
    def reply_comment(self, author, diary_id, email, content):
        diary = Diary.objects(pk=diary_id)
        diary_title = diary.first().title
        comment_em = CommentEm(
            author=u'博主回复',
            content=content,
        )
        diary.update_one(push__comments=comment_em)

        ''' Save in Comment model for admin manage'''
        comment = Comment(content=content)
        comment.diary = diary.first()
        comment.author = UserDispatcher().get_profile().name
        comment.save(validate=False)

        try:
            send_email_task(email, u'您评论的文章《' + diary_title + u'》收到了来自\
                            博主的回复, 请查收', content, diary_id, author,
                            diary_title)

            return json.dumps({'success': 'true'})
        except Exception as e:
            return json.dumps({'success': 'false', 'reason': str(e)})
Example #24
0
    def add_comment(self, author, diary_id, email, content):
        diary = Diary.objects(pk=diary_id)
        diary_title = diary.first().title
        comment_em = CommentEm(author=author, content=content, email=email)
        diary.update_one(push__comments=comment_em)

        comment = Comment(content=content)
        comment.diary = diary.first()
        comment.email = email
        comment.author = author
        comment.save(validate=False)

        try:
            send_email_task(Config.EMAIL, Config.MAIN_TITLE + u'收到了新的评论, 请查收',
                            content, diary_id, author, diary_title)

            response = make_response(json.dumps({'success': 'true'}))
            response.set_cookie('guest_name', author)
            response.set_cookie('guest_email', email)

            return response
        except Exception as e:
            return str(e)
Example #25
0
    def reply_comment(self, author, diary_id, email, content):
        diary = Diary.objects(pk=diary_id)
        diary_title = diary.first().title
        comment_em = CommentEm(
            author=u'博主回复',
            content=content,
        )
        diary.update_one(push__comments=comment_em)
        ''' Save in Comment model for admin manage'''
        comment = Comment(content=content)
        comment.diary = diary.first()
        comment.author = UserDispatcher().get_profile().name
        comment.save(validate=False)

        try:
            send_email_task(
                email, u'您评论的文章《' + diary_title + u'》收到了来自\
                            博主的回复, 请查收', content, diary_id, author,
                diary_title)

            return json.dumps({'success': 'true'})
        except Exception as e:
            return json.dumps({'success': 'false', 'reason': str(e)})
Example #26
0
def comment_del(comment_id):
    """Admin Comment Delete Action

    Used for delete Comment.Del comment from DiaryCommentEm and CommentDB

    Methods:
        GET

    Args:
        comment_id: CommentObjectedID

    Returns:
        none
    """
    comment = Comment.objects.get_or_404(pk=comment_id)

    diary = Diary.objects(pk=comment.diary.pk)

    diary.update_one(pull__comments={'content': comment.content})

    comment.delete()

    return redirect(url_for("admin.comment_list"))
Example #27
0
def comment_del(comment_id):
    """Admin Comment Delete Action

    Used for delete Comment.Del comment from DiaryCommentEm and CommentDB

    Methods:
        GET

    Args:
        comment_id: CommentObjectedID

    Returns:
        none
    """
    comment = Comment.objects.get_or_404(pk=comment_id)

    diary = Diary.objects(pk=comment.diary.pk)

    diary.update_one(pull__comments={'content': comment.content})

    comment.delete()

    return redirect(url_for("admin.comment_list"))
Example #28
0
 def get_next_diary(self, pub_time):
     """Return Next Diary object."""
     return Diary.objects(
         publish_time__gt=pub_time).order_by('-publish_time').first()
Example #29
0
    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
Example #30
0
    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
Example #31
0
 def get_next_diary(self, pub_time):
     """Return Next Diary object."""
     return Diary.objects(publish_time__gt=pub_time).order_by("-publish_time").first()
Example #32
0
 def get_prev_diary(self, pub_time):
     """Return Previous Diary object."""
     return Diary.objects(publish_time__lt=pub_time).order_by("-publish_time").first()
Example #33
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)
Example #34
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)
Example #35
0
 def get_prev_diary(self, pub_time):
     """Return Previous Diary object."""
     return Diary.objects(
         publish_time__lt=pub_time).order_by('-publish_time').first()