Ejemplo n.º 1
0
 def handle(self, *args, **options):
     wb = xlrd.open_workbook(
         os.path.join(settings.BASE_DIR, 'categories.xls'))
     sheet = wb.sheet_by_index(0)
     cats_db = {}
     for rnum in range(sheet.nrows):
         row = sheet.row_values(rnum)
         cats = row[0].split('/')
         path = ''
         depth = 0
         for cat in cats:
             cat_path = '/'.join((path, cat)) if path else cat
             print(path)
             if cat_path in cats_db:
                 path = cat_path
                 depth += 1
                 continue
             parent = cats_db.get(path, None)
             obj = Category(name=cat,
                            parent=parent,
                            depth=depth,
                            full_path=cat_path)
             path = cat_path
             cats_db[path] = obj
             depth += 1
             obj.save()
Ejemplo n.º 2
0
def new_category(args, user):
    # The name of a category is unique, we need to check if a category
    # already exists with the given name
    if Category.get_by_name(args["name"]) is not None:
        raise errors.Conflict("Category already exists")

    category = Category(name=args["name"], user=user)
    category.save_to_db()

    return CategorySchema().jsonify(category), 201
Ejemplo n.º 3
0
def new():
    """Create new category page.

    Displays the form to create a new category. If the form is successfully
    submitted an success message is shown.
    This page can be accessed only by logged in users.
    """

    form = CategoryForm()
    if request.method == 'GET':
        pass
    elif form.validate_on_submit():
        category = Category(form.name.data, form.description.data)

        db.session.add(category)
        db.session.flush()

        upload_category_image(form.image.data, category.id)

        db.session.commit()
        flash('Hooray! Category was successfully added!', 'success')
    else:
        print form.errors.items()
        flash('Oopss! There are some issues to fix here...', 'danger')
    return render_template('categories/new.html', form=form)
Ejemplo n.º 4
0
    def wrapper(**kwargs):
        category_id = kwargs.pop("category_id", None)
        category = Category.get_by_id(category_id)

        if category is None:
            raise errors.NotFound()

        kwargs["category"] = category

        return f(**kwargs)
Ejemplo n.º 5
0
 def handle(self, *args, **options):
     wb = xlrd.open_workbook(os.path.join(settings.BASE_DIR,
                                          'categories.xls'))
     sheet = wb.sheet_by_index(0)
     cats_db = {}
     for rnum in range(sheet.nrows):
         row = sheet.row_values(rnum)
         cats = row[0].split('/')
         path = ''
         depth = 0
         for cat in cats:
             cat_path = '/'.join((path, cat)) if path else cat
             print(path)
             if cat_path in cats_db:
                 path = cat_path
                 depth += 1
                 continue
             parent = cats_db.get(path, None)
             obj = Category(name=cat, parent=parent, depth=depth, full_path=cat_path)
             path = cat_path
             cats_db[path] = obj
             depth += 1
             obj.save()
Ejemplo n.º 6
0
def validate_category():
    name = request.form['name']
    description = request.form['description']
    invalid = False

    if not name or len(name.strip()) < 3:
        invalid = True
        flash('Type a valid name with three or more characters', 'warning')

    if not description or len(description.strip()) == 0:
        invalid = True
        flash('Type some description', 'warning')

    if not invalid:
        return Category(name=name, description=description)
    else:
        return None
Ejemplo n.º 7
0
    def create_categories():
        """ Insert template categories when none is found. """

        category_service = CategoryService()
        if not category_service.all().count():
            print "Creating categories..."
            cat_file = open(os.getcwd() + '/catalog/resources/categories.csv',
                            'r')
            for line in cat_file:
                if line.startswith("#"):
                    continue
                category_line = line.split('\t')
                category = Category(name=category_line[0],
                                    description=category_line[1])
                category_service.new(category)
        else:
            print "Category table already populated."

        print "Done."
Ejemplo n.º 8
0
def category_form():
    return render_template("category_form.html",
                           target_url="/category/new",
                           category=Category(name='', description=''))
Ejemplo n.º 9
0
def list_categories():
    categories = Category.get_all()

    return CategorySchema().jsonify(categories, many=True)
Ejemplo n.º 10
0
#!flask/bin/python
# -*- coding: utf-8 -*-

from catalog import db
from catalog.models.user import User
from catalog.models.category import Category
from catalog.models.recipe import Recipe, RecipeImage

user = User('facebook$10206593265223852', 'John Doe', '*****@*****.**', None)
db.session.add(user)
appetizer = Category('Appetizer', 'Appetizers and Snacks')
db.session.add(appetizer)
bbq = Category('BBQ & Grilling', 'BBQ and Grilling')
db.session.add(bbq)
dessert = Category('Desserts', 'Sweets, Desserts')
db.session.add(dessert)
breakfast = Category('Breakfast', 'Breakfast and Brunch')
db.session.add(breakfast)
drinks = Category('Drinks', 'Drinks')
db.session.add(drinks)
dinner = Category('Dinner', 'Dinner')
db.session.add(dinner)
fruits = Category('Fruits & Vegetables', 'Fruits, Vegetables and Other')
db.session.add(fruits)
healthy = Category('Healthy Recipes', 'Healthy Food')
db.session.add(healthy)
lunch = Category('Lunch Recipes', 'Lunch recipes')
db.session.add(lunch)
meat = Category(
    'Meat & Poultry', 'Top recipes for beef, chicken, pork, and more. \
    See classic recipes or find something new.')