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)
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_reply_mail(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)
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 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_reply_mail(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)})
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)
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"))
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)