def db_init(): """Initialize the database.""" from app import db db.create_all() click.echo('Initialize the database.') from app.models import Category c1 = Category.create(name=u'Uncategorized', slug=u'uncategorized') c1.save() c2 = Category.create(name=u'News', slug=u'news') c2.save()
def db_init(): """Initialize the database.""" from app import db from app.models import Category db.create_all() click.echo('Initialize the database.') c1 = Category.create(name=u'Uncategorized', slug=u'uncategorized') c1.save() c2 = Category.create(name=u'News', slug=u'news') c2.save()
def post(self): form = CategoryForm() if form.is_submitted(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) category = Category.create() if not form.slug.data: form.slug.data = form.name.data form.slug.data = Category.urlify(form.slug.data) form.populate_obj(category) category.save() return render_view(url_for('CategoriesView:put', id=category.id), message=_('CATEGORY_CREATE_SUCESS'), redirect=True) except Exception as e: flash(e.message, 'error') return render_view('admin/categories/add.html', form=form)
def post(self): form = CategoryForm() if form.is_submitted(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) category = Category.create() if not form.slug.data: form.slug.data = form.name.data form.slug.data = Category.urlify(form.slug.data) form.populate_obj(category) category.save() return render_view(url_for('CategoriesView:put', id=category.id), message=_('CATEGORY_CREATE_SUCESS'), redirect=True) except Exception as e: flash(e.message, 'error') return render_view('admin/categories/add.html', form=form)
def category_add(): if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/category_add.html') return template.render(categories=all_categories) if request.method == 'POST': new_category = Category.create(category_name=post_get('category_name')) app.flash('New category was added') redirect('/category/add')
def category_add(): if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/category_add.html') return template.render(categories=all_categories) if request.method == 'POST': new_category = Category.create(category_name=post_get('category_name')) app.flash(u'Нова категорія була успішно додана') redirect('/category/add')
def post(self): form = CategoryForm() if request.method == "POST": if form.validate_on_submit(): try: category = Category.create() form.populate_obj(category) category.save() flash(gettext("Category succesfully created")) return util.redirect_json_or_html(url_for("CategoriesView:index"), "category") except: flash(gettext("Error while creating the category"), "error") else: flash(gettext("Invalid submission, please check the message below"), "error") return render_template("admin/categories/add.html", title=gettext("Create Category"), form=form)
def post(self): """ @api {POST} /api/v1/categories Create category for an account @apiVersion 0.0.1 @apiName CreateCategory @apiGroup Categories @apiDescription Create a category for an account @apiHeader {String} Authorization Users auth token @apiHeader {String} Content-Type="application/json" Content-Type (should be application/json for every post requests) @apiHeaderExample {json} Header-Example: { "Authorization": "Bearer auth_token_here" } @apiParam {Number} acc_id Account ID @apiParam {String} name Category name @apiParam {String} type Category type @apiParam {Number=null} parent_id Parent category ID (omit if no parent) @apiParamExample {json} Request-Example: { "acc_id": 1, "name": "Cat", "type": "expense" } @apiParamExample {json} Request-Example with parent_id: { "acc_id": 1, "parent_id": 1, "name": "Dog meat", "type": "expense" } @apiSuccess (Success) {String} status Status @apiSuccess (Success) {String} message Message @apiSampleRequest /api/v1/transactions @apiExample cURL example $ curl -H "Content-Type: application/json" -H "Authorization": "Bearer auth_token_here" -X POST -d '{"acc_id": 1, "name": "Cat", "type": "expense"}' http://ec2-35-153-68-36.compute-1.amazonaws.com/api/v1/categories @apiSuccessExample {json} Success-Response: HTTP/1.0 200 OK { "message": "Successfully created new category", "status": "success" } """ ctx = _request_ctx_stack.top current_user = ctx.user request_body = request.get_json() acc_id, parent_id, name, type = get_dict_value_by_key( request_body, 'acc_id', 'parent_id', 'name', 'type') account = Account.get_by_id(acc_id, current_user.id) if account is None: return response('failed', 'This account belongs to another user', 401) new_category = Category.create(name, type.lower(), acc_id) if parent_id: parent_category = Category.get_by_id(parent_id) parent_category.children.append(new_category) try: new_category.save() except IntegrityError: return response('failed', 'Duplicate category name', 400) else: return response('success', 'Successfully created new category', 200)
def test_db(app): db.drop_all() db.create_all() # define categories State.create(id=1, name="North Carolina", state_short="NC") State.create(id=2, name="New York", state_short="NY") Sector.create(id=1, name="Home Services") Sector.create(id=2, name="Food & Drink") c1 = Category.create(id=1, name="Electrician", sector_id=1) c2 = Category.create(id=2, name="Plumber", sector_id=1) Category.create(id=3, name="Mexican Restaurant", sector_id=2) a1 = Address.create( line1="13 Brook St", city="Lakewood", zip="14750", state_id=2, latitude=42.100201, longitude=-79.340303 ) # add test users u1 = User.create( id=1, username="******", first_name="Sarah", last_name="Smith", email="*****@*****.**", address=a1 ) u2 = User.create( id=2, username="******", first_name="John", last_name="Jones", email="*****@*****.**", address=Address( line1="7708 covey chase Dr", line2='', city="Charlotte", zip="28210", state_id=1, latitude=35.123949, longitude=-80.864783 ) ) u3 = User.create( id=3, username="******", first_name="Mark", last_name="Johnson", email="*****@*****.**", address=Address( line1="7718 Covey Chase Dr", line2='', city="Charlotte", zip="28210", state_id=1, latitude=35.123681, longitude=-80.865045 ) ) User.create( id=4, username="******", first_name="Hyman", last_name="Rickover", email="*****@*****.**", address=Address( line1="7920 Covey Chase Dr", line2='', city="Charlotte", zip="28210", state_id=1, latitude=35.120759, longitude=-80.865781 ) ) # add test providers p1 = Provider.create( id=1, name="Douthit Electrical", telephone="704-726-3329", email="*****@*****.**", website='https://www.douthitelectrical.com/', categories=[c1, c2], address=Address( line1="6000 Fairview Rd", line2="suite 1200", city="Charlotte", zip="28210", state_id=1, latitude=35.150495, longitude=-80.838958 ) ) Provider.create( id=2, name="Evers Electric", telephone="7048431910", email='', website='http://www.everselectric.com/', categories=[c1], address=Address( line1="3924 Cassidy Drive", line2="", city="Waxhaw", zip="28173", state_id=1, latitude=34.938645, longitude=-80.760691 ) ) p3 = Provider.create( id=3, name="Preferred Electric Co", telephone="7043470446", email="*****@*****.**", categories=[c1], address=Address( line1="4113 Yancey Rd", line2='', city="charlotte", zip="28217", state_id=1, latitude=35.186947, longitude=-80.880459 ) ) p4 = Provider.create( id=4, name="Basic Electric Co", telephone="7044070077", email="*****@*****.**", is_active=False, categories=[c1], address=Address( line1="7708 Covey Chase Dr", line2='', city="Charlotte", zip="28217", state_id=1, latitude=35.123949, longitude=-80.865781 ) ) # add test groups g1 = Group.create( id=1, name="QHIV HOA", description="Hoa for the neighborhood", admin_id=2 ) Group.create( id=2, name="Shannon's Bees", description="Insects that like to make honey", admin_id=2 ) Group.create( id=3, name="Shawshank Redemption Fans", description="test", admin_id=3 ) # add test reviews Review.create( id=1, user=u2, provider=p1, category=c1, rating=3, cost=3, description="fixed a light bulb", comments="satisfactory work.", pictures=[ Picture( path=os.path.join( app.config['MEDIA_FOLDER'], '2', 'test1.jpg' ), name='test1.jpg' ) ] ) Review.create( id=2, user=u3, provider=p1, category=c1, rating=5, cost=5, price_paid="", description="installed breaker box", comments="very clean" ) Review.create( id=3, user=u1, provider=p1, category=c1, rating=1, cost=5, price_paid="", description="test", comments="Test" ) Review.create( id=4, user=u2, provider=p3, category=c1, rating=5, cost=2, price_paid="", description="test", comments="Test123", service_date=date(2019, 5, 1) ) Review.create( id=5, user=u2, provider=p3, category=c1, rating=4, cost=3, price_paid="", description="moretest", comments="Test123456", service_date=date(2019, 5, 1) ) Review.create( id=6, user=u1, provider=p1, category=c1, rating=1, cost=5, price_paid="", description="yetanothertest", comments="Testing" ) Review.create( id=7, user=u2, provider=p4, category=c1, rating=1, cost=1, price_paid=100, description="getting electrocuted", comments="terrible" ) # add test relationships u2.add(u1) u2.add(g1) u3.add(g1) # set user passwords u1.set_password("password1234") u2.set_password("password") u3.set_password("password5678") # set starting messages Message.send_new( dict(user_id=1), dict(user_id=2), "test subject", "test body" ) time.sleep(1) Message.send_new( dict(user_id=2), dict(full_name="admin"), "test admin subject", "test adminbody", msg_type="admin" ) Message.send_new( dict(user_id=1), dict(user_id=2), "yet another test subject", " yet another test body" ) yield db db.session.remove() db.drop_all()