def test_create_item_gets_modification_date(db, user): item = Item(user=user) old_modified = item.modified item.save() item = Item.objects.get(id=item.id) new_modified = item.modified delta = new_modified - old_modified assert delta.microseconds
def import_stock_report_dump(sourcefile): with open(sourcefile) as f: reader = csv.DictReader(f) for row in reader: site = Location.get_by_code(row['Site ID']) if not site: print(row) continue reporter = Connection.objects.get(identity=row['Mobile']).contact.worker created = utc.localize(datetime.utcfromtimestamp(int(row['Timestamp']))) logs = [] for chunk in row['Items'].split(';'): bits = chunk.split() item = Item.get_by_code(bits[0]) received = int(bits[1]) holding = int(bits[2]) logs.append(InventoryLog.objects.create(item=item, last_quantity_received=received, current_holding=holding)) report = StockReport.objects.create(site=site, reporter=reporter, created=created) report.logs.add(*logs) print('Done')
def scrap_itens(): desejos = Desejos.objects.all() for desejo in desejos: url = desejo.url headers = { "User-Agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36' } page = requests.get(url, headers=headers) soup = bs(page.content, 'html.parser') soup2 = bs(soup.prettify(), 'html.parser') spans = soup2.find_all('span', class_='a-price-whole') prices = [ str(price.get_text()).replace(' ', '').replace('\n', '').replace(',', '') for price in spans ] links = soup2.find_all('a', class_='a-link-normal') titles = [link['title'] for link in links if 'itemName_' in str(link)] ids = [link['id'] for link in links if 'itemName_' in str(link)] bulkCreate = [] bulkUpdate = [] for id_, price, title in zip(ids, prices, titles): if Item.objects.filter(id=id_).exists(): item = Item.objects.get(id=id_) if (item.minPrice > price and (item.dateCreated - date.today()).days >= 7) or (price <= ( ((100 - desejo.percent) / 100) * item.priceDayAdd)): bulkUpdate.append(Item(id=id_, minPrice=price)) #Notificar por email else: bulkCreate.append( Item(id=id_, lista=desejo, minPrice=price, priceDayAdd=price, title=title)) if len(bulkCreate) > 0: Item.objects.bulk_create(bulkCreate) if len(bulkUpdate) > 0: Item.objects.bulk_update(bulkUpdate, fields=['minPrice'])
def products(request): """ Returns last 10 items ordered by price """ if request.method == 'POST': try: item = Item(dbid = request.POST['dbid'], price = float(request.POST.get('price', 0)), delivery = request.POST['delivery'], kid_adult = bool(int(request.POST.get('kid_adult', 0))), kids = bool(int(request.POST.get('kids', 0))), women = bool(int(request.POST.get('women', 0))), name = request.POST['name'], sizes = request.POST['sizes'], url = request.POST['url'], free_porto = bool(int(request.POST.get('free_porto', 0))), package = bool(int(request.POST.get('package', 0))), price_old = float(request.POST.get('price_old', 0)), img_url = request.POST['img_url'] ) item.save() return HttpResponse("Item created.", status=201) except MultiValueDictKeyError: return HttpResponse("Some fields are missing from POST parameters.", status=500) # Get items and create paginator items_ = Item.objects.order_by('price') p = Paginator(items_, 10) page = request.GET.get('page') try: items = p.page(page) except PageNotAnInteger: # Deliver first page, if page is not an integer or does not specified items = p.page(1) # Prepare items for JSON serialization. data = [] for item in items: i_ = {} for field in Item._meta.fields: i_[field.name] = getattr(item, field.name) data.append(i_) return HttpResponse(json.dumps(data), content_type='application/json')
def getAllNewProducts(request, page=1): products = Product.objects.filter(sites=settings.SITE_ID).order_by("-pk") result = func.setPaginationForItemsWithValues(products, "NAME", 'DETAIL_TEXT', 'IMAGE', 'COST', 'CURRENCY', page_num=15, page=page) products_list = result[0] products_ids = [key for key, value in products_list.items()] companies = Company.objects.filter(p2c__child_id__in=products_ids) items = Item.objects.filter(p2c__child_id__in=companies, p2c__type="dependence", pk__in=Country.objects.all(), p2c__child__p2c__child__in=products_ids).values("country", "p2c__child_id", 'p2c__child__p2c__child', 'pk') items_id =[] for item in items: items_id.append(item['pk']) items_id.append(item['p2c__child_id']) items_id = set(items_id) itemsWithAttribute = Item.getItemsAttributesValues(("NAME", "IMAGE"), items_id) for item in items: products_list[item['p2c__child__p2c__child']].update({'COMPANY_NAME': itemsWithAttribute[item['p2c__child_id']]['NAME']}) products_list[item['p2c__child__p2c__child']].update({'COMPANY_IMAGE': itemsWithAttribute[item['p2c__child_id']]['IMAGE']}) products_list[item['p2c__child__p2c__child']].update({'COMPANY_ID': item['p2c__child_id']}) products_list[item['p2c__child__p2c__child']].update({'COUNTRY_NAME': itemsWithAttribute[item['pk']]['NAME']}) products_list[item['p2c__child__p2c__child']].update({'COUNTRY_ID': item['pk']}) page = result[1] paginator_range = func.get_paginator_range(page) url_paginator = "products:products_paginator" return render_to_response("Product/new.html", {'products_list': products_list, 'page': page, 'paginator_range':paginator_range, 'url_paginator': url_paginator, 'user': request.user}, context_instance=RequestContext(request))
def add_empty_items_to_db(self, items: List[Dict]) -> List[Item]: current_time = now() new_items, old_items = [], [] self.lock.acquire() for i, item in enumerate(items): create_params = { 'name': item['name'], 'marketplace_id': item['marketplace_id'], 'marketplace_source': self.marketplace_source, 'next_parse_time': current_time } get_params = { 'marketplace_id': item['marketplace_id'], 'marketplace_source': self.marketplace_source } try: old_items.append( Item.objects.get( **{ 'marketplace_id': 12731124, 'marketplace_source': self.marketplace_source })) old_items.append(Item.objects.get(**get_params)) except Item.DoesNotExist: new_items.append(Item(**create_params)) continue except Item.MultipleObjectsReturned: logger.error( f'Something is wrong. You should get one object for params: {get_params}' ) existing_items = Item.objects.filter(**get_params) old_items.append(existing_items[0]) for duplicate_item in existing_items[1:]: duplicate_item.delete() if new_items: new_items = Item.objects.bulk_create(new_items) self.lock.release() all_items = old_items + new_items if len(all_items) > 0: all_items.sort(key=lambda x: x.marketplace_id) return all_items
def newsDetail(request, item_id): new = get_object_or_404(News, pk=item_id) user = request.user newAttr = new.getAttributeValues("NAME", "ACTIVE_FROM", "DETAIL_TEXT", "IMAGE") newAttr = newAttr contrySorted = func.sortByAttr("Country", "NAME") sorted_id = [coun.id for coun in contrySorted] countryList = Item.getItemsAttributesValues(("NAME", ), sorted_id) products = Product.active.get_active_related().filter( sites=settings.SITE_ID).order_by("-pk")[:2] newProducrList = Product.getCategoryOfPRoducts( products, ("NAME", "COST", "CURRENCY", "IMAGE")) return render_to_response("News/detail.html", locals())
def import_stockout_dump(sourcefile): with open(sourcefile) as f: reader = csv.DictReader(f) for row in reader: site = Location.get_by_code(row['Site ID']) reporter = Connection.objects.get(identity=row['Mobile']).contact.worker created = utc.localize(datetime.utcfromtimestamp(int(row['Timestamp']))) modified = created stockout = StockOutReport.objects.create(site=site, reporter=reporter, created=created, modified=modified) items = [] for item_code in row['Items'].split(','): items.append(Item.get_by_code(item_code.strip())) stockout.items.add(*items) print('Done')
def newsList(request, page=1): user = request.user page = page result = func.getItemsListWithPagination("News", "NAME", "Active_From", "DETAIL_TEXT", "IMAGE", page=page) newsList = result[0] page = result[1] paginator_range = func.get_paginator_range(page) url_paginator = "news:paginator" contrySorted = func.sortByAttr("Country", "NAME") sorted_id = [coun.id for coun in contrySorted] countryList = Item.getItemsAttributesValues(("NAME", ), sorted_id) return render_to_response("News/index.html", locals())
def AddNewItem(request, slug): if request.user.is_anonymous(): ItemOwner = request.POST.get('ItemOwner') ItemOwnerState = "non-confirmed" ItemOwnerAvtr = "" ItemOwnerPrvdr = "" ItemOwnerFN = "" ItemOwnerLN = "" ItemOwnerLink = "" else: for account in request.user.socialaccount_set.all(): ItemOwnerPrvdr = account.provider if ItemOwnerPrvdr == "facebook": ItemOwner = account.extra_data['username'] ItemOwnerFN = account.extra_data['first_name'] ItemOwnerLN = account.extra_data['last_name'] ItemOwnerLink = account.extra_data['link'] ItemOwnerState = "confirmed" ItemOwnerAvtr = "https://graph.facebook.com/" + account.extra_data[ 'id'] + "/picture?type=large" elif ItemOwnerPrvdr == "twitter": ItemOwner = account.extra_data['screen_name'] ItemOwnerFN = account.extra_data['name'] ItemOwnerLN = "" ItemOwnerLink = "https://twitter.com/" + account.extra_data[ 'screen_name'] ItemOwnerState = "confirmed" ItemOwnerAvtr = account.extra_data['profile_image_url_https'] elif ItemOwnerPrvdr == "google": ItemOwner = account.extra_data['name'] ItemOwnerFN = account.extra_data['id'] ItemOwnerLN = "" ItemOwnerLink = "https://plus.google.com/" + account.extra_data[ 'id'] ItemOwnerState = "confirmed" ItemOwnerAvtr = account.extra_data['picture'] elif ItemOwnerPrvdr == "persona": ItemOwner = account.user ItemOwnerFN = account.user ItemOwnerLN = "" ItemOwnerLink = "mailto:" + account.extra_data['email'] ItemOwnerState = "confirmed" ItemOwnerAvtr = "/static/buttons/Persona.png" else: pass content = request.POST['content'] ItemDone = False WhoDone = "" TimeDone = timezone.now() list = get_object_or_404(List, slug=slug) p = Item( list=list, ItemOwner=ItemOwner, ItemOwnerFN=ItemOwnerFN, ItemOwnerLN=ItemOwnerLN, ItemOwnerPrvdr=ItemOwnerPrvdr, ItemOwnerLink=ItemOwnerLink, content=content, ItemOwnerAvtr=ItemOwnerAvtr, ItemOwnerState=ItemOwnerState, TimeDone=TimeDone, WhoDone=WhoDone, ItemDone=ItemDone, ) p.save() return HttpResponseRedirect(reverse('sopler:list', args=(list.slug, )))
def parse_redisq(json): package = ujson.loads(json)['package'] killmail = package['killmail'] victim = killmail['victim'] zkb = package['zkb'] # Check the KM doesn't already exist if Killmail.objects.filter(id=package['killID']).count() > 0: print("Kill ID %s already exists" % package['killID']) return # Populate killmail km = Killmail(id=package['killID'], source_id=1, date=parse_datetime(killmail['killmail_time']), system_id=killmail['solar_system_id'], ship_id=victim['ship_type_id'], value=zkb['totalValue'], damage=victim['damage_taken'], attackers=len(killmail['attackers'])) if "position" in victim: km.x = victim['position']['x'] km.y = victim['position']['y'] km.z = victim['position']['z'] db_victim = Involved(kill=km, attacker=False, ship_id=victim['ship_type_id'], damage=0) if "character_id" in victim: db_victim.character = Character.get_or_create(victim['character_id']) if "corporation_id" in victim: db_victim.corporation = Corporation.get_or_create( victim['corporation_id']) if "alliance_id" in victim: db_victim.alliance = Alliance.get_or_create(victim['alliance_id']) km.save() # Populate Involved attackers = [db_victim] for attacker in killmail['attackers']: a = Involved(kill=km, damage=attacker['damage_done']) if "final_blow" in attacker: a.final_blow = attacker['final_blow'] if "character_id" in attacker: a.character = Character.get_or_create(attacker['character_id']) if "corporation_id" in attacker: a.corporation = Corporation.get_or_create( attacker['corporation_id']) if "alliance_id" in attacker: a.alliance = Alliance.get_or_create(attacker['alliance_id']) if "ship_type_id" in attacker: a.ship_id = attacker['ship_type_id'] if "weapon_type_id" in attacker: a.weapon_id = attacker['weapon_type_id'] #a.save() attackers.append(a) Involved.objects.bulk_create(attackers) # Populate Items items = [] for item in victim['items']: i = Item(kill=km, type_id=item['item_type_id'], singleton=item['singleton'], flag=item['flag']) if "quantity_dropped" in item: i.quantity = item['quantity_dropped'] if "quantity_destroyed" in item: i.quantity = item['quantity_destroyed'] #i.save() items.append(i) Item.objects.bulk_create(items) print("Added Kill ID %s on %s with %s involved from Redisq" % (km.id, km.date.strftime("%d/%m/%Y %H:%M"), len(attackers)))
def AddNewItem(request, slug): if request.user.is_anonymous(): ItemOwner = request.POST.get('ItemOwner') ItemOwnerState = "non-confirmed" ItemOwnerAvtr = "" ItemOwnerPrvdr = "" ItemOwnerFN = "" ItemOwnerLN = "" ItemOwnerLink = "" else: for account in request.user.socialaccount_set.all(): ItemOwnerPrvdr = account.provider if ItemOwnerPrvdr == "facebook": ItemOwner = account.extra_data['username'] ItemOwnerFN = account.extra_data['first_name'] ItemOwnerLN = account.extra_data['last_name'] ItemOwnerLink = account.extra_data['link'] ItemOwnerState = "confirmed" ItemOwnerAvtr = "https://graph.facebook.com/" + account.extra_data['id'] + "/picture?type=large" elif ItemOwnerPrvdr == "twitter": ItemOwner = account.extra_data['screen_name'] ItemOwnerFN = account.extra_data['name'] ItemOwnerLN = "" ItemOwnerLink = "https://twitter.com/" + account.extra_data['screen_name'] ItemOwnerState = "confirmed" ItemOwnerAvtr = account.extra_data['profile_image_url_https'] elif ItemOwnerPrvdr == "google": ItemOwner = account.extra_data['name'] ItemOwnerFN = account.extra_data['id'] ItemOwnerLN = "" ItemOwnerLink = "https://plus.google.com/" + account.extra_data['id'] ItemOwnerState = "confirmed" ItemOwnerAvtr = account.extra_data['picture'] elif ItemOwnerPrvdr == "persona": ItemOwner = account.user ItemOwnerFN = account.user ItemOwnerLN = "" ItemOwnerLink = "mailto:"+account.extra_data['email'] ItemOwnerState = "confirmed" ItemOwnerAvtr = "/static/buttons/Persona.png" else: pass content = request.POST['content'] ItemDone = False WhoDone = "" TimeDone = timezone.now() list = get_object_or_404(List, slug=slug) p = Item( list=list, ItemOwner=ItemOwner, ItemOwnerFN = ItemOwnerFN, ItemOwnerLN = ItemOwnerLN, ItemOwnerPrvdr = ItemOwnerPrvdr, ItemOwnerLink = ItemOwnerLink, content = content, ItemOwnerAvtr = ItemOwnerAvtr, ItemOwnerState = ItemOwnerState, TimeDone = TimeDone, WhoDone = WhoDone, ItemDone=ItemDone, ) p.save() return HttpResponseRedirect(reverse('sopler:list', args=(list.slug,)))
def handle(self, *args, **options): url = 'http://www.unisport.dk/api/sample/' # Read items from URL logging.info('Reading data...') request = urllib2.urlopen(url) items_str = request.read() items = ast.literal_eval(items_str)['latest'] logging.info('Data evaluated.') logging.info('Now importing...') # Go through items for item_ in items: try: # Create item and save into Db item = Item() item.dbid = item_['id'] item.price = float(item_['price'].replace(',','.')) item.delivery = item_['delivery'] item.kid_adult = int(item_['kid_adult']) item.kids = int(item_['kids']) item.women = int(item_['women']) item.name = item_['name'].decode('unicode_escape') item.sizes = item_['sizes'] item.url = item_['url'] item.free_porto = int(item_['free_porto']) item.package = item_['package'] item.price_old = float(item_['price_old'].replace(',','.')) item.img_url = item_['img_url'] item.save() logging.info('Imported item %s' % item.dbid) except: logging.error('Failed to import %s' % item_['dbid']) # All done logging.info('All imported')
def test_create_item_without_user(db): item = Item() item.save()
def _update_existing_item(item: Item, item_info: Dict, fields_to_update: List[str]) -> None: for field in fields_to_update: item.__setattr__(field, item_info.get(field))
def handle(self, *args, **options): printful_api_base = 'https://api.printful.com/' key_bytes = settings.PRINTFUL_KEY.encode('utf-8') b64Val = base64.b64encode(key_bytes) key_decoded = b64Val.decode('utf-8') headers = { 'content-type': 'application/json', 'Authorization': "Basic %s" %key_decoded } url = printful_api_base + "store/products" response = requests.get(url, headers=headers) data = response.json()['result'] print("data: ", data) print("data length: ", len(data)) count = 1 for item in data: url = printful_api_base + "store/products" print("COUNT: ", count) print("item: ", item) item_id = item["id"] print("item_id: ", item_id) url = url + "/" + str(item_id) print("URL: ", url) response = requests.get(url, headers=headers) variant_data = response.json()["result"] sync_product = variant_data['sync_product'] print("sync_product: ", sync_product) new_item = Item( title = sync_product["name"], # price = models.DecimalField(blank=True, null=True, max_digits=10, decimal_places=2) # discount_price = models.DecimalField(blank=True, null=True, max_digits=10, decimal_places=2) # category = models.CharField(choices=CATEGORY_CHOICES, max_length=2, blank=True, null=True) # label = models.CharField(choices=LABEL_CHOICES, max_length=1, blank=True, null=True) # slug = models.SlugField(blank=True, null=True) # description = models.TextField(blank=True, null=True) # image = models.ImageField(blank=True, null=True) printful_product_id = sync_product["id"], thumbnail_url = sync_product["thumbnail_url"], printful_name = sync_product["name"], ) new_item.save() for variant in variant_data["sync_variants"]: print("variant: ", variant) new_item_variant = ItemVariant( title = variant["name"], item = new_item, printful_variant_id = variant["variant_id"], printful_item_variant_id = variant["id"], retail_price = variant["retail_price"], sku = variant["sku"], product_id = variant["id"], ) new_item_variant.save() print_file in variant["files"]: new_item_variant_file = ItemVariantFiles( file_id = print_file["id"], item_variant = new_item_variant, url = , file_name = ) new_item_variant_file.save() count += 1
def parse_esi(json=None, keyhash=None, attempts=0, source_id=3): if json != None: package = ujson.loads(json) victim = package['victim'] if Killmail.objects.filter(id=package['killmail_id']).count() > 0: print("Kill ID %s already exists" % package['killmail_id']) return else: if Killmail.objects.filter(id=keyhash[0]).count() > 0: print("Kill ID %s already exists" % keyhash[0]) return package = requests.get( "https://esi.tech.ccp.is/latest/killmails/%s/%s/" % (keyhash[0], keyhash[1])) if package.status_code != 200: if attempts < 5: sleep(1) return parse_esi(keyhash=keyhash, attempts=attempts + 1) else: print("Error fetching kill ID %s from ESI" % keyhash[0]) return None package = package.json() victim = package['victim'] # Pre-load char/corp/alliance sets so we can fill the database chars = set() corps = set() alliances = set() if "character_id" in victim: chars.add(victim['character_id']) if "corporation_id" in victim: corps.add(victim['corporation_id']) if "alliance_id" in victim: alliances.add(victim['alliance_id']) for attacker in package['attackers']: if "character_id" in attacker: chars.add(attacker['character_id']) if "corporation_id" in attacker: corps.add(attacker['corporation_id']) if "alliance_id" in attacker: alliances.add(attacker['alliance_id']) # Now call get_or_create to preload them in the database for char in chars - set( Character.objects.filter(id__in=chars).values_list('id', flat=True)): Character.get_or_create(char) for corp in corps - set( Corporation.objects.filter(id__in=corps).values_list('id', flat=True)): Corporation.get_or_create(corp) for alliance in alliances - set( Alliance.objects.filter(id__in=alliances).values_list('id', flat=True)): Alliance.get_or_create(alliance) # Populate killmail and data within transaction with transaction.atomic(): km = Killmail(id=package['killmail_id'], source_id=source_id, date=parse_datetime(package['killmail_time']), system_id=package['solar_system_id'], ship_id=victim['ship_type_id'], value=0, damage=victim['damage_taken'], attackers=len(package['attackers'])) if "position" in victim: if "x" in victim['position']: km.x = victim['position']['x'] km.y = victim['position']['y'] km.z = victim['position']['z'] db_victim = Involved(kill=km, attacker=False, ship_id=victim['ship_type_id'], damage=0) if "character_id" in victim: db_victim.character = Character.get_or_create( victim['character_id']) if "corporation_id" in victim: db_victim.corporation = Corporation.get_or_create( victim['corporation_id']) if "alliance_id" in victim: db_victim.alliance = Alliance.get_or_create(victim['alliance_id']) km.save() # Populate attackers attackers = [db_victim] for attacker in package['attackers']: a = Involved(kill=km, damage=attacker['damage_done']) if "final_blow" in attacker: a.final_blow = attacker['final_blow'] if "character_id" in attacker: a.character_id = attacker['character_id'] if "corporation_id" in attacker: a.corporation_id = attacker['corporation_id'] if "alliance_id" in attacker: a.alliance_id = attacker['alliance_id'] if "ship_type_id" in attacker: a.ship_id = attacker['ship_type_id'] if "weapon_type_id" in attacker: a.weapon_id = attacker['weapon_type_id'] #a.save() attackers.append(a) Involved.objects.bulk_create(attackers) # Populate Items items = [] for item in victim['items']: i = Item(kill=km, type_id=item['item_type_id'], singleton=item['singleton'], flag=item['flag']) if "quantity_dropped" in item: i.quantity = item['quantity_dropped'] if "quantity_destroyed" in item: i.quantity = item['quantity_destroyed'] #i.save() items.append(i) Item.objects.bulk_create(items) # Calculate the cost of the ship km.value = km.items.annotate(value=ExpressionWrapper( F('type__sell') * F('quantity'), output_field=FloatField())).aggregate( total_value=Sum('value'))['total_value'] if km.value == None: km.value = float(km.ship.sell) else: km.value = km.value + float(km.ship.sell) km.save() print("Added Kill ID %s on %s with %s involved from ESI" % (km.id, km.date.strftime("%d/%m/%Y %H:%M"), len(attackers))) return km
def orderProduct(request, step=1): #------ Order of products in threee step ----# if not request.user.is_authenticated(): url_product = reverse("products:detail", args=[request.POST.get('product', 1)]) return HttpResponseRedirect("/registration/?next=%s" %url_product) contrySorted = func.sortByAttr("Country", "NAME") sorted_id = [coun.id for coun in contrySorted] countryList = Item.getItemsAttributesValues(("NAME",), sorted_id) curr_url = 'order' if step == '1': #-----Step One , form of shipping addres plus deleviry method ---# orderForm = "" user = request.user if not request.session.get('product_id', False) or request.POST.get('product', False): request.session['product_id'] = request.POST.get('product', "") request.session['qty'] = request.POST.get('french-hens', "") if request.session.get("order", False): del request.session['order'] cabinet = Cabinet.objects.get(user=user) if request.POST.get('product', False) or not request.session.get("order", False): address = cabinet.getAttributeValues("ADDRESS_CITY", "ADDRESS_COUNTRY", "ADDRESS_ZIP", "ADDRESS", "TELEPHONE_NUMBER", "SHIPPING_NAME") else: session = request.session.get("order", False) if session: address = {"ADDRESS_CITY": [session.get("city", "")], "ADDRESS_COUNTRY": [session.get("country", "")], "ADDRESS_ZIP": [session.get("zipcode", "")], "ADDRESS": [session.get("address", "")], "TELEPHONE_NUMBER": [session.get("telephone_number", "")], "SHIPPING_NAME": [session.get("recipient_name", "")]} if request.POST.get("Continue", False): orderForm = OrderForm(request.POST) if orderForm.is_valid(): if request.POST.get("delivery", False): request.session['order'] = request.POST return HttpResponseRedirect(reverse("products:order", args=['2'])) else: orderForm.errors.update({"delivery": "Required deleviry method"}) product = get_object_or_404(Product, pk=request.session.get("product_id", " ")) productValues = product.getAttributeValues("NAME", "IMAGE", "CURRENCY", "COST", 'DISCOUNT', 'COUPON_DISCOUNT') productValues['COST'][0] = _getRealCost(productValues) totalCost = float( productValues['COST'][0]) * float(request.session.get('qty', 1)) return render_to_response("Product/orderStepOne.html", {'address': address, 'user': user, "orderForm": orderForm, 'productValues': productValues ,'totalCost': totalCost, 'curr_url': curr_url}, context_instance=RequestContext(request)) elif step == '2': #----Step two , checkout , and conformation of order----# product_id = request.session.get('product_id', False) qty = request.session.get('qty', False) product = get_object_or_404(Product, pk=product_id) productValues = product.getAttributeValues('NAME', "COST", 'CURRENCY', 'DISCOUNT', 'COUPON_DISCOUNT') orderDetails = {} session = request.session.get("order", " ") orderDetails['city'] = session['city'] orderDetails['country'] = session['country'] orderDetails['address'] = session['address'] productValues['COST'][0] = _getRealCost(productValues) totalSum = float(productValues['COST'][0]) * float(request.session.get('qty', 1)) user = request.user return render_to_response("Product/orderStepTwo.html", {'qty': qty, 'productValues': productValues, 'orderDetails': orderDetails, 'totalSum': totalSum, "user": user,'curr_url': curr_url, },context_instance=RequestContext(request)) else: #-----Step three , cleaning of sessions , and creatin of new order object that related to cabinet of user ---# if request.session.get("order", False): product = get_object_or_404(Product, pk=request.session.get('product_id')) productValues = product.getAttributeValues('NAME', "COST", 'CURRENCY', 'IMAGE', 'DISCOUNT', 'COUPON_DISCOUNT') qty = request.session.get('qty', False) dict = Dictionary.objects.get(title='CURRENCY') slot_id = dict.getSlotID(productValues['CURRENCY'][0]) productValues['CURRENCY'][0] = slot_id session = request.session['order'] address = {"ADDRESS_CITY": [session.get("city", "")], "ADDRESS_COUNTRY": [session.get("country", "")], "ADDRESS_ZIP": [session.get("zipcode", "")], "ADDRESS": [session.get("address", "")], "TELEPHONE_NUMBER": [session.get("telephone_number", "")], "DETAIL_TEXT": [session.get("comment", "")], "SHIPPING_NAME": [session.get("recipient_name", "")]} productValues['COST'][0] = float(_getRealCost(productValues)) * float(qty) with transaction.atomic(): order = Order(create_user=request.user) order.save() orderDict = {} orderDict.update(productValues) orderDict.update(address) orderDict.update({"QUANTITY": qty}) del orderDict['CREATE_DATE'] order.setAttributeValue(orderDict, request.user) cabinet = Cabinet.objects.get(user=request.user) Relationship.setRelRelationship(cabinet, order, request.user) Relationship.setRelRelationship(order, product, request.user) del request.session['product_id'] del request.session['qty'] del request.session['order'] else: return HttpResponseRedirect("/") return render_to_response("Product/orderStepThree.html", {"user": request.user, 'curr_url': curr_url }, context_instance=RequestContext(request))
def pull_vinted_products(): # sleep(10) creds = { 'login': settings.VINTED_LOGIN, 'password': settings.VINTED_PASSWORD } session = Vinted(creds=creds) session.login().content.decode('utf-8') friend_id = '12813951' # '36544471' print("Getting items for ", friend_id) i_member_info, i_all_items = session.get_items4member(friend_id) print("Got items from vinted", i_all_items) for item in i_all_items: an_item = Item() an_item.id = int(item["id"]) an_item.title = item["title"] an_item.price = float(item["price_numeric"]) an_item.discount_price = float(item["price_numeric"]) an_item.category = 'OW' an_item.label = "P" an_item.slug = slugify("{}{}".format(an_item.title, an_item.id)) url = item["photos"][0]['full_size_url'] img_filename = "{}.jpg".format(an_item.id) # (uuid.uuid4().hex) img_temp = NamedTemporaryFile(delete=True) img_temp.write(urlopen(url).read()) img_temp.flush() an_item.image.save(img_filename, File(img_temp)) an_item.save() print("Item {} processed.".format(an_item.title)) return None
def add_items_to_db(self, items: List[Dict]) -> List[Item]: self.lock.acquire() brand_id_to_idx, colour_id_to_idx, seller_id_to_idx, items_info = self._aggregate_info_from_items( items) self._fill_nones_in_items(brand_id_to_idx, items_info, Brand, 'brand', 'marketplace_id') self._fill_nones_in_items(colour_id_to_idx, items_info, Colour, 'colour', 'marketplace_id') self._fill_nones_in_items(seller_id_to_idx, items_info, Seller, 'seller', 'name') self.lock.release() current_time = now() new_items, old_items, colours_old_items, colours_new_items = [], [], [], [] fields_to_update = ['name', 'size_name', 'size_orig_name'] last_marketplace_id = 0 self.lock.acquire() for i, item in enumerate(items_info): create_params = { 'name': item['name'], 'marketplace_id': item['marketplace_id'], 'marketplace_source': self.marketplace_source, 'root_id': item['root_id'], 'brand': item['brand'], 'size_name': item['size_name'], 'size_orig_name': item['size_orig_name'], 'seller': item['seller'], 'next_parse_time': current_time, 'is_adult': item['is_adult'] } get_params = { 'marketplace_id': item['marketplace_id'], 'marketplace_source': self.marketplace_source } if i > 0: last_marketplace_id = items_info[i - 1]['marketplace_id'] try: existing_item = Item.objects.get(**get_params) except Item.DoesNotExist: new_item = Item(**create_params) self._add_item_and_fill_colours(new_items, colours_new_items, last_marketplace_id, new_item, item['colour'].pk) continue except Item.MultipleObjectsReturned: logger.error( f'Something is wrong. You should get one object for params: {get_params}' ) existing_items = Item.objects.filter(**get_params) existing_item = existing_items.earliest('created_at') for duplicate_item in existing_items.exclude( pk=existing_item.pk): # Delete existing relations in all duplicates self._change_revisions_positions_item( duplicate_item, existing_item) duplicate_item.delete() self._update_existing_item(existing_item, item, fields_to_update) self._add_item_and_fill_colours(old_items, colours_old_items, last_marketplace_id, existing_item, item['colour'].pk) if old_items: with transaction.atomic(): Item.objects.bulk_update(old_items, fields_to_update) for old_item, colour_pks in zip(old_items, colours_old_items): old_item.colours.add(*colour_pks) if new_items: with transaction.atomic(): try: new_items = Item.objects.bulk_create(new_items) except DataError as e: logger.exception(e) save_object_for_logging(new_items, 'corrupted_new_items.p') raise e for new_item, colour_pks in zip(new_items, colours_new_items): new_item.colours.add(*colour_pks) self.lock.release() all_items = old_items + new_items if len(all_items) > 0: all_items.sort(key=lambda x: x.marketplace_id) return all_items
def item_registration(request): if request.method=='POST': try: uid = request.POST.get('uniqueid') title = request.POST.get('title') category = request.POST.get('category') description = request.POST.get('description') tags = request.POST.get('tags') location = request.POST.get('location') photos = json.loads(request.POST['media']) new_item = Item() new_item.title = title new_item.unique_id = uid new_item.tags = tags new_item.description = description new_item.location = request.POST.get('location') new_item.category = category new_item.date_field = datetime.datetime.now().strftime("%Y-%m-%d") new_item.time_field = datetime.datetime.now().strftime("%H:%M:%S") new_item.found_by_user = request.user new_item.save() for media in photos: photo = Media() photo.of_item = new_item photo.media_type = "PHOTO" save_base64image_to_media(photo, media) photo.save() call_command('update_index') return HttpResponse(json.dumps({'result': 'OK', 'pk':new_item.pk}), content_type="application/json") except Exception as e: traceback.print_exc() return HttpResponse(json.dumps({'result': 'ERROR'}), content_type="application/json") context = RequestContext(request, {'request': request, 'user': request.user }) return render_to_response('public/registerfounditem.html', context_instance=context)
def test_create_item_with_image(db, user): img = open('testsuite/test.png', 'rb') item = Item(user=user, image=File(img)) item.save() assert item.image
def test_create_item_with_user(db, user): item = Item( user=user, title="title#1", description="description number #1" ) item.save()
def close_spider(self, spider): # print('day la') # print(self.variations) # return num_page = len(self.productItems) for page in range(num_page): num_item = len(self.productItems[page]['name']) for i in range(num_item): try: new_Item = Item() new_Item.title = self.productItems[page]['name'][i] new_Item.image = self.productItems[page]['images'][i][ 'path'] new_Item.category = self.productItems[page]['category'][i] new_Item.price = self.productItems[page]['price'][i] new_Item.slug = self.productItems[page]['slug'][i] new_Item.description = self.productItems[page][ 'description'][0] new_Item.label = self.productItems[page]['label'][0] new_Item.save() except Exception as e: print(e) pass num_variation = len(self.variations) for vari in range(num_variation): curr_vari = self.variations[vari] item = Item.objects.filter(title=curr_vari['itemName'][0]).first() if (item != None): if (item.description == ""): item.description = curr_vari['description'][0] item.save() try: num_variationValue = len(curr_vari['value']) new_variation = Variation() new_variation.item = item new_variation.name = curr_vari['variation'][0] new_variation.save() except Exception as e: print(e) pass variation = Variation.objects.filter( item=item, name=curr_vari['variation'][0]).first() for j in range(num_variationValue): try: new_variationValue = ItemVariation() new_variationValue.variation = variation new_variationValue.value = curr_vari['value'][j] new_variationValue.attachment = curr_vari['images'][j][ 'path'] if (len(curr_vari['images']) - 1) >= j else '' new_variationValue.save() except Exception as e: print(e) pass
def _create_items(self): call_command('migrate', 'auth') sys.stdout.write("\n==========Auth App Migrated===========\n") call_command('migrate') sys.stdout.write("\n==========Other Apps Migrated===========\n") call_command('syncdb', interactive=True) Item.objects.all().delete() CustomUser.objects.all().delete() CustomUser.objects.create_superuser(username='******', password='******', email='*****@*****.**') user1 = CustomUser() user1.username = "******" user1.first_name = "Nikolaus" user1.last_name = "Mickaelson" user1.email = "*****@*****.**" user1.prefered_way_of_contact = "IBF" user1.phone_number = "12345673" user1.set_password('nick') user1.save() user2 = CustomUser() user2.username = "******" user2.first_name = "Mark" user2.last_name = "Johnson" user2.email = "*****@*****.**" user2.prefered_way_of_contact = "PHONE" user2.phone_number = "122456141" user2.set_password("mark") user2.save() pitem = PreRegisteredItem() pitem.unique_id = '' pitem.title = "Green Adidas Bag " pitem.tags = "Bag" pitem.description = 'Green Bag lost near Southampton' pitem.location = "Southampton" pitem.category = "Bag" pitem.owner = CustomUser.objects.filter(username='******')[0] pitem.lost = True pitem.save() photo = Media() photo.of_item = pitem photo.media_type = "PHOTO" save_url_to_image(photo, 'http://www.fashionvortex.com/image/cache/data/Medici/MF-2475-Gr-a-600x600.jpg') photo.save() tphone = Item() tphone.unique_id = '123456789' tphone.title = "Black Samsung Galaxy S6 34GB" tphone.tags = "Black Samsung Galaxy S6 34GB" tphone.description = 'Black Samsung Galaxy S6 found in Stile' tphone.location = "Southampton" tphone.category = "Electronics" tphone.date_field = "2015-09-15" tphone.time_field = "14:33::22" tphone.found_by_user = user1 tphone.save() tbag = Item() tbag.description = 'Green bag found on the poll edge at "Summer Time"' tbag.title = "Bag Green" tbag.tags = "Bag Green" tbag.location = "london" tbag.category = "Bag" tbag.date_field = "2016-09-09" tbag.time_field = "10:33::22" tbag.found_by_user = user1 tbag.save() photo = Media() photo.of_item = tbag photo.media_type = "PHOTO" save_url_to_image(photo, 'http://www.suggestcamera.com/wp-content/uploads/2015/08/81K-jtyW82L._SL1500_.jpg') photo.save() tbag = Item() tbag.description = 'Green bag found on the poll edge at "Summer Time"' tbag.title = "Big Bag" tbag.tags = "Bag" tbag.location = "london" tbag.category = "Bag" tbag.date_field = "2016-09-09" tbag.time_field = "10:33::22" tbag.found_by_user = user1 tbag.save() photo = Media() photo.of_item = tbag photo.media_type = "PHOTO" save_url_to_image(photo, 'http://i.dailymail.co.uk/i/pix/2013/11/13/article-2505060-0B22502B000005DC-342_634x422.jpg') photo.save() tLeptop = Item() tLeptop.unique_id = '098765432' tLeptop.description = '15 inch Dell found in Winchester"' tLeptop.title = "Dell Leptop Inspiron" tLeptop.tags = "Leptop Dell Black" tLeptop.location = "london" tLeptop.category = "Electronics" tLeptop.date_field = "2015-09-18" tLeptop.time_field = "10:33::22" tLeptop.found_by_user = user1 tLeptop.save() tLaptop = Item() tLaptop.unique_id = '123456788' tLaptop.description = 'Apple MacBook 15" found at Hartley Library' tLaptop.title = "Apple MacBook" tLaptop.tags = "Apple MacBook" tLaptop.location = "Southampton" tLaptop.category = "Electronics" tLaptop.date_field = "2015-11-16" tLaptop.time_field = "22:35::22" tLaptop.found_by_user = user1 tLaptop.save() photo = Media() photo.of_item = tLaptop photo.media_type = "PHOTO" save_url_to_image(photo, 'http://static.trustedreviews.com/94/9736c1/5242/15168-crw398s.jpg') photo.save() tIDCard = Item() tIDCard.unique_id = '123459876' tIDCard.description = 'Passport found outside Sprinkles' tIDCard.title = "UK EU e-passport" tIDCard.tags = "Passport UK EU e-passport" tIDCard.location = "Southampton" tIDCard.category = "ID/Cards" tIDCard.date_field = "2015-07-23" tIDCard.time_field = "12:07::22" tIDCard.found_by_user = user1 tIDCard.save() photo = Media() photo.of_item = tIDCard photo.media_type = "PHOTO" save_url_to_image(photo, 'http://i.telegraph.co.uk/multimedia/archive/01595/mp-passport-pa_1595880b.jpg') photo.save() tBook = Item() tBook.unique_id = '121212123' tBook.description = 'Dan Brown The Lost Symbol paperback edition' tBook.title = "The Lost Symbol Paperback" tBook.tags = "Dan Brown The Lost Symbol Paperback " tBook.location = "Bournemouth" tBook.category = "Books" tBook.date_field = "2015-09-30" tBook.time_field = "17:53:28" tBook.found_by_user = user2 tBook.save() photo = Media() photo.of_item = tBook photo.media_type = "PHOTO" save_url_to_image(photo, 'http://thumbs1.ebaystatic.com/d/l225/m/mIuB9Oannj3xR0YhYCIiEZg.jpg') photo.save() tScarf = Item() tScarf.unique_id = '666777888' tScarf.description = 'Grey Scarf with Dark Grey Stripes' tScarf.title = "Scarf" tScarf.tags = "Scarf Grey Dark Grey Stripes " tScarf.location = "Surrey" tScarf.category = "Clothes" tScarf.date_field = "2015-10-28" tScarf.time_field = "13:53:28" tScarf.found_by_user = user2 tScarf.save() photo = Media() photo.of_item = tScarf photo.media_type = "PHOTO" save_url_to_image(photo, 'http://assets3.howtospendit.ft-static.com/images/52/46/d7/5246d742-1619-46b4-83c8-9e9d726203da_three_eighty.png') photo.save() tNecklace = Item() tNecklace.unique_id = '898998989' tNecklace.title = 'Black Leather necklace' tNecklace.tags = 'Black Leather necklace' tNecklace.description = "leather necklace black men unisex" tNecklace.location = "Glasgow" tNecklace.category = "Accessories" tNecklace.date_field = "2015-11-28" tNecklace.time_field = "13:27:28" tNecklace.found_by_user = user2 tNecklace.save() photo = Media() photo.of_item = tNecklace photo.media_type = "PHOTO" save_url_to_image(photo, 'http://cdn.notonthehighstreet.com/system/product_images/images/001/615/301/original_mens-leather-necklace.jpg') photo.save() tHobbit = Item() tHobbit.unique_id = '454647489' tHobbit.title = 'J R R Tolkien -' tHobbit.tags = 'J R R Tolkien - The Hobbit Hard Cover' tHobbit.description = "tolkien hobbit the hobbit hardcover" tHobbit.location = "Eastleigh" tHobbit.category = "Books" tHobbit.date_field = "2015-10-30" tHobbit.time_field = "10:41:28" tHobbit.found_by_user = user2 tHobbit.save() photo = Media() photo.of_item = tHobbit photo.media_type = "PHOTO" save_url_to_image(photo, 'https://i.ytimg.com/vi/X75pnPtqhvE/maxresdefault.jpg') photo.save() tPlayer = Item() tPlayer.unique_id = '145897123' tPlayer.title = 'Sony Walkman MP4 Player Black' tPlayer.tags = 'Sony Walkman MP4 Player Black' tPlayer.description = "sony walkman mp4 player mp3 black " tPlayer.location = "London" tPlayer.category = "Electronics" tPlayer.date_field = "2015-10-30" tPlayer.time_field = "10:41:28" tPlayer.found_by_user = user2 tPlayer.save() photo = Media() photo.of_item = tPlayer photo.media_type = "PHOTO" save_url_to_image(photo, 'https://i.ytimg.com/vi/PI_nQ3MSSHI/maxresdefault.jpg') photo.save() tDog = Item() tDog.unique_id = '321654987' tDog.title = 'Chihuahua' tDog.tags = 'Lost Chihuahua found on Portswood Road' tDog.description = "chihuahua dog portswood southampton lost " tDog.location = "Southampton" tDog.category = "Animal" tDog.date_field = "2015-11-17" tDog.time_field = "22:41:28" tDog.found_by_user = user2 tDog.save() photo = Media() photo.of_item = tDog photo.media_type = "PHOTO" save_url_to_image(photo, 'https://canophilia.files.wordpress.com/2014/04/chihuahua_4.jpg') photo.save() tHobbit = Item() tHobbit.unique_id = '125678991' tHobbit.title = 'Adele - Rolling in the Deep' tHobbit.tags = 'Adele - Rolling in the Deep CD Album' tHobbit.description = "adele rolling in the deep cd album" tHobbit.location = "Manchester" tHobbit.category = "Other" tHobbit.date_field = "2015-09-27" tHobbit.time_field = "13:44:28" tHobbit.found_by_user = user2 tHobbit.save() photo = Media() photo.of_item = tHobbit photo.media_type = "PHOTO" save_url_to_image(photo, 'http://thumbs2.ebaystatic.com/d/l225/m/mQTzqU9kSL8uIcBHIkfwOqA.jpg') photo.save() tMug = Item() tMug.unique_id = '123654897' tMug.description = 'Found this mug at the Solent Library, 2nd Level' tMug.title = "Mug" tMug.tags = "mug white solent southampton" tMug.location = "Southampton" tMug.category = "Other" tMug.date_field = "2015-10-06" tMug.time_field = "09:13:28" tMug.found_by_user = user2 tMug.save() photo = Media() photo.of_item = tMug photo.media_type = "PHOTO" save_url_to_image(photo, 'https://s-media-cache-ak0.pinimg.com/736x/7c/01/a9/7c01a9440c8e8afde4b11ab4acbfcd3d.jpg') photo.save() sys.stdout.write("\n==========Database Re-populated===========\n") call_command('rebuild_index')
def item_registration(request): if request.method == 'POST': try: uid = request.POST.get('uniqueid') title = request.POST.get('title') category = request.POST.get('category') description = request.POST.get('description') tags = request.POST.get('tags') location = request.POST.get('location') photos = json.loads(request.POST['media']) new_item = Item() new_item.title = title new_item.unique_id = uid new_item.tags = tags new_item.description = description new_item.location = request.POST.get('location') new_item.category = category new_item.date_field = datetime.datetime.now().strftime("%Y-%m-%d") new_item.time_field = datetime.datetime.now().strftime("%H:%M:%S") new_item.found_by_user = request.user new_item.save() for media in photos: photo = Media() photo.of_item = new_item photo.media_type = "PHOTO" save_base64image_to_media(photo, media) photo.save() call_command('update_index') return HttpResponse(json.dumps({ 'result': 'OK', 'pk': new_item.pk }), content_type="application/json") except Exception as e: traceback.print_exc() return HttpResponse(json.dumps({'result': 'ERROR'}), content_type="application/json") context = RequestContext(request, { 'request': request, 'user': request.user }) return render_to_response('public/registerfounditem.html', context_instance=context)