Beispiel #1
0
 def test_category_model(self):
     category = Category()
     category.category_id = '228383838'
     category.category_name = 'Others'
     db.session.rollback()
     db.session.add(category)
     self.assertEqual(None, db.session.commit())
Beispiel #2
0
def faker():
    if request.args.get('remove'):
        model = False
        if request.args.get('remove') == 'categories':
            model = Category
        if request.args.get('remove') == 'products':
            model = Product
        if model:
            model.query.delete()
            db.session.commit()

        return redirect('/admin/faker', code=302)

    if request.args.get('count') and request.args.get('model'):
        objs = []
        for i in range(int(request.args.get('count'))):
            fake = Faker()
            # model = request.args.get('model')()

            if request.args.get('model') == 'categories':

                name = fake.word()
                if Category.query.filter_by(name=name).count() > 0:
                    continue

                model = Category()
                model.name = name
                model.parent_id = get_random_elem(Category.query.all())

                db.session.add(model)
                db.session.commit()

            if request.args.get('model') == 'products':

                name = fake.word()
                if Product.query.filter_by(name=name).count() > 0:
                    continue

                model = Product()
                model.name = name
                model.category_id = get_random_elem(Category.query.all(),
                                                    False)
                model.price = fake.random_number(4)
                model.prop1 = fake.random_number(1, True)
                model.prop2 = fake.random_number(2, True)

                db.session.add(model)
                db.session.commit()
        return redirect('/admin/faker', code=302)

    categories = Category.query.all()
    products = Product.query.all()

    return render_template('admin/faker.html',
                           categories=categories,
                           products=products)
def seed_categories_data():
    category_ids = ('cat-1234', 'cat-1256', 'cat-3456', 'cat-5678', 'cat-3443')
    category_names  = ('Banks', 'Hostels', 'Others', 'Administrative', 'Schools')
    categories_data = dict(zip(category_ids, category_names))
    for id,name in categories_data.items():
        category = Category()
        category.category_id = id
        category.category_name = name
        db.session.add(category)
        db.session.commit()
        print(category)
    return True