def put(self, category_id): """Method for updating a category""" category_name = request.data['category_name'] args = {'category_name':category_name} # parser = reqparse.RequestParser() # parser.add_argument('category_name', type = str) # args = parser.parse_args() if value_is_empty(args): return {'error': 'all fields must be filled'}, 422 category_name = args['category_name'] auth = request.headers.get('x-access-token') userid = User.verify_auth_token(auth) cat = RecipeCategory.query.filter_by(user=userid, category_id = category_id).first() if category_name.isspace() or category_name!=category_name.strip(): return ({'message':'no spaces allowed'}) if not is_category_name_valid(category_name): return {'message':'invalid input'}, 400 if cat is None: return ({'message':'category doesnot exist'}), 404 new_category = RecipeCategory.query.filter_by(category_name=category_name).first() if new_category: return ({"message":'category name already used please choose another one'}), 400 cat.category_name = category_name db.session.commit() return ({'message':'category edited', 'category name':cat.category_name, 'category_id':category_id}), 200
def get(self): """A method to get categories created by a user""" auth = request.headers.get('x-access-token') userid = User.verify_auth_token(auth) results = [] categories = RecipeCategory.query.filter_by(user=userid).all() # results.append(result1) if categories is []: return jsonify({'message':'no categories to display' }), 404 else: page = page = request.args.get('page', 1, type=int) q = request.args.get('q', None, type=str) items, nex, pagination, previous = paginate_categories(page, q, userid) for item in items: result1 = { 'category_name':item.category_name, 'category_id':item.category_id, 'next':nex, 'count':pagination.total, 'previou':previous, 'pagenumber':pagination.page } results.append(result1) return ({'results':results, 'count':pagination.total, 'next':nex}, 200)
def post(self): """A method to create a category""" # parser = reqparse.RequestParser() # parser.add_argument('category_name', type = str) category_name = request.data['category_name'] args = {'category_name':category_name} category_name = args['category_name'] if value_is_empty(args): return {'error': 'all fields must be filled'}, 422 category_name = args['category_name'] auth = request.headers.get('x-access-token') userid = User.verify_auth_token(auth) category1 = RecipeCategory.query.filter_by( category_name = category_name, user = userid).first() if category_name.isspace() or category_name!=category_name.strip(): return ({'message':'no spaces allowed'}) if not is_category_name_valid(category_name): return {'message':'invalid input use format peas'}, 400 if category1 is None: new_category = RecipeCategory(category_name = category_name, user = userid) new_category.save_category() response = {'message':"Category created", 'category_name':new_category.category_name} return make_response(jsonify(response), 201) else: response = ({'message': "Category already exists"}) return (response, 401)
def post(self): """A function to allow users to log out""" authorisation = request.headers.get('x-access-token') userid = User.verify_auth_token(authorisation) if not authorisation: return ({"message": "You are not logged in"}), 400 blacklist = Blacklist(userid, authorisation) blacklist.save_token() return ({'message': 'logged out'}), 200
def delete(self, category_id): """Method for deleting categories""" auth = request.headers.get('x-access-token') userid = User.verify_auth_token(auth) cat = RecipeCategory.query.filter_by(user=userid, category_id = category_id).first() if cat is None: return ({'message':'category doesnot exist'}), 404 cat_delete = cat.category_name db.session.delete(cat) db.session.commit() return ({'message': 'successfully deleted', 'deleted_category': cat_delete}), 200
def post(self): """A function that will allow a user to log in""" username = request.data['username'] password = request.data['password'] # args = parser.parse_args() args = {'username': username, 'password': password} username = args['username'] password = args['password'] users = User.query.filter_by(username=username).first() if not users: response = {'message': 'user doesnot exist'} return make_response(jsonify(response), 401) user = User.verify_password(username, password) if user: token = g.user.generate_auth_token() user = g.user response = { 'message': 'You have successfully logged in', 'token': token.decode('ascii') } return make_response(jsonify(response), 200) responses = {'message': 'invalid credentials'} return make_response(jsonify(responses), 401)
def get(self): """Method to search for categories by name""" results=[] auth = request.headers.get('x-access-token') userid = User.verify_auth_token(auth) parser = reqparse.RequestParser() parser.add_argument('q', type = str) parser.add_argument('per_page', type = int, default=2) parser.add_argument('page', type = int, default=1) # q = request.data['q'] # per_page = request.data['per_page'] # page = request.data['page'] # args = {'q':q, 'per_page':per_page, 'page':page} args = parser.parse_args() q = args['q'] per_page = args['per_page'] page = args['page'] if not q: return ({"message":"search item not provided"}), 400 items, nex, pagination, previous = paginate_categories(page, q, userid) for item in items: result1 = { 'category_name':item.category_name, 'category_id':item.category_id, 'next':nex, 'count':pagination.total, 'previou':previous, 'pagenumber':pagination.page } results.append(result1) if results: print ({'zzzzzzzz':items}) # return (results) return ({'results':results, 'count':pagination.total, 'next':nex, 'per_page':pagination.per_page, 'page':pagination.page}, 200) return ({"message":"search item not found"}), 404
def post(self): """This method allows for the creation of users""" """This handles post requests for this view --- Tags: - User Authentication Parameters: - in: Body name: Body required: True type: String description: This registers a new user responses: 201: description: User created """ username = request.data['username'] firstname = request.data['firstname'] email = request.data['email'] password = request.data['password'] confirm_password = request.data['confirm_password'] args = { 'username': username, 'firstname': firstname, 'email': email, 'password': password, 'confirm_password': confirm_password } if values_is_empty(args): return {'error': 'all fields must be filled'}, 400 username = args['username'] email = args['email'] password = args['password'] firstname = args['firstname'] confirm_password = args['confirm_password'] if args['email'] == "": return ({'email': 'email not provided'}) returned = {} if not is_email_address_valid(email): # returned.append('invalid email') returned['email'] = 'invalid email' if not is_username_valid(username): # returned.append('invalid username cannot begin with numbers') returned['username'] = '******' if not is_firstname_valid(firstname): # returned.append('invalid firstname cannot have numbers') returned['firstname'] = 'invalid firstname cannot have numbers' if confirm_password != password: # returned.append('passwords do not match') returned['confirm_password'] = '******' if len(password) < 8: # returned.append('password must be more than 8 characters') returned['password'] = '******' person = User.query.filter_by(username=username, email=email).first() if person is None: new_user = User(username, email, password, firstname) new_user.hash_password(password) try: new_user.save_user() response = {'message': 'Successfully registered'} return make_response(jsonify(response), 201) # return { # 'userid': new_user.userid, 'Username': new_user.username, # "email": new_user.email, 'firstname':new_user.firstname}, 201 except exc.IntegrityError: returned[ 'username_exists'] = 'username already exists please choose another one' # return ({"error":'username already exists please choose another one'}), 400 else: returned['user_exists'] = 'User already exists' # return {"error": "User already exists"}, 400 if returned: response = {"error": returned} return make_response(jsonify(response), 400)