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})
def brands(request): form = BrandForm() if request.method == "POST": if form.validate(request.form): form.save() form = BrandForm() return render_to_response('gift/admin/brands.html', {'form':form.as_widget(), 'brands':Brand.all()})
def brand_edit(request, key): brand = Brand.get(key) if brand is None: return redirect('/gift/admin/brands/') form = BrandForm(instance=brand) if request.method == 'POST' and form.validate(request.form): form.save() return redirect('/gift/admin/brands/') return render_to_response('gift/admin/brand_edit.html', {'form':form.as_widget()})
def all_brands(request): objs_tmp = Brand.all() brands = {} brands_ids = {} objs = [] for o in objs_tmp: if o.gifts.filter('leftovers >', 0).count(): objs.append(o) for brand in objs: brands[brand.brand] = [Gift.all().filter('brand =', brand).fetch(3)] brands_ids[brand.brand] = brand.key().id() return render_to_response('index_page/brands.html', {'brands': brands, 'brands_ids': brands_ids})
def get_brand(request, id): brand = Brand.get_by_id(id) if not brand: return redirect('/') gifts_list = brand.gifts.order('-rating') gifts_list = filter_leftovers(gifts_list) paginator = Paginator(gifts_list, 24) try: page = int(request.args.get('page', 1)) except ValueError: page = 1 try: gifts = paginator.page(page) except (EmptyPage, InvalidPage): gifts = paginator.page(paginator.num_pages()) categories = get_cat_subcat() return render_to_response('index_page/brand.html', {'brand': brand, 'gifts': gifts, 'categories': categories[0], 'subcategories': categories[1], 'subcategories_keys': categories[2], 'price_modif': GlobalPriceModif.get_price_modif()})
def import_data(in_file='quick_export.csv'): images_path = os.path.join(DEFAULT_PATH, 'images') reader = csv.reader(open(os.path.join(DEFAULT_PATH, in_file))) for i, line in enumerate(reader): if not i: continue uid = line[0] gift_imgs_path = os.path.join(images_path, uid) try: imgs_files = os.listdir(gift_imgs_path) except OSError: imgs_files = [] name = line[2].decode('utf-8') category = line[3].decode('utf-8') subcategory = line[4].decode('utf-8') brand = line[5].decode('utf-8') if line[6]: price = float(line[6]) else: price = 0.0 size = line[9].decode('utf-8') if not size: size = u'' desc = line[10].decode('utf-8') if line[11]: rating = float(line[11]) else: rating = 3.0 if imgs_files: imgs_files =\ [open(os.path.join(gift_imgs_path, img), 'rb').read() \ for img in imgs_files] if brand: brand_obj = Brand.all().filter('brand =', brand) if not brand_obj.count(): brand_obj = Brand(brand=brand) brand_obj.put() else: brand_obj = brand_obj[0] else: brand_obj = None if category: category_obj = Category.all().filter('category =', category) if category_obj.count(): category_obj = category_obj[0] else: category_obj = Category(category=category) category_obj.put() else: category_obj = None if category and subcategory: subcategory_obj = Subcategory.all().filter('subcategory =', subcategory).filter('on_category =', category_obj) if subcategory_obj.count(): subcategory_obj = subcategory_obj[0] else: subcategory_obj = Subcategory(subcategory=subcategory, on_category=category_obj) subcategory_obj.put() else: subcategory_obj = None gift = Gift(name=name, uid_5studio=uid, brand=brand_obj, category=category_obj, subcategory=subcategory_obj, description=desc, rating=rating, price=price, size=size) gift.put() if gift.name: title = gift.name.replace('"', '"') else: title = '' content_type = 'image/jpeg' for thumb in imgs_files: thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type, is_using_pil=True) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type, is_using_pil=True) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type, is_using_pil=True) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type, is_using_pil=True) if not gift.thumbs.count(): thumb_img.main_gift = gift thumb_img.gift = gift thumb_img.put()
def delete_brand(request, key): brand = Brand.get(key) if brand: brand.delete() return redirect('/gift/admin/brands/')