def create_user_and_sample_Items(name, phone_num): customer = Customer(name, phone_num) cookie = Item(name + "_Cookie", 1.50) muffin = Item(name + "_Muffin", 2.00, "Carby stuff") latte = Item(name + "_Latte", 4.35, "Steamed milk over expresso") mocha = Item(name + "_Mocha", 5.00, "Steamed milk over expresso and chocolate syrup") session.add(customer) session.add(cookie) session.add(muffin) session.add(latte) session.add(mocha) session.commit() result = {"customer": customer} item_names = ['cookie', 'muffin', 'latte', 'mocha'] for name in item_names: item = session.query(Item).\ filter(func.lower(Item.name).like('%' + name + '%')).\ filter(func.lower(Item.name).like('%' + customer_name + '%')).\ scalar() result[name] = item return result
def items(db): db.session.add_all([ Item(name='apple'), Item(name='banana'), Item(name='cucumber'), Item(name='dill'), Item(name='eggplant'), ]) db.session.commit() return Item.query.all()
def post(self, user, id): """ API Endpoint to create a new item --- tags: - Bucketlist Item parameters: - name: auth_token description: Authentication token in: header type: string required: true - name: id description: Bucketlist Id in: path type: integer required: true - name: description description: The description of am item in: formData type: string required: true """ # search for bucketlist bucketlist = Bucketlist.query.filter_by(id=id, created_by=user.email).first() # return 400 if bucketlist non exixtant or not belongs to this user if bucketlist is None: return 'Bucketlist not found', 202 if 'description' not in self.request.form: return "Item not created", 202 # parse and validate form data item_description = self.request.form['description'] if not item_description: return "Item decription cannot be empty", 401 # Create item and save in database item = Item(description=item_description, date_created=datetime.utcnow(), bucketlist_id=bucketlist.id, bucketlist=bucketlist) item.save() return "Successfully created item", 201
def create(category_id): form = CreateForm() form.category_id.choices = [ (c.category_id, c.name) for c in Category.query.filter( or_( current_user.filter_ == Category.filter_, current_user.filter_ == None, # noqa: E711 )).order_by(Category.name) ] if form.validate_on_submit(): try: item = Item(name=form.name.data, category_id=form.category_id.data) database.session.add(item) database.session.commit() flash("The item has been created.") except IntegrityError: database.session.rollback() flash( "The item has not been created due to concurrent modification.", "error", ) return redirect(url_for("item.list")) elif request.method == "GET": form.category_id.data = category_id return render_template( "item/create.html.jinja", title="Create Item", form=form, cancel_url=url_for("item.list"), )
def is_changed(self): from application.models import Item head = Item.objects(item_id=self.head).first() if not head: return True if self.modified != head.modified:return True return False
def update_item_availability(item_id): from application.models import Item item = Item.objects(item_id=item_id, availability=True).first() if not item: return for spec in item.specs: if spec.availability: return item.update(set__availability=False, set__status='DEL')
class ItemTestCase(unittest.TestCase): def setUp(self): self.user = User('mock-name', 'mock-pass') self.user.id = 123 self.item = Item('mock-item', self.user) def test_buy(self): self.item.buy(self.user, True) self.assertEqual(self.item.bought_by, self.user.id) self.assertTrue(self.item.bought_date, not None) def test_serialize(self): data = {'id': self.item.id, 'name': self.item.name, 'create_date': self.item.create_date, 'created_by': self.item.created_by, 'bought_date': self.item.bought_date, 'bought_by': self.item.bought_by} self.assertEqual(self.item.serialize(), data)
def update_to_head(self): from application.models import Item head = Item.objects(item_id=self.head).first() if head: data = head._data for k, v in data.items(): setattr(self, k, v) self.save() else: return self
def create_from_skus(cls, customer_id, skus, logistic_provider, coupon_codes, coin=0, cash=0, address=None, **kwargs): from application.models import OrderEntry, ForexRate, Item, ItemSpec entries = [] for e in skus: availability = check_availability_and_update_stock( e['item_id'], e['sku'], e['quantity']) if not availability: return e spec = ItemSpec.objects(sku=e['sku']).first() item = Item.objects(item_id=e['item_id']).first() entry = OrderEntry(spec=spec, item=item, quantity=e['quantity']).save() entries.append(entry) order = cls(customer_id=customer_id, entries=entries, logistic_provider=logistic_provider, coupon_codes=coupon_codes, coin=coin, cash=cash, **kwargs) if not order.forex: order.forex = ForexRate.get() order.update_amount() order.reload() for e in order.entries: e.create_snapshot() if address: order.set_address(address) import application.services.jobs as Jobs #Jobs.stat.update_user_stats(str(order.customer_id)) Signals.order_created.send('system', order=order) return order
def addItem(): error = None form = ItemForm() if form.validate_on_submit(): try: i = Item() form.populate_obj(i) db.session.add(i) db.session.commit() flash('Item added successfully') return redirect(url_for('masterItem')) except Exception as e: db.session.rollback() error = e return render_template('detail-item.html', form=form, error=error, mode='add')
def add_item(count, name, is_bought): for i in range(count): item = Item(name=name, is_bought=is_bought) db.session.add(item) db.session.commit()
def post(self): data = request.get_json() item = Item(name=data.get('name')) db.session.add(item) db.session.commit() return jsonify(item.as_dict())
from application import db from application.models import Warranty, Store, Item from application.schema import WarrantySchema, StoreSchema, ItemSchema db.create_all() warranty_schema = WarrantySchema() store_schema = StoreSchema() item_schema = ItemSchema() store = Store(uuid="b21ad0676f26439") db.session.add(store) item = Item(_type="furniture", sku="986kjeo8fy9qhu", cost=150.0, title="Amy's Sectional Sofa") db.session.add(item) db.session.commit() warranty_first = Warranty(store_id=store.id, item_id=item.id, warranty_price=10.7, warranty_duration_months=4) db.session.add(warranty_first) item = Item(_type="light", sku="186azkjeo8fy9qba", cost=75.5, title="Bed light") db.session.add(item) db.session.commit()
def setUp(self): """ create test database and client """ self.client = app.test_client() SECRET_KEY = app.config.get("SECRET_KEY") db.create_all() default_user = User(username="******", email="*****@*****.**") default_user.set_password(password="******") default_user2 = User(username="******", email="*****@*****.**") default_user2.set_password(password="******") default_user3 = User(username="******", email="*****@*****.**") default_user3.set_password(password="******") # add test bucketlists bucketlist1 = Bucketlist(name="Trip to Mombasa", date_created=datetime.utcnow(), created_by=default_user.username, author=default_user) bucketlist2 = Bucketlist(name="Charity Drive", date_created=datetime.utcnow(), created_by=default_user.username, author=default_user) bucketlist3 = Bucketlist(name="Dubai sky dive", date_created=datetime.utcnow(), created_by=default_user2.username, author=default_user2) # add test items item1 = Item(description="Test item no 1", bucketlist_id=bucketlist1.id, bucketlist=bucketlist1) item2 = Item(description="Test item no 2", bucketlist_id=bucketlist2.id, bucketlist=bucketlist2) item3 = Item(description="Test item no 3", bucketlist_id=bucketlist3.id, bucketlist=bucketlist3) db.session.add(default_user) db.session.add(default_user2) db.session.add(default_user3) db.session.add(bucketlist1) db.session.add(bucketlist2) db.session.add(bucketlist3) db.session.add(item1) db.session.add(item2) db.session.add(item3) db.session.commit() # User login user = {'username': '******', 'password': '******'} response = self.client.post('/auth/login', data=user) self.assertEqual(response.status_code, 200) # extract auth_token user_auth = json.loads(response.data) auth_token = user_auth['auth_token'] self.headers = {'auth_token': auth_token}
def setUp(self): self.user = User('mock-name', 'mock-pass') self.user.id = 123 self.item = Item('mock-item', self.user)