Exemplo n.º 1
0
Arquivo: views.py Projeto: gmist/f-toy
def sync_new_task(request, uid):
    logging.info("Create new gift from 5studio.ru with uid: %s" % uid)
    url = 'http://www.5studio.ru/api/v1/get/gift.json?&uid=%s' % uid
    result = urlfetch.fetch(url)
    if result.status_code == 200:
        j = simplejson.loads(result.content)
        if len(j):
            name = j.get('name', '').replace('\\"', '"')
            name_origin = j.get('name_origin', '').replace('\\"', '"')
            barcode = j.get('barcode', '')
            description = j.get('description', '').replace('\\"', '"')
            price = j.get('price', 0.0)
            leftovers = j.get('leftovers', 0)

            gift = Gift.all().filter('uid_5studio =', uid).fetch(1)
            if gift:
                gift = gift[0]
                is_new = False
            else:
                is_new = True
                gift = Gift(
                    uid_5studio=uid,
                    name=name,
                    name_origin=name_origin,
                    barcode=barcode,
                    description=description,
                    price=price,
                    leftovers=leftovers)

            brand = j.get('brand', '').replace('\\"', '"')
            if brand:
                brand_obs = Brand.all().filter('brand =', brand)
                if not brand_obs.count():
                    brand = Brand(brand=brand)
                    brand.put()
                else:
                    brand = brand_obs.get()
                gift.brand = brand

            gift.category = get_category(j)
            gift.subcategory = get_subcategory(j, gift.category)
            gift.put()

            if is_new or not gift.thumbs:
                thumbs_url = j.get('thumbs_url', [])
                for url in thumbs_url:
                    gift_uid = gift.key().id()
                    memcache.add('sync/add_image/%s' % gift_uid,
                        'http://%s' % url, 7200)

                    def txn():
                        taskqueue.add(url=url_for(
                            'sync/sync_add_image',
                            uid=gift.key().id()),
                            transactional=True)
                    db.run_in_transaction(txn)

    return render_json_response({'api_msg': 'Ok', 'api_success': True})
Exemplo n.º 2
0
def update_process(file):
    from apps.gift.models import Gift

    csv_file = open_csv(file)
    i = 0
    for line in csv_file:
        if not i:
        # skip first line (header)
            i = 1
            continue

        catalogue_uid = unicode.strip(line[3].decode('utf8'))
        if not catalogue_uid:
            continue

        status = unicode.strip(line[0].decode('utf-8'))
        if status:
            status = int(status)
        else:
            status = None
        if status is None or status > 70:
            gift = get_gift_by_catalogue_uid(catalogue_uid)
            if gift:
                gift.delete()
            continue

        barcode = unicode.strip(line[5].decode('utf8'))
        name_origin = unicode.strip(line[6].decode('utf8'))
        name = unicode.strip(line[7].decode('utf8'))
        subcategory = unicode.strip(line[8].decode('utf8'))
        group = unicode.strip(line[9].decode('utf8'))
        category = unicode.strip(line[10].decode('utf8'))
        receipt_date = unicode.strip(line[14].decode('utf8'))
        try:
            if receipt_date:
                receipt_date = datetime.date(
                    datetime.strptime(receipt_date, '%m/%d/%Y'))
        except:
            receipt_date = None
        brand = unicode.strip(line[15].decode('utf8'))
        master_box = unicode.strip(line[16].decode('utf8'))
        rating = unicode.strip(line[17].decode('utf8'))
        if rating:
            rating = int(rating)
        else:
            rating = None
        box_size = unicode.strip(line[18].decode('utf8'))

        price = str.strip(line[21])
        if price:
            price = price.replace(' ', '').replace(',', '').replace('.', ' ')
            price = float(price)
        else:
            price = 0.0

        real_price = str.strip(line[25])
        if real_price:
            real_price = real_price.replace(' ', '').\
            replace(',', '').replace('.', ' ')
            real_price = float(real_price)
        else:
            real_price = 0.0

        gift = get_gift_by_catalogue_uid(catalogue_uid)
        if not gift:
            print 'Gift not found by catalogue uid ( %s ). \
            Create it!' % catalogue_uid
            gift = Gift()
            gift.put()

        update_data(
            obj_key=gift.key(),
            status=status,
            catalogue_uid=catalogue_uid,
            name=name,
            name_origin=name_origin,
            brand=brand,
            category=category,
            subcategory=subcategory,
            group=group,
            price=price,
            barcode=barcode,
            box_size=box_size,
            master_box=master_box,
            rating=rating,
            receipt_date=receipt_date,
            real_price=real_price)
        print '%s. %s, %s, %s'\
        % (i, catalogue_uid.encode('utf8'), name.encode('utf8'), price)

        i += 1