def put(self, request, *args, **kwargs): # 获取参数 PUT = QueryDict(request.body) data = PUT.dict() article_id = data.get("id") title = data.get("title") tags = data.get("tags") article = data.get("article") # 校验参数 try: title = self._check_title(title) tags = self._check_tags(tags) abstract = self._check_abstract(article) article = self._check_article(article) article_obj = self._check_article_exist(article_id) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0])) # 如果数据没有改变,就不动数据库 if article_obj.article != article: article_obj.article = article if article_obj.title != title: article_obj.title = title if article_obj.abstract != abstract: article_obj.abstract = abstract if article_obj.tags != tags: article_obj.tags.clear() for tag in tags: article_obj.tags.add(tag) article_obj.save() return JsonResponse( response_detail( 200, "ok", ArticleDetailSerializer(instance=article_obj, many=False).data))
def delete(self, request, *args, **kwargs): DELETE = QueryDict(request.body) data = DELETE.dict() article_id = data.get("id") try: article = self._check_article_exist(article_id) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0])) article.delete() return JsonResponse(response_detail(200, 'ok'))
def get(self, request, *args, **kwargs): article_id = kwargs.get("detail") try: article = Article.objects.get(id=article_id) ads = ArticleDetailSerializer(instance=article, many=False) result = dict(ads.data) result = self._next_article(result, article_id) result = self._previous_article(result, article_id) return JsonResponse(response_detail(200, 'ok', result), safe=False) except Article.DoesNotExist: return JsonResponse(response_detail(400, "文章不存在"), safe=False)
def post(self, request, *args, **kwargs): """ 管理员登录 """ username = request.POST.get("username") password = request.POST.get("password") try: user = Manager.objects.get(name=username, password=password) token = uuid.uuid4() Token(token=token, user=user).save() return JsonResponse(response_detail(200, 'ok', {"token": token})) except Manager.DoesNotExist: return JsonResponse(response_detail(401))
def get_paginated_response(self, data): return JsonResponse( response_detail(200, "ok", { 'next': self.get_next_link(), 'previous': self.get_previous_link(), 'results': data }))
def post(self, request, *args, **kwargs): """ 需要的参数: - email: 邮箱,必填 - username:用户名,选填,不填默认为 email - article_id: 文章id, 必填 - talking: 评论内容,必填 - father_id: 如果是回复某人的评论的话,需要指定回复的是哪条评论 """ email = request.POST.get("email") username = request.POST.get("username") article_id = request.POST.get("article_id") talking = request.POST.get("talking") father_id = request.POST.get("father_id") try: email = self._check_email(email) username = self._check_username(username) article = self._check_article_id(article_id) talking = self._check_talking(talking) father = self._check_father_id(father_id) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0])) if not username: username = email new_talking = Talking(email=email, username=username, talk=talking, article=article, father=father) new_talking.save() if father: article_url = settings.ARTICLE_DETAIL_URL + str(article.id) try: self._send_email(receiver=father.email, receiver_name=father.username, talked=father.talk, talker=username, article=article.title, detail=talking, article_url=article_url) except AssertionError as e: # return JsonResponse(response_detail(400, e.args[0])) pass return JsonResponse( response_detail( 200, "ok", TalkingSerializer(instance=new_talking, many=False).data))
def delete(self, request, *args, **kwargs): tag_name = QueryDict(request.body).dict().get("tag_name") try: tag = self._check_tag(tag_name) tag.delete() return self.get(request, args, kwargs) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0]))
def post(self, request, *args, **kwargs): tag_name = request.POST.get('name') try: tag_name = self._check_tag_name(tag_name) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0])) Tags(name=tag_name).save() return self.get(request, args, kwargs)
def delete(self, request, *args, **kwargs): DELETE = QueryDict(request.body).dict() talking_id = DELETE.get("talking_id") article_id = DELETE.get("article_id") try: talking = self._check_talking_id(talking_id) article = self._check_article_id(article_id) talking.delete() setattr(request, "GET", {"article_id": article_id}) return self.get(request) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0]))
def post(self, request, *args, **kwargs): title = request.POST.get('title') tags = request.POST.get('tags') article = request.POST.get('article') try: title = self._check_title(title) tags = self._check_tags(tags) article = self._check_article(article) abstract = self._check_abstract(article) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0])) new_article = Article(title=title, article=article, abstract=abstract) new_article.save() for tag in tags: new_article.tags.add(tag) new_article.save() return JsonResponse( response_detail( 200, 'ok', ArticleDetailSerializer(instance=new_article, many=False).data))
def get(self, request, *args, **kwargs): article_id = request.GET.get("article_id") try: article = self._check_article_id(article_id) except AssertionError as e: return JsonResponse(response_detail(400, e.args[0])) talking = Talking.objects.filter(article=article) mcp = MyCursorPagination() paginate_data = mcp.paginate_queryset(queryset=talking, request=request, view=self) serializer_data = TalkingSerializer(instance=paginate_data, many=True) return mcp.get_paginated_response(serializer_data.data)
def get(self, request, *args, **kwargs): tags = Tags.objects.all() tags_serializer = TagsSerializer(instance=tags, many=True) return JsonResponse(response_detail(200, 'ok', tags_serializer.data))