本文整理汇总了Python中products.models.Product.size方法的典型用法代码示例。如果您正苦于以下问题:Python Product.size方法的具体用法?Python Product.size怎么用?Python Product.size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类products.models.Product
的用法示例。
在下文中一共展示了Product.size方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _product
# 需要导入模块: from products.models import Product [as 别名]
# 或者: from products.models.Product import size [as 别名]
def _product(self, item):
try:
product = Product.objects.get(wholesale_legacy_id=int(item['id']), is_visible=True)
except Product.DoesNotExist:
product = Product()
except Product.MultipleObjectsReturned:
print item['id']
for p in Product.objects.filter(wholesale_legacy_id=int(item['id'])):
print p.id, p.title, p.code, p.sku
print '-----'
raise
quantity = int(item.get('qtyi', 0))
if quantity < 0:
quantity = 0
weight = int(item.get('weight', 0))
if weight < 0:
weight = 0
product.sku = item['article']
product.weight = weight
product.quantity = quantity
product.code = item['link']
product.title = item['name']
product.description = item['descr']
product.is_visible = bool(int(item['pub']))
product.wholesale_legacy_id = item['id']
product.created = TIMEZONE.localize(datetime.datetime.fromtimestamp(int(item['dt'])))
size = None
try:
size = SIZE_RE.findall(u' '.join([item['name'], item.get('alt', ''), item['descr']]))[0]
except IndexError:
try:
size = SIZE2_RE.findall(u' '.join([item['name'], item.get('alt', ''), item['descr']]))[0]
except IndexError:
pass
if size:
product.size = u'×'.join([str(x) for x in size])
return product
示例2: _product
# 需要导入模块: from products.models import Product [as 别名]
# 或者: from products.models.Product import size [as 别名]
def _product(self, item):
try:
product = Product.objects.get(id=int(item['id']))
except Product.DoesNotExist:
product = Product(id=int(item['id']))
quantity = int(item.get('qtyi', 0))
if quantity < 0:
quantity = 0
weight = int(item.get('weight', 0))
if weight < 0:
weight = 0
product.sku = item['article']
product.weight = weight
product.quantity = quantity
product.code = item['link']
product.title = item['name']
product.description = item['descr']
product.is_visible = bool(int(item['pub']))
product.is_wholesale = False
product.created = TIMEZONE.localize(datetime.datetime.fromtimestamp(int(item['dt'])))
size = None
try:
size = SIZE_RE.findall(u' '.join([item['name'], item.get('alt', ''), item['descr']]))[0]
except IndexError:
try:
size = SIZE2_RE.findall(u' '.join([item['name'], item.get('alt', ''), item['descr']]))[0]
except IndexError:
pass
if size:
product.size = u'×'.join([str(x) for x in size])
return product
示例3: handle
# 需要导入模块: from products.models import Product [as 别名]
# 或者: from products.models.Product import size [as 别名]
def handle(self, *args, **options):
"""
Scrapes and stores product information
"""
# get beer page html and make soup object
html = urllib2.urlopen(TOP_URL + "/beers/search")
soup_beers = BeautifulSoup(html)
# find all beers
beers = soup_beers.find_all("a", "brand-link")
for beer in beers:
# get beer page and make soup object
beer_url = beer["href"]
beer_html = urllib2.urlopen(TOP_URL + beer_url)
soup_beer = BeautifulSoup(beer_html)
# get sizes
beer_products = soup_beer.find_all("table", "brand-pricing")
# get propertis and valus and merge them into dict
labels = soup_beer.dl.find_all("dt")
details = soup_beer.dl.find_all("dd")
beer_details = dict(zip(labels,details))
# get name and image
beer_name = soup_beer.find("div", "only-desktop").find("h1", "page-title").get_text()
beer_image = soup_beer.find("div","brand-image").img["src"]
# get country and type
beer_attributes = soup_beer.find("p","introduction").find_all("span")
beer_attributes = beer_attributes[::-1]
beer_country = beer_attributes[0].get_text()
beer_type = beer_attributes[1].get_text()
# loop through beer products
for beer_product in beer_products:
beer_containers = beer_product.find_all("tbody")
# loop through container tables
for beer_container in beer_containers:
beer_sizes = beer_container.find_all("tr")
# loop through container sizes
for beer_size in beer_sizes:
# get product information
beer_ids = beer_size.a["href"].split('=')[1]
beer_id = beer_ids.split('-')[0]
print beer_id
beer_product_id = beer_ids.split('-')[1]
# Comment to disable monitoring
beer_product_size = beer_size.find("td","size").get_text()
beer_product_price = beer_size.find("td","price").get_text()
# check if product exists
# NOTE: used this custom solution because django get_or_create
# doesn't play nice with custom primary keys
try:
product_entry = Product.objects.get(product_id=int(beer_product_id.strip()))
except:
product_entry = Product()
# set fields
product_entry.name = beer_name.strip()
product_entry.size = beer_product_size.strip()
product_entry.beer_id = int(beer_id.strip())
product_entry.product_id = int(beer_product_id.strip())
product_entry.image_url = beer_image.strip()
product_entry.country = beer_country.strip()
product_entry.type = beer_type.strip()
# set product attributes
# NOTE: this code was created befor the beer store redesign
# it still works but some items no longer exist so they were
# temporarily omitted from the serializer
for key, value in beer_details.iteritems():
attr = key.get_text()[:-1]
val = value.get_text()
if attr == 'Category':
product_entry.category = val
if attr == 'Alcohol Content (ABV)':
product_entry.abv = float(val[:-1])
if attr == 'Style':
product_entry.style= val
if attr == 'Attributes':
product_entry.attributes= val
if attr == 'Brewer':
product_entry.brewer= val
# update pricing info
try:
product_entry.price = float(beer_product_price.strip()[1:])
#.........这里部分代码省略.........