def kind_list(request): jrs = JsonResponse() if request.method == "POST": name = request.POST.get("name").strip() alias = request.POST.get('alias').strip() introdution = request.POST.get("introdution").strip() if not alias: alias = name try: bk = BlogKind() bk.name = name bk.alias = alias bk.introdution = introdution bk.create_date = datetime.datetime.now() bk.save() jrs.set_success(0, "ok") jrs.url = "/blog-backend/display-blog-list/" jrs.id = bk.id jrs.name = bk.name except: jrs.set_error(300, "create failure") else: # GET bk_obj_list = BlogKind.objects.all() bk_list = [] for bk_obj in bk_obj_list: bk_list.append(bk_obj.name) jrs.set_success(0, "ok") jrs.data = bk_list return HttpResponse(jrs.set_json_pack())
def message_list(request): if request.method == "GET": message_obj_list = MessageList.objects.all().order_by("-id") message_list = [] for message in message_obj_list: message_list.append({ "msg_id": message.id, "content": message.content, "username": message.creator.username, "create_date": message.create_date.strftime("%Y-%m-%d %H:%M:%S"), "adjustment_date": message.adjustment_date.strftime("%Y-%m-%d %H:%M:%S") if message.adjustment_date else None, "head_pic_url": message.creator.head_pic_url }) pass return render(request, "blog_backend/message_list.html", locals()) else: msg_id = request.POST.get("msg_id") MessageList.objects.filter(id=msg_id).delete() jrs = JsonResponse() jrs.set_success(0, "ok") jrs.url = "/blog-backend/message-list/" return HttpResponse(jrs.set_json_pack())
def kind_delete(request): name_list = request.POST.get("name_list") name_list = json.loads(name_list) for name in name_list: BlogKind.objects.filter(name=name).delete() jrs = JsonResponse() jrs.set_success(0, "ok") jrs.url = "/blog-backend/kind-delete/" return HttpResponse(jrs.set_json_pack())
def message(request): """ message mode editor """ if request.method == "GET": msg_id = request.GET.get("id") if msg_id: print(msg_id) ml_obj = MessageList.objects.filter(id=msg_id) return render(request, "blog_backend/message.html", locals()) else: typ = request.POST.get("typ") if typ == "get_content": print(typ) msg_id = request.POST.get("msg_id") ml_obj = MessageList.objects.get(id=msg_id) jrs = JsonResponse() jrs.set_success(0, ml_obj.content) return HttpResponse(jrs.set_json_pack()) else: content = request.POST.get("content") jrs = JsonResponse() users_dict = request.session.get("user") userid = users_dict["id"] id = request.POST.get("msg_id") if id: ml_obj = MessageList.objects.get(id=id) ml_obj.content = content ml_obj.creator_id = userid adjustment_date = datetime.datetime.now() ml_obj.adjustment_date = adjustment_date ml_obj.save() else: create_date = datetime.datetime.now() ml_obj = MessageList() ml_obj.content = content ml_obj.creator_id = userid ml_obj.create_date = create_date ml_obj.save() jrs.set_success(0, "ok") jrs.url = "/blog-backend/message-list/" return HttpResponse(jrs.set_json_pack())
def normal_edit_blog(request): """ normal editor(trash) """ if request.method == "GET": blog_id = request.GET.get("id") if blog_id: try: bl_obj = BlogList.objects.get(id=blog_id) except: return redirect("/blog-backend/display-blog-list/") bk_obj_list = BlogKind.objects.all() tg_obj_list = Tag.objects.all() return render(request, "blog_backend/normal_edit_blog.html", locals()) else: typ = request.POST.get("typ") title = request.POST.get("blog_title") content = request.POST.get("content") kind_id = request.POST.get("kind_id") tag_list = request.POST.get("tag") tag_list = tag_list.split(",") userid = request.session.get("user")["id"] if typ == "create": bk = BlogList() bk.title = title bk.blog_content = content bk.blog_kind_id = kind_id bk.creator_id = userid bk.create_date = datetime.datetime.now() bk.save() else: print("repair") blog_id = request.GET.get("id") bk = BlogList.objects.filter(id=blog_id).first() if bk: bk.title = title bk.blog_content = content bk.blog_kind_id = kind_id bk.creator_id = userid bk.create_date = datetime.datetime.now() bk.save() else: bk = BlogList() bk.title = title bk.blog_content = content bk.blog_kind_id = kind_id bk.creator_id = userid bk.adjustment_date = datetime.datetime.now() bk.save() bk.tag.clear() for tag in tag_list: if tag: obj = Tag.objects.get(id=tag) bk.tag.add(obj) jrs = JsonResponse() jrs.set_success(0, "ok") jrs.url = "/blog-backend/display-blog-list/" return HttpResponse(jrs.set_json_pack())
def md_edit_blog(request): """ markdown mode editor """ if request.method == "GET": blog_id = request.GET.get("id") if blog_id: try: bl_obj = BlogList.objects.get(id=blog_id) except: return redirect("/blog-backend/display-blog-list/") bk_obj_list = BlogKind.objects.all() tg_obj_list = Tag.objects.all() return render(request, "blog_backend/md_edit_blog.html", locals()) else: typ = request.POST.get("typ") title = request.POST.get("blog_title") content = request.POST.get("content") kind_id = request.POST.get("kind_id") tag_list = request.POST.get("tag") tag_list = tag_list.split(",") userid = request.session.get("user")["id"] if typ == "create": bk = BlogList() bk.title = title bk.blog_content = content bk.blog_kind_id = kind_id bk.creator_id = userid bk.create_date = datetime.datetime.now() bk.save() else: blog_id = request.GET.get("id") bk = BlogList.objects.filter(id=blog_id).first() if bk: bk.title = title bk.blog_content = content bk.blog_kind_id = kind_id bk.creator_id = userid bk.create_date = datetime.datetime.now() bk.save() else: bk = BlogList() bk.title = title bk.blog_content = content bk.blog_kind_id = kind_id bk.creator_id = userid bk.adjustment_date = datetime.datetime.now() bk.save() md_file_name = bk.title path = "var/data/blog_files" path = os.path.join(BASE_DIR, path) tempo_path = os.path.join(path, md_file_name) history_file = "%s-%s.md" % ( tempo_path, datetime.datetime.now().strftime("%Y-%m-%d")) if os.path.exists( "%s.md" % tempo_path) and not os.path.exists(history_file): os.rename("%s.md" % tempo_path, history_file) tempo_path = os.path.normpath(tempo_path) with open("%s.md" % tempo_path, "wb") as fp: fp.write(bytes(bk.blog_content, "utf8")) bk.tag.clear() for tag in tag_list: if tag: obj = Tag.objects.get(id=tag) bk.tag.add(obj) jrs = JsonResponse() jrs.set_success(0, "ok") jrs.id = bk.id jrs.url = "/blog-backend/display-blog-list/" return HttpResponse(jrs.set_json_pack())