def post(self): """ Register a user, generate their token and add them to the database :return: Json Response with the user`s token """ if request.content_type == 'application/json': post_data = request.get_json() name = post_data.get('name') key = post_data.get('key') if not name or len(name) < 4: return response( 'failed', 'Missing name or name is less than 4 characters', 400) if not key: return response( 'failed', 'Missing key: Key is required to generate new token', 400) if not check_key(key): return response('failed', 'Key provided is invalid', 400) token = create_token(name) print("Token Type:", type(token)) print("Token:", token) return response_auth('success', 'Successfully created token', token, 201) return response('failed', 'Content-type must be json', 400)
def edit_resource(resource_id): """ Validate the resource Id. Also check for the data in the json payload. If the data exists update the resource with the new data. :param resource_id: resource Id :return: Http Json response """ if request.content_type == 'application/json': data = request.get_json().get("resource") link = data.get('link') if data.get('link') is not None else None categories = data.get('categories') if data.get( 'categories') is not None else None status = data.get('status') if data.get('status') is not None else None updated_resource = Resource(link, categories, status) if link or categories or status: try: int(resource_id) except ValueError: return response('failed', 'Please provide a valid resource Id', 400) resource = Resource.query.filter_by( resource_id=resource_id).first() if resource: resource.update(updated_resource) return response_for_created_resource(resource, 201) return response( 'failed', 'The resource with Id ' + resource_id + ' does not exist', 404) return response( 'failed', 'No attribute or value was specified, nothing was changed', 400) return response('failed', 'Content-type must be json', 202)
def get(self, user): if request.content_type != 'x-www-form-urlencode': return response('failed', 'Content-type must be x-www-form-urlencode', 400) #get_data = request.get_json() bank_name = request.args.get('bank_name') city = request.args.get('city') limit = request.args.get('limit') offset = request.args.get('offset') print("BNAME:", bank_name, "City:", city, "Limit:", limit, "Offset:", offset) if bank_name is not None and city is not None: count = BankDetails.query.filter( and_( func.lower(BankDetails.bank_name) == func.lower(bank_name), func.lower(BankDetails.city) == func.lower(city))).count() if limit is None and offset is None: bank_details = BankDetails.query.filter( and_( func.lower( BankDetails.bank_name) == func.lower(bank_name), func.lower( BankDetails.city) == func.lower(city))).all() else: if offset is None: offset = 0 else: offset = int(offset) if offset >= count: return response( 'failed', 'offset is greater than or equal to available entries', 200) if limit is None: limit = count - offset else: limit = int(limit) if limit is 0: return response('failed', 'limit should be greater than 0', 200) bank_details = BankDetails.query.filter( and_( func.lower( BankDetails.bank_name) == func.lower(bank_name), func.lower(BankDetails.city) == func.lower( city))).offset(offset).limit(limit).all() if len(bank_details) < 1: return response('failed', 'No matching results', 200) bank_details = bank_details_schema.dump(bank_details).data return {"Bank Details": bank_details}, 200 return response('failed', 'invalid bank_name or city or both', 400)
def get(self, user): if request.content_type != 'x-www-form-urlencode': return response('failed', 'Content-type must be x-www-form-urlencode', 400) #get_data = request.get_json() #ifsc_code = get_data.get('ifsc') ifsc_code = request.args.get('ifsc') if ifsc_code is not None: bank_details = BankDetails.query.filter( func.lower(BankDetails.ifsc) == func.lower(ifsc_code)).first() if not bank_details: return response('failed', 'no matching results', 200) bank_details = bank_detail_schema.dump(bank_details).data return {"Bank Details": bank_details}, 200 return response('failed', 'no ifsc code provided', 400)
def get_resource(resource_id): """ Return a resource. :param resource_id: resource Id :return: """ try: int(resource_id) except ValueError: return response('failed', 'Please provide a valid resource Id', 400) else: resource = Resource.query.filter_by(resource_id=resource_id).first() if resource: return response_for_resource(resource.json()) return response('failed', "Resource not found", 404)
def delete_resource(resource_id): """ Deleting a resource from the database if it exists. :param resource_id: :return: """ try: int(resource_id) except ValueError: return response('failed', 'Please provide a valid resource Id', 400) resource = Resource.query.filter_by(resource_id=resource_id).first() if not resource: abort(404) resource.delete() return response('success', 'Resource deleted successfully', 200)
def handle_400_errors(e): """ Return a custom response for 400 errors. :param e: :return: """ return response('failed', 'Bad Request', 400)
def handle_404_error(e): """ Return a custom message for 404 errors. :param e: :return: """ return response('failed', 'Resource cannot be found', 404)
def create_resource(): """ Create an resource from the sent json data. :return: """ if request.content_type == 'application/json': data = request.get_json().get("resource") link = data.get('link') if data.get('link') is not None else None categories = data.get('categories') if data.get( 'categories') is not None else None status = data.get('status') if data.get('status') is not None else None if link and categories and status: resource = Resource(link, categories, status) resource.save() return response_for_created_resource(resource, 201) return response('failed', 'Missing some resource data, nothing was changed', 400) return response('failed', 'Content-type must be json', 202)