def post(self, request): result = Result() wiki = json.loads(request.body.decode()) if wiki is not None: account = wiki.get('account') wiki_id = wiki.get('wiki_id') introduction = wiki.get('introduction') category = wiki.get('category') content = wiki.get('content') img = wiki.get('img') oldWiki = Wiki.objects.filter(id=wiki_id)[0] newWiki = Wiki(title=oldWiki.title, introduction=introduction, category=category, content=content, img_url=img, status=0, hits=oldWiki.hits) newWiki.save() user = User.objects.filter(account=account)[0] newWikiUser = WikiUser(user_account=user, wiki=newWiki, relationship=2) newWikiUser.save() result.setOK() return HttpResponse(json.dumps(result))
def post(self, request): result = Result() category = json.loads(request.body.decode()).get('category') existing = Wiki.objects.filter(category=category, status=1).order_by('time') if len(existing) > 0: result.setStatuscode(1) result.setData("existing", []) temp = set() for i in existing[::-1]: if i.title not in temp: result["existing"].append({ "title": i.title, "id": i.id, "introduction": i.introduction, "img": i.img_url }) temp.add(i.title) else: result.setStatuscode(0) return HttpResponse(json.dumps(result))
def post(self, request): result = Result() body = json.loads(request.body.decode()) account = body.get("account") portrait_url = body.get("portrait_url") query_res = User.objects.filter(account=account) if len(query_res) == 0: return HttpResponse(json.dumps(result)) else: user = query_res[0] print(portrait_url) user.portrait_url = portrait_url user.save(update_fields=["portrait_url"]) result.setOK() return HttpResponse(json.dumps(result))
def post(self, request): result = Result() comm = json.loads(request.body.decode()) account = comm.get('account') wiki_title = comm.get('wiki_title') content = comm.get('content') user = User.objects.filter(account=account)[0] comment_ = Comment(content=content, wiki_title=wiki_title, user_account=user) comment_.save() result.setOK() return HttpResponse(json.dumps(result))
def get(self, request): result = Result() account = request.GET.get("account") query_res = User.objects.filter(account=account).get() result.setOK() result.setData("portrait_url", query_res.portrait_url) return HttpResponse(json.dumps(result))
def post(self, request): result = Result() wiki = json.loads(request.body.decode()) if wiki is not None: account = wiki.get('account') title = wiki.get('title') introduction = wiki.get('introduction') category = wiki.get('category') content = wiki.get('content') img = wiki.get('img') newWiki = Wiki(title=title, introduction=introduction, category=category, content=content, img_url=img, status=0) newWiki.save() user = User.objects.filter(account=account) if len(user) > 0: user = user[0] user.num_of_wiki += 1 user.save() newWikiUser = WikiUser(user_account=user, wiki=newWiki, relationship=1) newWikiUser.save() result.setData('wiki_id', newWiki.id) result.setOK() else: newWiki.delete() newWiki.save() return HttpResponse(json.dumps(result))
def post(self, request): result = Result() body = json.loads(request.body.decode()) account = body.get("account") pwd = body.get("pwd") query_res = User.objects.filter(account=account) if len(query_res) == 0: result.setData("data", "用户名不存在") return HttpResponse(json.dumps(result)) user = authenticate(account=account, password=pwd) if user is not None: result.setOK() login(request, user) else: result.setData("data", "密码不正确") return HttpResponse(json.dumps(result), content_type='application/json')
def post(self, request): result = Result() body = json.loads(request.body.decode()) account = body.get("account") old_pwd = body.get("old_pwd") new_pwd = body.get("new_pwd") query_res = User.objects.filter(account=account) if len(query_res) == 0: result.setData("data", "用户名不存在") return HttpResponse(json.dumps(result)) user = authenticate(account=account, password=old_pwd) if user is not None: user.set_password(new_pwd) user.save() result.setOK() return HttpResponse(json.dumps(result)) else: result.setData("data", "密码不正确") return HttpResponse(json.dumps(result))
def get(self, request): result = Result() account = request.GET.get("account") data = [{ "wiki_id": w.id, "title": w.title, "status": w.status } for w in Wiki.objects.filter(wikiuser__user_account=account, wikiuser__relationship=1).select_related()] result.setData("create", data) data = [{ "wiki_id": w.id, "title": w.title, "status": w.status } for w in Wiki.objects.filter(wikiuser__user_account=account, wikiuser__relationship=2).select_related()] result.setData("modified", data) result.setOK() return HttpResponse(json.dumps(result))
def post(self, request): result = Result() wiki_title = json.loads(request.body.decode()).get('wiki_title') comments = Comment.objects.filter( wiki_title=wiki_title).order_by('time') result.setData("comments", []) for i in comments: result['comments'].append({ "account": i.user_account.account, "content": i.content, "time": str(i.time).split('+')[0] }) result.setOK() return HttpResponse(json.dumps(result))
def post(self, request): body = json.loads(request.body.decode()) account = body.get("account") pwd = body.get("pwd") result = Result() default_partrait = "/media/default.png" if len(User.objects.filter(account=account)) == 0: user = User.objects.create_user(account=account, password=pwd, portrait_url=default_partrait, num_of_wiki=0) user.save() result.setOK() else: result.setStatusCode(-1) return HttpResponse(json.dumps(result))
def get(self, request): result = Result() wikis = Wiki.objects.filter(status=1).order_by('time') if len(wikis): result.setData("wikis", []) temp = set() for i in wikis[::-1]: if i.title not in temp: result['wikis'].append({ "id": i.id, "title": i.title, "img": i.img_url }) result.setOK() return HttpResponse(json.dumps(result))
def get(self, request): result = Result() query_res = User.objects.order_by( F("num_of_wiki").desc()).exclude(is_admin=1) if len(query_res) > 3: data = [{ "account": user.account, "portrait_url": user.portrait_url, "num_of_wiki": user.num_of_wiki } for user in query_res[0:3]] else: data = [{ "account": user.account, "portrait_url": user.portrait_url, "num_of_wiki": user.num_of_wiki } for user in query_res] result.setOK() result.setData("data", data) return HttpResponse(json.dumps(result))
def post(self, request): result = Result() img = request.FILES.get("file") if img is not None: existingFiles = set( map(lambda str: str.split('.')[0], os.listdir(MEDIA_ROOT))) filename = "".join(str(uuid4()).split('-')[:3]) while filename in existingFiles: filename = "".join(str(uuid4()).split('-')[:3]) filename = filename + "." + str(img).split(".")[-1] # 将图片存到media with open(os.path.join(MEDIA_ROOT, filename), "wb") as f: for chunk in img.chunks(): f.write(chunk) result.setData("url", "/media/" + filename) result.setOK() return HttpResponse(json.dumps(result))
def get(self, request): result = Result() logout(request) result.setOK() return HttpResponse(json.dumps(result))
def get(self, request): result = Result() id = request.GET.get('id') wiki = Wiki.objects.filter(id=id) if len(wiki) > 0: wiki = wiki[0] result.setData("title", wiki.title) result.setData("introduction", wiki.introduction) result.setData("content", wiki.content) result.setData("img", wiki.img_url) result.setData("category", wiki.category) wiki.hits = int(wiki.hits) + 1 wiki.save() result.setOK() return HttpResponse(json.dumps(result))