本文整理汇总了Python中models.Item类的典型用法代码示例。如果您正苦于以下问题:Python Item类的具体用法?Python Item怎么用?Python Item使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Item类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
def post(self):
form = AddItemForm()
item = Item()
if form.validate_on_submit():
ar_title = Titles()
fr_title = Titles()
en_title = Titles()
ar_title.title = form.ar_title.data.strip()
ar_title.lang = 'ar'
fr_title.title = form.fr_title.data.strip()
fr_title.lang = 'fr'
en_title.title = form.en_title.data.strip()
en_title.lang = 'en'
item.titles.append(ar_title)
item.titles.append(fr_title)
item.titles.append(en_title)
item.description = form.description.data
item.submitter = User.objects.get(id=current_user.id)
else:
flash('upload unsuccessful', 'error')
return render_template('items/add_item.html', form=form)
uploaded_files = request.files.getlist("files")
thumbnail = request.files['thumbnail']
thumbnail_name = secure_filename(thumbnail.filename)
if thumbnail and allowed_thumbnails(thumbnail_name):
ext = thumbnail.mimetype.split('/')[-1]
# use the 'thumbnail' name for all thumbnails
filename = '.'.join(["thumbnail", ext])
item.thumbnail.put(thumbnail.stream,
content_type=thumbnail.mimetype,
filename=filename)
for file in uploaded_files:
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Check if the file is one of the allowed types/extensions
if file and allowed_file(filename):
# put the file in the ListField.
# see https://gist.github.com/tfausak/1299339
file_ = GridFSProxy()
file_.put(file.stream,
content_type=file.mimetype,
filename=filename)
item.files.append(file_)
# Save the thing
item.save()
flash('upload successful')
return render_template('items/add_item.html', form=form)
示例2: test_update
def test_update(self):
order = Order(name="Dummy Order")
order.save()
for i in range(2):
item = Item(name="Item %i" % i, sku=str(i) * 13, price=D("9.99"), order=order, status=0)
item.save()
tag = Tag(name="Test", content_object=order)
tag.save()
res = self.client.get("/inlines/1/")
self.assertEqual(res.status_code, 200)
order = Order.objects.get(id=1)
self.assertEquals(2, order.item_set.count())
self.assertEquals("Item 0", order.item_set.all()[0].name)
data = {
"name": u"Dummy Order",
"item_set-TOTAL_FORMS": u"4",
"item_set-INITIAL_FORMS": u"2",
"item_set-MAX_NUM_FORMS": u"",
"item_set-0-name": "Bubble Bath",
"item_set-0-sku": "1234567890123",
"item_set-0-price": D("9.99"),
"item_set-0-status": 0,
"item_set-0-order": 1,
"item_set-0-id": 1,
"item_set-1-name": "Bubble Bath",
"item_set-1-sku": "1234567890123",
"item_set-1-price": D("9.99"),
"item_set-1-status": 0,
"item_set-1-order": 1,
"item_set-1-id": 2,
"item_set-2-name": "Bubble Bath",
"item_set-2-sku": "1234567890123",
"item_set-2-price": D("9.99"),
"item_set-2-status": 0,
"item_set-2-order": 1,
"item_set-3-DELETE": True,
"tests-tag-content_type-object_id-TOTAL_FORMS": 3,
"tests-tag-content_type-object_id-INITIAL_FORMS": 1,
"tests-tag-content_type-object_id-MAX_NUM_FORMS": u"",
"tests-tag-content_type-object_id-0-name": u"Test",
"tests-tag-content_type-object_id-0-id": 1,
"tests-tag-content_type-object_id-0-DELETE": True,
"tests-tag-content_type-object_id-1-name": u"Test 2",
"tests-tag-content_type-object_id-2-name": u"Test 3",
}
res = self.client.post("/inlines/1/", data, follow=True)
self.assertEqual(res.status_code, 200)
order = Order.objects.get(id=1)
self.assertEquals(3, order.item_set.count())
self.assertEquals(2, Tag.objects.count())
self.assertEquals("Bubble Bath", order.item_set.all()[0].name)
示例3: test_format_item_to_json
def test_format_item_to_json(self):
item = Item(1, "test name", "Medium", "This is a test description")
item.save()
results = item.to_json()
expected_results = dict(id=1, user_id=1, name="test name", priority="Medium",
description="This is a test description")
self.assertEqual(expected_results, results)
示例4: item_create
def item_create(replace_key=None):
if replace_key:
item = replace_key.get()
else:
url_safe_name = make_url_safe(request.form.get('name'))
item = Item(id = url_safe_name)
item.populate(
name = request.form.get('name'),
description = request.form.get('description'),
count = int(request.form.get('count')) if request.form.get('count') else 1,
prices = [float(request.form.get('price_int')) if request.form.get('price_int') else 0,
float(request.form.get('price_ext')) if request.form.get('price_ext') else 0,
float(request.form.get('price_com')) if request.form.get('price_com') else 0,
float(request.form.get('price_buy')) if request.form.get('price_buy') else 0,
],
tax_per_day = True if request.form.get('tax_per_day') else False,
category = request.form.get('category'),
)
id = item.put().id()
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
示例5: item_store
def item_store(category_name):
"""Store new item in DB"""
if 'username' not in login_session:
return redirect(url_for('login'))
category = Category.query.filter(Category.name == category_name).first()
if category.author_id != login_session['user_id']:
return render_template('401.html', name='item')
form = ItemForm()
form.category_id.choices = [(c.id, c.name)
for c in Category.query.order_by('name')]
form.category_id.data = category.id
if form.validate_on_submit():
try:
new_item = Item(form.name.data,
form.description.data,
category.id,
login_session['user_id'])
if 'image' in request.files and request.files['image']:
filename = images.save(request.files['image'])
new_item.image_name = filename
db_session.add(new_item)
db_session.commit()
return redirect(url_for('item_view',
category_name=category_name,
item_name=form.name.data))
except IntegrityError:
db_session.rollback()
flash("Item name must be unique value")
except Exception, e:
db_session.rollback()
print e
flash("Database error encountered")
示例6: save_edited_category
def save_edited_category():
cat_name = request.form.get("category")
key = request.form.get("key")
existing = Category.all().filter("owner =", users.get_current_user())
for e in existing:
if e.title.lower() == cat_name.lower():
if not str(e.key()) == key:
error = "You already have a category with that name. Please choose a different name"
return Response(status=400)
category = Category.get(key)
items_from_form = request.form.get("items").split(",")
old_items_from_db = Item.all().ancestor(category)
for item in old_items_from_db:
if not item.title in items_from_form:
db.delete(item)
else:
items_from_form.remove(item.title)
for new_item in items_from_form:
if not new_item == "":
i = Item(parent=category, title=new_item)
i.put()
category.title = request.form.get("category")
category.put()
return jsonify(new_items=items_from_form)
示例7: on_message
def on_message(self, message):
data = json.loads(message)
if "update_type" in data and data["update_type"] == "draw":
board_id = data["board_id"]
broadcastData = data
self.broadcast(board_id, broadcastData, write_to_self=False)
return
message_type = data.get('message_type')
board_id = data["board_id"]
if message_type and message_type == 'user_update':
self._update_user(board_id, data['username'])
self._broadcast_user_display(board_id)
elif message_type and message_type == 'board_name_update':
name = self._update_board_name(board_id, data['name'])
self._broadcast_board_name_update(board_id, name)
else:
board = Board.get(board_id)
item = Item(**data["item"])
board.updateItem(item)
board.save()
broadcastData = {
"update_type": "pos_change",
"item": json.loads(item.to_json())
}
self.broadcast(board_id, broadcastData, write_to_self=False)
示例8: create_item
def create_item(request):
"""
@param request:
@param id:
@return: JSON for account for added Inventory Item
"""
item = Item(purchase_account=Account.objects.get(name='Purchase', company=request.company),
sales_account=Account.objects.get(name='Sales', company=request.company),
purchase_tax_scheme=TaxScheme.objects.get(name='No Tax', company=request.company),
sales_tax_scheme=TaxScheme.objects.get(name='No Tax', company=request.company))
scenario = 'Create'
for query in request.GET:
setattr(item, query, request.GET[query])
if request.POST:
form = ItemForm(data=request.POST, instance=item, company=request.company)
if form.is_valid():
item = form.save(commit=False)
item.company = request.company
item.save()
if request.is_ajax():
return render(request, 'callback.html', {'obj': InventoryAccountSerializer(item.account).data})
return redirect('/inventory/items/')
else:
form = ItemForm(instance=item, company=request.company)
form.hide_field(request)
if request.is_ajax():
base_template = 'modal.html'
else:
base_template = 'dashboard.html'
return render(request, 'item_form.html', {
'scenario': scenario,
'form': form,
'base_template': base_template,
})
示例9: item_form
def item_form(request, id=None):
if id:
item = get_object_or_404(Item, id=id, company=request.company)
scenario = 'Update'
else:
item = Item(purchase_account=Account.objects.get(name='Purchase', company=request.company),
sales_account=Account.objects.get(name='Sales', company=request.company),
purchase_tax_scheme=TaxScheme.objects.get(name='No Tax', company=request.company),
sales_tax_scheme=TaxScheme.objects.get(name='No Tax', company=request.company))
scenario = 'Create'
if request.POST:
form = ItemForm(data=request.POST, instance=item, company=request.company)
if form.is_valid():
item = form.save(commit=False)
item.company = request.company
item.save()
if request.is_ajax():
return render(request, 'callback.html', {'obj': ItemSerializer(item).data})
return redirect('/inventory/items/')
else:
form = ItemForm(instance=item, company=request.company)
if request.is_ajax():
base_template = 'modal.html'
else:
base_template = 'dashboard.html'
return render(request, 'item_form.html', {
'scenario': scenario,
'form': form,
'base_template': base_template,
})
示例10: post_item
def post_item(request):
if request.method == 'POST':
form = ItemForm(request.POST, request.FILES)
if form.is_valid():
m_tags = form.cleaned_data['item_tags']
m = Item(
name=form.cleaned_data['item_name'],
# type=form.cleaned_data['item_type'],
# item_image=request.FILES['item_image'],
image_first = form.cleaned_data['image_first'],
image_second= form.cleaned_data['image_second'],
image_third= form.cleaned_data['image_third'],
looking_for = True if 'True' == form.cleaned_data['item_sellOrLookFor'] else False,
category = form.cleaned_data['item_category'],
#price=form.cleaned_data['item_price'],
negotiable=form.cleaned_data['item_negotiable'],
owner=request.user,
description=form.cleaned_data['item_description']
)
m.save()
print "item has been saved with item_id "+str(m.pk)
print m_tags
m.tags.add(*m_tags)
# return my_items(request)
return HttpResponseRedirect('/')
else:
form = ItemForm()
return render_to_response('post_item.html', {'form': form, 'user': request.user}, context_instance=RequestContext(request))
示例11: test_saving_and_retrieving_items
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)
示例12: create_digest
def create_digest(self, item_type, github_items):
"""
builds a DigestData instance filled with the digest
"""
issue_list = list(self.get_issues())
digest = DigestData(item_type)
digest.user = self._user
digest.repo = self._repository_name
for github_item in github_items:
if github_item.state == ItemStates.OPEN:
digest.total_opened += 1
elif github_item.state == ItemStates.CLOSED:
digest.total_closed += 1
digest.total_items += 1
item = Item()
item.url = github_item.html_url
item.label = '{}/{}#{}'.format(self._user, self._repository_name, github_item.number)
item.title = github_item.title
item.state = github_item.state
github_user = github_item.user
display_name = github_user.name or github_user.login
if display_name not in digest.users:
user = User()
user.name = display_name
user.gravatar = github_user.avatar_url
digest.users[display_name] = user
digest.items.setdefault(display_name, []).append(item)
return digest
示例13: test_update
def test_update(self):
order = Order(name='Dummy Order')
order.save()
for i in range(2):
item = Item(name='Item %i' % i, sku=str(i) * 13, price=D('9.99'), order=order, status=0)
item.save()
tag = Tag(name='Test', content_object=order)
tag.save()
res = self.client.get('/inlines/1/')
self.assertEqual(res.status_code, 200)
order = Order.objects.get(id=1)
self.assertEquals(2, order.item_set.count())
self.assertEquals('Item 0', order.item_set.all()[0].name)
data = {
'name': u'Dummy Order',
'item_set-TOTAL_FORMS': u'4',
'item_set-INITIAL_FORMS': u'2',
'item_set-MAX_NUM_FORMS': u'',
'item_set-0-name': 'Bubble Bath',
'item_set-0-sku': '1234567890123',
'item_set-0-price': D('9.99'),
'item_set-0-status': 0,
'item_set-0-order': 1,
'item_set-0-id': 1,
'item_set-1-name': 'Bubble Bath',
'item_set-1-sku': '1234567890123',
'item_set-1-price': D('9.99'),
'item_set-1-status': 0,
'item_set-1-order': 1,
'item_set-1-id': 2,
'item_set-2-name': 'Bubble Bath',
'item_set-2-sku': '1234567890123',
'item_set-2-price': D('9.99'),
'item_set-2-status': 0,
'item_set-2-order': 1,
'item_set-3-DELETE': True,
'tests-tag-content_type-object_id-TOTAL_FORMS': 3,
'tests-tag-content_type-object_id-INITIAL_FORMS': 1,
'tests-tag-content_type-object_id-MAX_NUM_FORMS': u'',
'tests-tag-content_type-object_id-0-name': u'Test',
'tests-tag-content_type-object_id-0-id': 1,
'tests-tag-content_type-object_id-0-DELETE': True,
'tests-tag-content_type-object_id-1-name': u'Test 2',
'tests-tag-content_type-object_id-2-name': u'Test 3',
}
res = self.client.post('/inlines/1/', data, follow=True)
self.assertEqual(res.status_code, 200)
order = Order.objects.get(id=1)
self.assertEquals(3, order.item_set.count())
self.assertEquals(2, Tag.objects.count())
self.assertEquals('Bubble Bath', order.item_set.all()[0].name)
示例14: import_inventory
def import_inventory(request):
from models import PCGSNumberException
shop = request.shop
failures = []
if request.method == 'POST':
remove_inventory = bool(request.POST['r'] == "True")
if remove_inventory:
items = Item.objects.filter(shop=shop)
for item in items:
item.delete()
for file in request.FILES.getlist('file'):
try:
from xls_parser import CoinInventoryParser
parser = CoinInventoryParser()
(keys, products) = parser.parse_xls(file)
for product in products:
if product == []: continue
try:
Item.create_from_inventory(shop, keys, product)
except PCGSNumberException:
logging.critical("PCGSNumber not exists for product %s" % product)
failures.append(product[0])
except Exception, e:
logging.error(e)
示例15: create
def create():
form = CreateItemForm(request.form)
if request.method == 'POST' and form.validate():
sheetmusic_id = form.sheetmusic_id.data
sheetmusic = Sheetmusic.query.filter_by(id=sheetmusic_id).one()
new_item = Item()
form.populate_obj(new_item)
new_item.user_id = g.user.id
g.db.add(new_item)
g.db.commit()
for file in request.files.getlist('images'):
if file:
ext = file.filename.rsplit('.', 1)[-1]
filename = g.user.username + '_' + str(uuid4()) + '.' + ext
image = ItemImage(filename)
save_image(file, filename)
new_item._images.append(image)
g.db.commit()
flash("You just made a new item!", "success")
return redirect(url_for('items.index', item_id=new_item.id))
sheetmusic_id = int(request.args.get('sheetmusic_id', 0))
if sheetmusic_id:
form.sheetmusic_id.data = sheetmusic_id
return render_template('items/create_item.html', form=form)