def update_business(current_user, businessId): """Updates a business given a business ID confirms if current user is owner of business """ data = request.get_json() business = get_in_module('business', businessId) business.name = data['name'] business.logo = data['logo'] business.location = data['location'] business.category = data['category'] business.bio = data['bio'] business.save() if business.name == data['name']: return jsonify({ 'success': 'successfully updated', 'business': { 'id': business.id, 'name': business.name, 'logo': business.logo, 'location': business.location, 'category': business.category, 'bio': business.bio, 'owner': business.owner.username, 'created_at': business.created_at, 'updated_at': business.updated_at } }), 201 return jsonify({'warning': 'Business Not Updated'}), 400
def update_diary(current_user, diaryId): """Updates a diary given a diary ID confirms if current user is owner of diary """ data = request.get_json() diary = get_in_module('diary', diaryId) diary.name = data['name'] diary.logo = data['logo'] diary.location = data['location'] diary.category = data['category'] diary.bio = data['bio'] diary.save() if diary.name == data['name']: return jsonify({ 'success': 'successfully updated', 'diary': { 'id': diary.id, 'name': diary.name, 'logo': diary.logo, 'location': diary.location, 'category': diary.category, 'bio': diary.bio, 'owner': diary.owner.username, 'created_at': diary.created_at, 'updated_at': diary.updated_at } }), 201 return jsonify({'warning': 'Diary Not Updated'}), 400
def wrap(*args, **kwargs): diary = get_in_module('diary', kwargs['diaryId']) if not diary: return jsonify({'warning': 'Diary Not Found'}), 404 if args[0] != diary.owner.id: return jsonify({'warning': 'Not Allowed, you are not owner'}), 401 return f(*args, **kwargs)
def wrap(*args, **kwargs): business = get_in_module('business', kwargs['businessId']) if not business: return jsonify({'warning': 'Business Not Found'}), 404 if args[0] != business.owner.id: return jsonify({'warning': 'Not Allowed, you are not owner'}), 401 return f(*args, **kwargs)
def delete_diary(current_user, diaryId): """Deletes a diary confirms if current user is owner of diary """ diary = get_in_module('diary', diaryId) name = diary.name diary.delete() if not existing_module('diary', name): return jsonify({'success': 'Diary Deleted'}), 200 return jsonify({'warning': 'Diary Not Deleted'}), 400
def delete_business(current_user, businessId): """Deletes a business confirms if current user is owner of business """ business = get_in_module('business', businessId) name = business.name business.delete() if not existing_module('business', name): return jsonify({'success': 'Business Deleted'}), 200 return jsonify({'warning': 'Business Not Deleted'}), 400
def create_diary(current_user): """Creates a diary Takes current_user ID and update data test if actually saved """ data = request.get_json() # Check if there is an existing diary with same name if existing_module('diary', data['name']): return jsonify({ 'warning': 'Diary name {} already taken'.format(data['name']) }), 409 diary_owner = get_in_module('user', current_user) # create new diary instances new_diary = Diary( name=data['name'], logo=data['logo'], location=data['location'], category=data['category'], bio=data['bio'], owner=diary_owner ) # Commit changes to db new_diary.save() # Send response if diary was saved if new_diary.id: return jsonify({ 'success': 'successfully created diary', 'diary': { 'id': new_diary.id, 'name': new_diary.name, 'location': new_diary.location, 'category': new_diary.category, 'bio': new_diary.bio, 'owner': new_diary.owner.username } }), 201 return jsonify({'warning': 'Could not create new diary'}), 401
def read_diary(diaryId): """Reads Diary given a diary id""" diary = get_in_module('diary', diaryId) if diary: return jsonify({ 'diary': { 'id': diary.id, 'name': diary.name, 'logo': diary.logo, 'location': diary.location, 'category': diary.category, 'bio': diary.bio, 'owner': diary.owner.username, 'created_at': diary.created_at, 'updated_at': diary.updated_at } }), 200 return jsonify({'warning': 'Diary Not Found'}), 404
def read_business(businessId): """Reads Business given a business id""" business = get_in_module('business', businessId) if business: return jsonify({ 'business': { 'id': business.id, 'name': business.name, 'logo': business.logo, 'location': business.location, 'category': business.category, 'bio': business.bio, 'owner': business.owner.username, 'created_at': business.created_at, 'updated_at': business.updated_at } }), 200 return jsonify({'warning': 'Business Not Found'}), 404
def create_business(current_user): """Creates a business Takes current_user ID and update data test if actually saved """ data = request.get_json() # Check if there is an existing business with same name if existing_module('business', data['name']): return jsonify( {'warning': 'Business name {} already taken'.format(data['name'])}), 409 business_owner = get_in_module('user', current_user) # create new business instances new_business = Business(name=data['name'], logo=data['logo'], location=data['location'], category=data['category'], bio=data['bio'], owner=business_owner) # Commit changes to db new_business.save() # Send response if business was saved if new_business.id: return jsonify({ 'success': 'successfully created business', 'business': { 'id': new_business.id, 'name': new_business.name, 'location': new_business.location, 'category': new_business.category, 'bio': new_business.bio, 'owner': new_business.owner.username } }), 201 return jsonify({'warning': 'Could not create new business'}), 401