Esempio n. 1
0
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())
Esempio n. 2
0
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())
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
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
Esempio n. 7
0
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
Esempio n. 8
0
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
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
def get_category(category_id):
    """
    Gets a category.
    """
    return jsonify(Category.get(category_id))
Esempio n. 12
0
def get_categories():
    """
    Gets the list of categories.
    """
    # TODO: pagination
    return jsonify(Category.all())
Esempio n. 13
0
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)
Esempio n. 14
0
def get_category(category_id):
    """
    Gets a category.
    """
    return jsonify(Category.get(category_id))
Esempio n. 15
0
 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.')
Esempio n. 16
0
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)
Esempio n. 17
0
 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.')
Esempio n. 18
0
 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)
Esempio n. 19
0
 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)
Esempio n. 20
0
def get_categories():
    """
    Gets the list of categories.
    """
    # TODO: pagination
    return jsonify(Category.all())