def get_posts_by_user(request): user_id = utils.get_user_id_by_request(request) posts = post_service.get_posts_by_user(UserAccount.objects.get(id=user_id)) serializer = PostSerializer(posts, many=True) return JsonResponse( data=serializer.data, safe=False, status=200 )
def create_post(request): title = request.data.get('title') content = request.data.get('content') user_id = utils.get_user_id_by_request(request) post_obj = Post(title=title, content=content, posted_by=UserAccount.objects.get(id=user_id)) try: result = post_service.create_post(post_obj) serializer = PostSerializer(result, many=False) return JsonResponse( data=serializer.data, safe=False, status=200 ) except Exception as e: logging.exception("message") return JsonResponse( data={ "message": "Unexpected server error" }, status=500 )
def has_permission(self, request, view): user_id = utils.get_user_id_by_request(request) user = UserAccount.objects.get(id=user_id) return user.role == 'Admin'
def me(request): user_id = utils.get_user_id_by_request(request) user = UserAccount.objects.get(id=user_id) serializer = UserSerializer(user, many=False) return JsonResponse(data=serializer.data, safe=False, status=200)
def has_permission(self, request, view): user_id = utils.get_user_id_by_request(request) post_id = view.kwargs.get('post_id') return Post.objects.get( id=post_id).posted_by.id == user_id or user.role == 'Admin'