def user_login(request): try: email = request.data['email'] password = request.data['password'] try: myuser = MyUser.objects.get(email__exact=email) except MyUser.DoesNotExist: output = "email is not registered......" return exception_handler.error_handling(errMsg=output) if not check_password(password, myuser.password): output = "password is wrong" return exception_handler.error_handling(errMsg=output) user = authenticate(email=email, password=password) refresh = RefreshToken.for_user(user) if user: data = { 'id':user.id, 'email':user.email, 'role_type':user.role_id.role_type, 'refresh': str(refresh), 'access': str(refresh.access_token), } return exception_handler.error_handling(data=data) else: output = 'can not authenticate with the given credentials or the account has been deactivated' return exception_handler.error_handling(errMsg=output) except KeyError: output = 'please provide a email and a password' return exception_handler.error_handling(errMsg=output)
def get_user_access_detail(request, access_id): try: access_obj = SystemAccess.objects.get(id=access_id) except SystemAccess.DoesNotExist: return exception_handler.error_handling(errMsg='id does not exist') serializer = SystemAccessSerializer(access_obj) return exception_handler.error_handling(data=serializer.data)
def create_question(request): result = IsUserWrite(request.user.id) if result: return exception_handler.error_handling(result) request.data['user_id'] = request.user.id serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data) return exception_handler.error_handling(errMsg=serializer.errors)
def poster_create(request): if not request.data: return exception_handler.error_handling(errMsg="invalid access") request.data["update_by"] = request.user.id request.data["created_by"] = request.user.id serializer = PosterSerializer(data=request.data) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data) return exception_handler.error_handling(errMsg=serializer.errors)
def delete_question(request, role_id): result = IsUserDelete(request.user.id) if result: return exception_handler.error_handling(result) try: question = Question.objects.get(id=role_id) except Question.DoesNotExist: exception_handler.error_handling(errMsg='role id does not exist', ) question.delete() return exception_handler.error_handling(data='role id delete succefully', status_code=200)
def get_question(request, question_id): result = IsUserRead(request.user.id) if result: return exception_handler.error_handling(result) try: question = Question.objects.get(id=question_id) except Question.DoesNotExist: exception_handler.error_handling(errMsg='id does not exist', ) serializer = QuestionSerializer(question) return exception_handler.error_handling(data=serializer.data, status_code=200)
def update_user_access(request, access_id): try: access_obj = SystemAccess.objects.get(id=access_id) except SystemAccess.DoesNotExist: return exception_handler.error_handling(errMsg='id does not exist') serializer = SystemAccessSerializer(access_obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data) return exception_handler.error_handling(data=serializer.errors)
def poster_update(request, poster_id): try: poster = MoviePoster.objects.get(id=poster_id) except MoviePoster.DoesNotExist: error = "poster id does not exist" return exception_handler.error_handling(errMsg=error) request.data["update_by"] = request.user.id request.data['update_by_time_stamp'] = datetime.now() serializer = PosterSerializer(poster, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data) return exception_handler.error_handling(errMsg=serializer.errors)
def signup(request): if not request.data: return exception_handler.error_handling(errMsg="invalid access") email = MyUser.objects.filter(email=request.data['email']) if len(email) != 0: return exception_handler.error_handling(errMsg="Email id is already register.") serializer = MyUserSerializer(data=request.data) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data) return exception_handler.error_handling(errMsg=serializer.errors)
def update_question(request, question_id): result = IsUserUpdate(request.user.id) if result: return exception_handler.error_handling(result) try: question = Question.objects.get(id=question_id) except Question.DoesNotExist: return exception_handler.error_handling(errMsg='id does not exist', ) serializer = QuestionSerializer(question, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data, status_code=200) return exception_handler.error_handling(serializer.errors)
def get_movies(request): search = request.query_params.get('search') movie = Movie.objects.filter(Q(title__icontains=search)) serializer = MovieSerializer(movie, many=True, context={"request": request}) return exception_handler.error_handling(data=serializer.data)
def poster_delete(request, poster_id): try: poster = MoviePoster.objects.get(id=poster_id) except MoviePoster.DoesNotExist: error = "poster id does not exist" return exception_handler.error_handling(errMsg=error) poster.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get_questions(request): question = Question.objects.all().values() return exception_handler.error_handling(data=question, status_code=200)
def get_movie_list(request): movie = Movie.objects.all() serializer = MovieSerializer(movie, many=True, context={"request": request}) return exception_handler.error_handling(data=serializer.data)
def post(self, request): serializer = RoleSerializer(data=request.data) if serializer.is_valid(): serializer.save() return exception_handler.error_handling(data=serializer.data, ) return exception_handler.error_handling(errMsg=serializer.errors)