def article_index(): """ 文章列表 page 页 from_id 分类(0:全部 1:hj's world 2:fashion) tag_id 标签ID per_page 每页几个 :return: """ conditions = {'deleted_at': None} from_id = request.form.get('from_id', 0, int) if from_id: conditions.update(from_id=from_id) per_page = request.form.get('per_page', 10, int) # tag_id = request.form.get('tag_id') # if tag_id: # conditions.update(tags=tag_id) page = request.form.get('page', 1, int) cs = Content.objects(**conditions).order_by('-created_at') total = cs.count() data = [c.as_dict() for c in cs[per_page * (page - 1): per_page * page]] return res(data=dict(data=data, page=page, total=total))
def get_contact(): if request.method == 'GET': return render_template('contact.html') c = Content.objects(from_id=Content.FROM_CONTACT, deleted_at=None).first() if not c: return res() return res(data=dict(data=c.as_dict()))
def article_details(): """ 文章列表详情 content_id # 文章ID :return: """ content_id = request.form.get('content_id') if not content_id: return res(Errors.PARAMS_REQUIRED) c = Content.objects(id=content_id, deleted_at=None).first() if not c: return res(Errors.NOT_FOUND) pre = Content.objects(from_id=c.from_id, created_at__gt=c.created_at).order_by('created_at').first() next = Content.objects(from_id=c.from_id, created_at__lt=c.created_at).order_by('-created_at').first() return res(data=dict(data=c.as_dict(), pre_data=pre.as_dict() if pre else None, next_data=next.as_dict() if next else None))
def article_delete(): """ 删除文章 article_id 文章ID (可选, 编辑时必填) :return: """ article_id = request.form.get('article_id') if not article_id: return res(Errors.PARAMS_REQUIRED) c = Content.objects(id=article_id, deleted_at=None).first() if not c: return res(Errors.NOT_FOUND) c.deleted_at = now_lambda() c.save() return res()
def article_edit(): """ 文章新建/编辑 content_id # 文章ID (可选, 编辑时必传) title # 文章标题 text # 内容 author_id # 作者 from_id # 大分类 # tags # 标签ID列表 :return: """ if request.method == 'GET': return render_template('edit.html') content_id = request.args.get('content_id') or request.form.get('content_id') title = request.form.get('title') if not title: return res(Errors.PARAMS_REQUIRED) text = request.form.get('text') if not text: return res(Errors.PARAMS_REQUIRED) from_id = request.form.get('from_id', 1, type=int) if not from_id: return res(Errors.PARAMS_REQUIRED) # tags = request.form.getlist('tags[]', []) # if not tags: # return res(Errors.PARAMS_REQUIRED) if from_id == Content.FROM_CONTACT: c = Content.objects(from_id=from_id, deleted_at=None).first() mode = 'update' if not c: mode = 'new' c = Content() else: c = Content.objects(id=content_id, deleted_at=None).first() mode = 'update' if not c: mode = 'new' c = Content() c.title = title c.text = text c.author_id = current_user.id c.from_id = from_id # c.tags = tags c.save() if mode == 'new': emails1 = CommentText.objects(notify_new_post=True).distinct('email') emails2 = Reply.objects(notify_new_post=True).distinct('email') emails = list(set(emails1 + emails2)) if emails: html = """ Hi, I wrote a new article in in <b>HJ Rose Fashion Critic</b>, come here <a href="http://rosehj.com/d/{0}">>><b>click me</b></a> -- <b>RoseHJ</b> """.format(c.id) from utils.mail_utils import Email e = Email('*****@*****.**', [str(e) for e in emails], sender='HJ Rose Fashion Critic', subject='Have a new reply!', body=None, html=html) e.send_email() return res(data=c.as_dict())
def article_edit(): """ 文章新建/编辑 content_id # 文章ID (可选, 编辑时必传) title # 文章标题 text # 内容 author_id # 作者 from_id # 大分类 tags # 标签ID列表 :return: """ content_id = request.form.get('content_id') if not content_id: return res(Errors.PARAMS_REQUIRED) title = request.form.get('title') if not title: return res(Errors.PARAMS_REQUIRED) text = request.form.get('text') if not text: return res(Errors.PARAMS_REQUIRED) from_id = request.form.get('from_id') if not from_id: return res(Errors.PARAMS_REQUIRED) tags = request.form.getlist('tags[]', []) if not tags: return res(Errors.PARAMS_REQUIRED) c = Content.objects(id=content_id, deleted_at=None).first() mode = 'update' if not c: mode = 'new' c = Content() c.title = title c.text = text c.author_id = current_user.id c.from_id = from_id c.tags = tags c.save() if mode == 'new': emails1 = CommentText.objects(notify_new_post=True).distinct('email') emails2 = Reply.objects().distinct( notify_new_post=True).distinct('email') emails = list(set(emails1 + emails2)) body = """ Hi, I have new posts, como here >> http://www.baidu.com """ e = Email('hjrose', emails, sender=None, subject='Have a new reply!', body=body, html=None) e.send_email() return res(data=c.as_dict())