def post(self, bucketlist_id): """ Handles the adding bucketlist items """ post_data = request.get_json() name = strip_white_space(post_data.get('name')) finished_by = strip_white_space(post_data.get('finished_by'), skip_check_symbols=True) if not name or not finished_by: return abort(400, "Bucketlist item name and finished_by should be provided") bucketlist = BucketList.query.filter_by(user_id=g.current_user.id, id=bucketlist_id).first() if bucketlist: bucketlist_item = BucketListItem( name=name, finished_by=finished_by, bucketlist_id = bucketlist.id ) if bucketlist_item.save_bucketlist_item(): response = { 'status': 'success', 'message': 'Bucket list item added' } return response, 201 return abort(409, 'Bucketlist Item Exists') return abort(404, 'Bucketlist with ID {} not found in the database'.format(bucketlist_id))
def post(self): """ Handles post requests for authentication of a user """ post_data = request.get_json() email = strip_white_space(post_data.get('email'), skip_check_symbols=True) password = strip_white_space(post_data.get('password')) if not validate_email(email): return abort(400, 'Invalid Email Address') if not password: return abort(400, 'Password is required') user = User.query.filter_by(email=email).first() try: if user and user.verify_password(password): auth_token = user.generate_authentication_token() user_data = dict(data=marshal(user, USER)) response = { 'status': 'success', 'message': 'Login Successful', 'auth_token': auth_token.decode('utf-8') } response.update(user_data) return response, 201 response = { 'status': 'fail', 'message': 'Failed to authenticate user' } return response, 401 except Exception as e: return abort(500, 'Error logging in user:{}'.format(e))
def post(self): """ Handles post requests for registration of a new user """ post_data = request.get_json() fname = strip_white_space(post_data.get('fname')) or None lname = strip_white_space(post_data.get('lname')) or None email = strip_white_space(post_data.get('email'), skip_check_symbols=True) or None password = strip_white_space(post_data.get('password')) or None if not validate_email(email): return abort(400, 'Invalid Email Address') if not fname or not lname or not fname.isalpha() or not lname.isalpha( ): return abort(400, "First and last name must be provided") if not password: return abort(400, "Password must be provided") user = User(email=email, fname=fname.capitalize(), lname=lname.capitalize(), password=password) try: check = user.save_user() if check: auth_token = user.generate_authentication_token() user_data = dict(data=marshal(user, USER)) response = { 'status': 'success', 'message': 'Successfully Registered', 'auth_token': auth_token.decode('utf-8') } response.update(user_data) return response, 201 else: response = {'status': 'fail', 'message': 'User Exists'} return response, 409 except Exception as e: return abort(500, 'Error creating your account:{}'.format(e))
def put(self, bucketlist_id): """ Updates existing bucketlists for specific user """ print(request) put_data = request.get_json() name = strip_white_space(put_data.get('name')) if not name: return abort(400, "Bucket list name must be provided") bucketlist = BucketList.query.filter_by(user_id=g.current_user.id, id=bucketlist_id).first() if bucketlist: if bucketlist.save_bucketlist(name): return bucketlist, 201 return abort(409, "Bucketlist exists") return abort( 404, 'Bucketlist with ID {} not found in the database'.format( bucketlist_id))
def put(self, bucketlist_id, item_id): """ Handles put requests to alter a single bucketlist item """ put_data = request.get_json() name = strip_white_space(put_data.get('name') or '') or None completed = str(put_data.get('completed')) or None if completed is not None and isinstance(completed, str): completed = completed.strip() completed = completed.capitalize() test_completed = completed is None or not completed.isalnum() verify_completed = completed == 'True' or completed == 'False' if completed is not None and not verify_completed: return abort(400, 'Completed must either be true or false') if not name and test_completed: return abort(400, 'No data sent for updating or inaccurate data provided') bucketlist = BucketList.query.filter_by(user_id=g.current_user.id, id=bucketlist_id).first() if bucketlist: item = BucketListItem.query.filter_by( bucketlist_id=bucketlist_id, id=item_id ).first() if item and name == item.name: if item.save_bucketlist_item( completed = completed ): return item, 201 return abort(409, "Bucket list item already exists") if item: if item.save_bucketlist_item( name = name, completed = completed ): return item, 201 return abort(409, "Bucket list item already exists") return abort(404, 'Bucketlist Item with ID {} not found in the database'.format(item_id)) return abort(404, 'Bucketlist with ID {} not found in the database'.format(item_id))
def post(self): """ Handles adding of new bucketlists """ post_data = request.get_json() name = strip_white_space(post_data.get('name')) if not name: return abort(400, "Bucket list name must be provided") bucketlist = BucketList(name=name, user_id=g.current_user.id) try: check = bucketlist.save_bucketlist() if check: response = {'status': 'success', 'message': 'Bucketlist Added'} return response, 201 else: response = {'status': 'fail', 'message': 'Bucketlist Exists'} return response, 409 except Exception as e: return abort(500, message='Error creating your account:{}'.format( e.message))
def test_string_contain_symbols(self): """ Tests that when a string containing symbols is passed to the strip white space method, false is returned """ self.assertFalse(strip_white_space("This is a bucket |!$t"))
def test_white_space_return_false(self): """ Tests that if only spaces are passed to the strip white space method false is returned """ self.assertFalse(strip_white_space(" "))
def test_strip_white_space(self): """ Tests that white space is trimmed off string passed to strip white space method """ self.assertEqual(strip_white_space(" testing "), "testing")