def set_category_sort(): """ Sets the order of the categories. """ if 'categories' not in request.json: return api_error('Invalid form.') categories = request.json['categories'] for data in categories: category = Category.get(data['id']) category.sort_order = data['sort_order'] category.save() return jsonify(Category.all())
def create_category(): """ Create a category. Must be an admin. """ form = CategoryForm() if not form.validate_on_submit(): return api_error(form.errors) name = form.name.data description = form.description.data category = Category(name=name, description=description) category.save() return '', 201
def update_category(category_id): """ Update an category. """ category = Category.get(category_id) if not category: return api_error('Category not found', 404) form = CategoryForm( validate_unique_name=category.name != request.json.get('name') ) if not form.validate_on_submit(): return api_error(form.errors) category.name = form.name.data category.description = form.description.data if 'services' in request.json: services = request.json['services'] for data in services: service = Service.get(data['id']) service.sort_order = data['sort_order'] service.save() category.save() return '', 200
def delete_category(category_id): """ Delete an category. """ category = Category.get(category_id) if not category: return api_error('Category not found', 404) category.delete() return '', 200
def create_service(): """ Create a service. Must be an admin. """ form = ServiceForm() if not form.validate_on_submit(): return api_error(form.errors) service = Service( name=form.name.data, description=form.description.data, category=Category.get(form.category.data) ) service.save() return '', 201
def update_service(service_id): """ Update an service. """ service = Service.get(service_id) if not service: return api_error("Service not found", 404) form = ServiceForm(validate_unique_name=service.name != request.json.get("name")) if not form.validate_on_submit(): return api_error(form.errors) service.name = form.name.data service.description = form.description.data service.category = Category.get(form.category.data) service.save() return "", 200
def update_category(category_id): """ Update an category. """ category = Category.get(category_id) if not category: return api_error('Category not found', 404) form = CategoryForm( validate_unique_name=category.name != request.json.get('name')) if not form.validate_on_submit(): return api_error(form.errors) category.name = form.name.data category.description = form.description.data if 'services' in request.json: services = request.json['services'] for data in services: service = Service.get(data['id']) service.sort_order = data['sort_order'] service.save() category.save() return '', 200
def get_category(category_id): """ Gets a category. """ return jsonify(Category.get(category_id))
def get_categories(): """ Gets the list of categories. """ # TODO: pagination return jsonify(Category.all())
def seed_services(): """Seed some categories and services.""" food = Category.get_by_name("Food") if not food: food = Category() food.name = "Food" food.description = "" food.save() food_delivery = Service.get_by_name("Delivery") if not food_delivery: food_delivery = Service() food_delivery.name = "Delivery" food_delivery.description = "Quick for those hungry children" food_delivery.category = food food_delivery.save() shelter = Category.get_by_name("Shelter") if not shelter: shelter = Category() shelter.name = "Shelter" shelter.description = "Housing for a limited time." shelter.save() shelter_one_night = Service.get_by_name("One night") if not shelter_one_night: shelter_one_night = Service() shelter_one_night.name = "One night" shelter_one_night.description = "A bed for the night" shelter_one_night.category = shelter shelter_one_night.save() clothing = Category.get_by_name("Clothing") if not clothing: clothing = Category() clothing.name = "Clothing" clothing.description = "Shirts, shoes, and all things." clothing.save() clothing_young = Service.get_by_name("0-5 clothing") if not clothing_young: clothing_young = Service() clothing_young.name = "0-5 clothing" clothing_young.description = "Clothing for chidren ages 0 to 5" clothing_young.category = clothing clothing_young.save() return (food_delivery, shelter_one_night, clothing_young)
def validate_name(self, field): if self.validate_unique_name and Category.get_by_name(field.data): raise ValidationError('This service name is already in use.')
def seed_services(): """Seed some categories and services.""" food = Category.get_by_name("Food") if not food: food = Category() food.name = "Food" food.description = "" food.save() food_delivery = Service.get_by_name("Delivery") if not food_delivery: food_delivery = Service() food_delivery.name = "Delivery" food_delivery.description = "Quick for those hungry children" food_delivery.category = food food_delivery.save() shelter = Category.get_by_name("Shelter") if not shelter: shelter = Category() shelter.name = "Shelter" shelter.description = "Housing for a limited time." shelter.save() shelter_one_night = Service.get_by_name("One night") if not shelter_one_night: shelter_one_night = Service() shelter_one_night.name = "One night" shelter_one_night.description = "A bed for the night" shelter_one_night.category = shelter shelter_one_night.save() clothing = Category.get_by_name("Clothing") if not clothing: clothing = Category() clothing.name = "Clothing", clothing.description = "Shirts, shoes, and all things." clothing.save() clothing_young = Service.get_by_name("0-5 clothing") if not clothing_young: clothing_young = Service() clothing_young.name = "0-5 clothing" clothing_young.description = "Clothing for chidren ages 0 to 5" clothing_young.category = clothing clothing_young.save() return (food_delivery, shelter_one_night, clothing_young)
def __init__(self, *args, **kwargs): super(ServiceForm, self).__init__(*args, **kwargs) self.category.choices = [(category.id, category.name) for category in Category.all()] self.validate_unique_name = kwargs.get('validate_unique_name', True)
def __init__(self, *args, **kwargs): super(ServiceForm, self).__init__(*args, **kwargs) self.category.choices = [ (category.id, category.name) for category in Category.all() ] self.validate_unique_name = kwargs.get('validate_unique_name', True)