def post(self, request): '''登录''' flag, data = validate_form(LoginForm, request.jsondata) if not flag: return HttpJsonResponse({ 'message': 'Validation Failed', 'errors': data }, status=422) try: get_user_model().objects.get(username=data['username'], is_active=True) except get_user_model().DoesNotExist: return http_response({ 'message': 'Validation Failed', 'errors': [ {"field": "username", "code": "invalid"}] }, status=422) user = auth.authenticate( username=data['username'], password=data['password'] ) if user: auth.login(request, user) return HttpJsonResponse({'username': user.username}, status=200) else: return http_response({ 'message': 'Validation Failed', 'errors': [ {"field": "password", "code": "invalid"}] }, status=422)
def post(self, request, poi_id, comment_id, reply_id): status, data = validate_form(ReplyForms, request.jsondata) if not status: return response_errors(data) try: poi = Poi.objects.get(poi_id=poi_id) except Poi.DoesNotExist: return http_response(status=404) try: comment = PoiComment.objects.get(comment_id=comment_id) except PoiComment.DoesNotExist: return http_response(status=404) try: reply = PoiCommentReply.objects.get(id=reply_id) except PoiComment.DoesNotExist: return http_response(status=404) data['poi_id'] = poi.poi_id data['comment_id'] = comment.comment_id data['reply_id'] = reply.id reply = PoiCommentReply.objects.create(**data) return http_response({ 'connect': reply.content, 'reply_sub_id': reply.id, 'create_time': datetime_to_timestamp(reply.create_time) }, status=201)
def delete(self, request, poi_id, comment_id): '''删除单条评论''' try: Poi.objects.get(poi_id=poi_id) except Poi.DoesNotExist: return http_response(status=404) try: comment = PoiComment.objects.get(comment_id=comment_id) except PoiComment.DoesNotExist: return http_response(status=404) comment.delete() return http_response(status=204)
def get(self, request, poi_id, comment_id): '''获取单条评论''' try: Poi.objects.get(poi_id=poi_id) except Poi.DoesNotExist: return http_response(status=404) try: comment = PoiComment.objects.get(comment_id=comment_id) except PoiComment.DoesNotExist: return http_response(status=404) result = comment.detail_info() return http_response(result, status=200)
def get(self, request): '''获取标签列表''' key = 'tag_list' if key in cache: tags = cache.get(key) result = [_.detail_info() for _ in tags] return http_response(result, status=200) else: tags = Tag.objects.all() cache.set(key, tags, 1 * 60) result = [_.detail_info() for _ in tags] return http_response(result, status=200)
def get(self, request, poi_id): try: poi = Poi.objects.get(poi_id=poi_id) except Poi.DoesNotExist: return http_response(status=404) # 获取文章评论列表 if not request.GET: comments = poi.poicomment_set.all() result = [_.detail_info() for _ in comments] return http_response(result, status=200) # 获取标签筛选文章评论列表 else: tag_id = request.GET.get('tag') print(tag_id) comments = poi.poicomment_set.all().filter(tag_id=tag_id) result = [_.detail_info() for _ in comments] return http_response(result, status=200)
def post(self, request): '''创建文章''' status, data = validate_form(PoisForms, request.jsondata) if not status: return response_errors(data) data['poi_id'] = uuid.uuid1().hex poi = Poi.objects.create(**data) return http_response({ 'poi_id': poi.poi_id, 'create_time': datetime_to_timestamp(poi.create_time) }, status=201)
def get(self, request, poi_id, tag_id): '''通过标签筛选文章下的评论列表''' try: poi = Poi.objects.get(poi_id=poi_id) except Poi.DoesNotExist: return http_response(status=404) try: tag = Tag.objects.get(tag_id=tag_id) except Tag.DoesNotExist: return http_response(status=404) key = 'tag_comment_list' if key in cache: tag_comments = cache.get(key) result = [_.detail_info() for _ in tag_comments] return http_response({"comments": result}, status=200) else: tag_comments = poi.poicomment_set.all().filter(tag=tag) cache.set(key, tag_comments, 1 * 60) result = [_.detail_info() for _ in tag_comments] return http_response({"comments": result}, status=200)
def post(self, request, poi_id): '''创建文章下评论''' status, data = validate_form(CommentsForms, request.jsondata) if not status: return response_errors(data) try: Poi.objects.get(poi_id=poi_id) except Poi.DoesNotExist: return http_response(status=404) data['comment_id'] = uuid.uuid1().hex data['poi_id'] = poi_id tag = Tag.objects.get(tag_name=data['tag']) data['tag'] = tag comment = PoiComment.objects.create(**data) return http_response({ 'comment_id': comment.comment_id, 'create_time': datetime_to_timestamp(comment.create_time) }, status=201)
def post(self, request): '''创建标签''' status, data = validate_form(TagsForms, request.jsondata) if not status: return response_errors(data) data['tag_id'] = uuid.uuid1().hex tag = Tag.objects.create(**data) return http_response( { 'tag_id': tag.tag_id, 'tag_name': tag.tag_name, 'create_time': datetime_to_timestamp(tag.create_time) }, status=201)