Esempio n. 1
0
def add_useritem_into_group(url, group_id):
    source_obj, scraper, url = validate(url)
    try:
        item = OriginalItem.objects.get(url=url)
        result = scraper.scrape(url)
    except Exception as e:
        result = scraper.scrape(url)
        result['data']['info']['url'] = result['url']
        item = OriginalItem(source=source_obj, **result['data']['info'])
        item.save()

    tpl_data = dict(
        base_price=0.0,
        discount_price=00
    )
    tpl_data.update(result['data']['price'])

    item_price = ItemPrice(
        item=item,
        current_price=min(filter(lambda x: x > 0, tpl_data.values()) or (0.0,)),
        **tpl_data
    )
    item_price.save()

    user_item = UserItem.objects.create(group_id=group_id, item=item, custom_name=item.title)
    partner = item.source.partner

    item_price.current_price = defaultfilters.floatformat(item_price.current_price, 2)
    item = json.loads(serializers.serialize("json", [item]))[0]
    price = json.loads(serializers.serialize("json", [item_price]))[0]
    user_item = json.loads(serializers.serialize('json', [user_item]))[0]
    partner = json.loads(serializers.serialize('json', [partner]))[0]
    price['formatted_date'] = defaultfilters.date(item_price.created_date.replace(tzinfo=utc), u"d E Y г. H:i")

    return dict(item=item, price=price, user_item=user_item, partner=partner)
Esempio n. 2
0
def add_url(url):
    source_obj, scraper, url = validate(url)
    try:
        item = OriginalItem.objects.get(url=url)
        result = scraper.scrape(url)
    except Exception as e:
        result = scraper.scrape(url)
        result['data']['info']['url'] = result['url']
        item = OriginalItem(source=source_obj, **result['data']['info'])
        item.save()

    tpl_data = dict(
        base_price=0.0,
        discount_price=00
    )
    tpl_data.update(result['data']['price'])

    item_price = ItemPrice(
        item=item,
        current_price=min(filter(lambda x: x > 0, tpl_data.values()) or (0.0,)),
        **tpl_data
    )
    item_price.save()

    item = json.loads(serializers.serialize("json", [item]))[0]
    price = json.loads(serializers.serialize("json", [item_price]))[0]
    return dict(item=item, price=price)