def update_account(request, pk): account = get_account_object(pk) serializer = AccountSerializer(account, data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def account_detail(request, pk): """ Retrieve, update or delete a code snippet. """ try: account = Account.objects.get(pk=pk) except Account.DoesNotExist: return HttpResponse(json.dumps({'responseCode': 404}), status=404) if request.method == 'GET': serializer = AccountSerializer(account) data = { 'responseCode': 0, 'responseDesc': 'Success', 'accounts': serializer.data } return JsonResponse(data, safe=False) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = AccountSerializer(account, data=data) if serializer.is_valid(): serializer.save() data = {'responseCode': 0, 'responseDesc': 'Success'} return JsonResponse(data, safe=False) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': account.delete() data = {'responseCode': 0, 'responseDesc': 'Success'} return JsonResponse(data, safe=False)
def account_create(request): """ Creates a new Account and saves it in the database. Args: request (Request): An object containing data needed to create a new Account. Returns: Reponse: An HTTP response indicating that the new Account was successfully saved in the database or that there was an error and the Account object was not created. """ data = request.data if 'loc' in data: loc_data = data.pop('loc') if type( loc_data ) == dict: # If we've got a dict, that means the Location object should be created from the lat/lng loc_serializer = LocationSerializer(data=loc_data) if loc_serializer.is_valid(): loc_obj = loc_serializer.save() data['cur_loc'] = loc_obj.loc_id else: return Response( { 'STATUS': '1', 'REASON': 'LOCATION SERIALIZER ERROR', 'ERRORS': { **loc_serializer.errors } }, status=status.HTTP_400_BAD_REQUEST) else: # For testing purposes, don't create a Location object but just toss in an existing primary key data['cur_loc'] = loc_data acc_serializer = AccountSerializer(data=data) if acc_serializer.is_valid(): acc_obj = acc_serializer.save() return Response( { 'STATUS': '0', 'REASON': 'SUCCESSFULLY CREATED ACCOUNT OBJECT', 'user_id': acc_obj.user_id }, status=status.HTTP_200_OK) else: return Response( { 'STATUS': '1', 'REASON': 'ACCOUNT SERIALIZER ERROR', 'ERRORS': { **acc_serializer.errors } }, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, id, format=None): serializer = AccountSerializer(data=request.data) if serializer.is_valid(): serializer.save() response = Response(serializer.data, status=status.HTTP_201_CREATED) Log.objects.create(request=request.get_full_path, response=response) return response response = Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) Log.objects.create(request=request.get_full_path, response=response) return response
def account_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': accounts = Account.objects.all() serializer = AccountSerializer(accounts, many=True) data = { 'responseCode': 0, 'responseDesc': 'Success', 'accounts': serializer.data } return JsonResponse(data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) print("JSON BODY: " + str(data)) serializer = AccountSerializer(data=data) if serializer.is_valid(): serializer.save() data = {'responseCode': 0, 'responseDesc': 'Success'} return JsonResponse(data, safe=False) return JsonResponse(serializer.errors, status=400)
def account_register(request): if request.method == 'POST': body = JSONParser().parse(request) username = body['username'] password = body['password'] username_available = False password_available = False try: AccountSerializer(Account.objects.get(username__exact=username)) except ObjectDoesNotExist: username_available = True try: AccountSerializer(Account.objects.get(password__exact=password)) except ObjectDoesNotExist: password_available = True if (username_available and password_available): serializer = AccountSerializer(data=body) if (serializer.is_valid()): serializer.save() data = {'responseCode': 0, 'responseDesc': 'Success'} elif (not username_available and not password_available): data = { 'responseCode': 3, 'responseDesc': 'Username and Password not available' } elif (not password_available): data = { 'responseCode': 2, 'responseDesc': 'Password not available' } else: data = { 'responseCode': 1, 'responseDesc': 'Username not available' } return JsonResponse(data, safe=False)
def create_account(request): serializer = AccountSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = AccountSerializer(data=request.data) if serializer.is_valid(): result = serializer.save() return Response(result, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def account_update(request): """ Updates the information in an existing Account within the database. Args: request (Request): An object containing the new data to be placed into the Account object. Returns: Response: An HTTP response that indicates whether the Account was successfully updated or if there was an error. """ data = request.data if 'email' not in data: return Response( { 'STATUS': '1', 'REASON': 'MISSING REQUIRED EMAIL ARGUMENT' }, status=status.HTTP_400_BAD_REQUEST) try: acc_obj = Account.objects.get(email=data['email']) except Account.DoesNotExist: return Response( { 'STATUS': '1', 'REASON': 'NO ACCOUNT EXISTS WITH GIVEN USER_ID' }, status=status.HTTP_400_BAD_REQUEST) if 'loc' in data: loc_data = data.pop('loc') loc_serializer = LocationSerializer(acc_obj.cur_loc, data=loc_data) if loc_serializer.is_valid(): loc_obj = loc_serializer.save() if acc_obj.cur_loc is None: # Since location is optional on registration, we might need to assign a location ID if it's null in the existing object data['cur_loc'] = loc_obj.loc_id else: return Response( { 'STATUS': '1', 'REASON': 'LOCATION SERIALIZER ERROR', 'ERRORS': { **loc_serializer.errors } }, status=status.HTTP_400_BAD_REQUEST) acc_serializer = AccountSerializer(acc_obj, data=data, partial=True) if acc_serializer.is_valid(): acc_serializer.save() return Response( { 'STATUS': '0', 'REASON': 'SUCCESSFULLY UPDATED ACCOUNT' }, status=status.HTTP_200_OK) return Response( { 'STATUS': '1', 'REASON': 'ACCOUNT SERIALIZER ERROR', 'ERRORS': { **acc_serializer.errors } }, status=status.HTTP_400_BAD_REQUEST)
def account_updatelocation(request): """ Updates the location associated with an account. Args: request (Request): The HTTP Request containing the user_id and lat/lng field that needs to be updated. Returns: Reponse: The reponse denoting success or failure of the update. """ data = request.data if 'user_id' not in data: return Response( { 'STATUS': '1', 'REASON': 'MISSING REQUIRED USER_ID ARGUMENT' }, status=status.HTTP_400_BAD_REQUEST) if 'lat' not in data or 'lng' not in data: return Response( { 'STATUS': '1', 'REASON': 'MISSING REQUIRED LAT/LNG ARGUMENTS' }, status=status.HTTP_400_BAD_REQUEST) try: acc_obj = Account.objects.get(user_id=data['user_id']) # No partial=True since we always need to specify a full location loc_serializer = LocationSerializer(acc_obj.cur_loc, data=data) if loc_serializer.is_valid(): loc_obj = loc_serializer.save() # No location currently associated with account [either never existed or nothing currently there], assign the two together if acc_obj.cur_loc is None: acc_serializer = AccountSerializer( acc_obj, data={'cur_loc': loc_obj.loc_id}, partial=True) if acc_serializer.is_valid(): acc_serializer.save() else: return Response( { 'STATUS': '1', 'REASON': 'ACCOUNT SERIALIZER ERROR', 'ERRORS': { **acc_serializer.errors } }, status=status.HTTP_400_BAD_REQUEST) return Response( { 'STATUS': '0', 'REASON': 'ACCOUNT LOCATION SUCCESSFULLY UPDATED' }, status=status.HTTP_200_OK) else: return Response( { 'STATUS': '1', 'REASON': 'LOCATION SERIALIZER ERROR', 'ERRORS': { **loc_serializer.errors } }, status=status.HTTP_400_BAD_REQUEST) except Account.DoesNotExist: return Response( { 'STATUS': '1', 'REASON': 'NO ACCOUNT EXISTS WITH GIVEN USER_ID' }, status=status.HTTP_400_BAD_REQUEST)