def delete_user(request, pk): if request.method == 'DELETE': try: user = validations_utils.user_validation( pk) # Validates if user exists or not. except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) user.delete() fire_base = firebase.FirebaseApplication( 'https://vogorentals.firebaseio.com/') result = fire_base.get('/users', None) res = result.keys() urlkey = '' for i in res: if result[str(i)]['id'] == int(pk): urlkey = i rem = fire_base.delete('/users/' + urlkey, None) return Response(messages.DELETED_USER, status=status.HTTP_204_NO_CONTENT) elif request.method == 'GET': try: user = validations_utils.user_validation( pk) # Validates if user exists or not. except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) user_profile_serializer = UserProfileSerializer(user) return Response(user_profile_serializer.data, status=status.HTTP_200_OK)
def cart_detail(request, pk): """ **Get all the product_categories data- Ignore** > GET Returns the product_categories data. * Possible HTTP status codes and JSON response: * `HTTP_200_OK` - Returns the products data: { "id": Integer, "name": String, "description": String, "image": Url, "is_available": Boolean } * `HTTP_500_INTERNAL_SERVER_ERROR` - Internal server error :param pk: :param request: """ data = request.data try: user = validations_utils.user_validation( pk) # Validates if user exists or not. token_user_id = validations_utils.user_token_validation( request.auth.user_id, pk) # Validates user's Token authentication. except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) if request.method == 'GET': if Cart.objects.filter(user_id=user.id).exists( ): # Checks if product_category exists with given id. cart_items = Cart.objects.filter(user_id=user.id) else: return Response(messages.EMPTY_CART, status=status.HTTP_404_NOT_FOUND) if cart_items: cart_serializer = CartSerializer(cart_items, many=True) cart_data = cart_serializer.data data = [] for obj in cart_data: x = utils.get_item_id(obj) item = validations_utils.item_validation(int(x)) obj['name'] = item.name data.append(obj) return Response(data, status=status.HTTP_200_OK) else: return Response(messages.EMPTY_CART, status=status.HTTP_204_NO_CONTENT)
def user_detail(request, pk): """ **Get or change the user profile data- Ignore** > GET Returns the User Profile data. * Requires `user id` which is an integer and taken as primary key to identify user. * Possible HTTP status codes and JSON response: * `HTTP_200_OK` - Returns the User Profile data: { "email": String, "id": Integer, "first_name": String, "last_name": String, "created": String, "contact_no": Integer } * `HTTP_500_INTERNAL_SERVER_ERROR` - Internal server error > PUT ### Update User Profile Data * Requires data that needs to be changed. Any and all of the below fields could be modified in a single PUT request. 1. `first_name`: String 2. `last_name`: String 3. `contact_no`: Integer 4. `email` : String * Requires only the changed data of the user and `email` along the changed parameters. * Possible HTTP status codes and JSON response: * `HTTP_200_OK` - User profile data in JSON format: { "email": String, "id": Integer, "first_name": String, "last_name": String, "created": String, "contact_no": Integer } * `HTTP_500_INTERNAL_SERVER_ERROR` :param pk: :param request: """ data = request.data try: user = validations_utils.user_validation( pk) # Validates if user exists or not. # validations_utils.user_token_validation(request.auth.user_id, pk) # Validates user's Token authentication. except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) if request.method == 'GET': fire_base = firebase.FirebaseApplication( 'https://userfirebase-1e188.firebaseio.com/', None) result = fire_base.get('/users', None) user_profile_serializer = UserProfileSerializer(user) return Response(user_profile_serializer.data, status=status.HTTP_200_OK) elif request.method == 'PUT': try: data = validations_utils.email_validation( data ) # Validates email id, it returns lower-cased email in data. updated_data = utils.update_user(data, user) # Updates user data. return Response(updated_data, status=status.HTTP_200_OK) except ValidationException as e: # Generic exception return Response(e.errors, status=e.status)
def user_change_password(request, pk): """ ### Change Password * While changing password for user registered with email, PUT request requires two fields and their values: * current_password - String * new_password - String * Possible HTTP status codes and JSON response: * `HTTP_200_OK` - If password change was successful: { "user_id": integer, "message": "Password updated successfully" } * `HTTP_401_UNAUTHORIZED` - If user provided incorrect value for current_password: { "message": "Current password is incorrect." } * `HTTP_400_BAD_REQUEST` - If new_password is same as current_password: { "message": "New password cannot be same as current password" } * `HTTP_500_INTERNAL_SERVER_ERROR` - Internal server error :param pk: :param request: """ try: user = validations_utils.user_validation( pk) # Validates if user exists or not. # validations_utils.user_token_validation(request.auth.user_id, pk) # Validates user's Token authentication. except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) if request.method == 'PUT': try: request.data['current_password'] except KeyError: return Response(messages.REQUIRED_CURRENT_PASSWORD, status=status.HTTP_400_BAD_REQUEST) try: new_password = request.data['new_password'] if new_password is None or not re.match(r'[A-Za-z0-9@#$%^&+=]+', new_password): return Response(messages.PASSWORD_NECESSITY, status=status.HTTP_406_NOT_ACCEPTABLE) else: pass except KeyError: return Response(messages.REQUIRED_NEW_PASSWORD, status=status.HTTP_400_BAD_REQUEST) data_keys = request.data.keys() # Change Password will only require current_password and new_password. if 'current_password' in data_keys and 'new_password' in data_keys: current_password = request.data['current_password'] new_password = request.data['new_password'] try: password = utils.change_password(current_password, new_password, user) # Changes password. return Response(password, status=status.HTTP_200_OK) except ValidationException as e: return Response(e.errors, status=e.status)
def cart(request, pk, key): """ **add a new item to cart- Ignore** * Accepts only POST requests dd > POST * Requires following fields of users in JSON format: - Sign Up with Email 1. `email` - Valid email address 2. `password` - String * Possible HTTP status codes and JSON response: * `HTTP_201_CREATED` - When new user registration is done successfully: { "first_name": null or string, "last_name": null or string, "created": date_timestamp, "contact_no": integer, "token": "token string", "user_role": integer, "email": string } * `HTTP_400_BAD_REQUEST` : - Email already used to register one user. Use a different email address { "message": "User with this email already exists." } * `HTTP_400_BAD_REQUEST` - Invalid email address { "message": "Enter a valid email address." } * `HTTP_500_INTERNAL_SERVER_ERROR` - Internal server error * Status code can be used from HTTP header. A separate status field in json data is not provided. :param key: :param pk: :param request: """ data = request.data try: user = validations_utils.user_validation( pk) # Validates if user exists or not. token_user_id = validations_utils.user_token_validation( request.auth.user_id, pk) # Validates user's Token authentication. item = validations_utils.item_validation(key) except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) if request.method == 'POST': try: with transaction.atomic(): try: data['user'] = user.id data['item'] = item.id data = utils.add_item_to_cart( data) # Creates user with request data. return Response(data, status=status.HTTP_201_CREATED) except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) except IntegrityError: return Response(messages.ADD_ITEM_TO_CART_FAILED, status=status.HTTP_500_INTERNAL_SERVER_ERROR) elif request.method == 'DELETE': try: with transaction.atomic(): try: if Cart.objects.filter(user_id=user.id).filter( item_id=item.id).exists(): # Checks if product_category exists with given id. cart_item_obj = Cart.objects.filter( user_id=user.id).filter(item_id=item.id) else: return Response(messages.EMPTY_CART, status=status.HTTP_404_NOT_FOUND) if cart_item_obj: # cart_item = Cart.objects.get(pk=cart_item_obj.id) cart_item_obj.delete() return Response( messages.CART_ITEM_SUCCESSFULLY_DELETED, status=status.HTTP_200_OK) except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) except IntegrityError: return Response(messages.DELETE_ITEM_TO_CART_FAILED, status=status.HTTP_500_INTERNAL_SERVER_ERROR) elif request.method == 'PUT': try: with transaction.atomic(): try: data['user'] = user.id data['item'] = item.id if Cart.objects.filter(user_id=user.id).filter( item_id=item.id).exists(): # Checks if product_category exists with given id. cart_item_obj = Cart.objects.get(user_id=user.id, item_id=item.id) else: return Response(messages.EMPTY_CART, status=status.HTTP_404_NOT_FOUND) try: cart_item = validations_utils.cart_item_validation( cart_item_obj.id) except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) updated_data = utils.update_cart_item( data, cart_item) # Updates cart data. return Response(updated_data, status=status.HTTP_200_OK) except ValidationException as e: # Generic exception return Response(e.errors, status=e.status) except IntegrityError: return Response(messages.UPDATE_ITEM_TO_CART_FAILED, status=status.HTTP_500_INTERNAL_SERVER_ERROR)