def index(request): if request.method == 'POST': text = request.form.get('q', '') if not text: return redirect('/') text = text.strip() gifts = Gift.all().filter('in_trash =', False)\ .filter('leftovers >', 0).search( text, properties=['name'] ) if not gifts.count(): gifts = Gift.all().filter('in_trash =', False)\ .filter('leftovers >', 0).search( text, properties=['barcode'] ) if not gifts.count(): gifts = Gift.all().filter('in_trash =', False)\ .filter('leftovers >', 0).search( text, properties=['catalogue_id'] ) if not gifts: return redirect('/') return render_to_response( 'srch/index.html', {'gifts': gifts, 'search_text': text} ) else: return redirect('/')
def quick_import(file_name='leftovers.csv'): csv_file = open_csv(file_name) for i, line in enumerate(csv_file): if line[0] and line[1]: name = unicode.strip(line[0].decode('utf8')) gift = Gift.all().filter('name =', name) if gift.count(): gift = gift[0] leftovers = line[1].strip().replace(',', '').replace('.', '') if not leftovers: leftovers = 0 else: leftovers = int(leftovers) if gift.leftovers == leftovers: if gift.leftovers !=0 and gift.receipt_date: gift.receipt_date = None gift.put() continue gift.leftovers = leftovers if gift.receipt_date: gift.receipt_date = None gift.put() print "%s %s : %s" % (i, gift.uid, gift.leftovers) print "========================================================" for g in Gift.all(): print g.leftovers
def show(request): gifts = [] order_items = request.session.get('order', {}) if not order_items: return render_to_response('cart/show.html', {'gifts':gifts}) total_price = 0 for k, val in order_items.iteritems(): gift = Gift.get_by_id(k) if gift: gift.count = val gifts.append(gift) total_price += gift.price * val form = OrderForm() if request.method == 'POST' and form.validate(request.form): order = form.save(commit=False) order.email = request.form.get('email', None) order.address = request.form.get('address', None) order.put() for k, v in request.session.get('order', {}).items(): gift = Gift.get_by_id(k) if gift: oi = OrderItem(gift_id=gift, count=v) oi.in_order = order oi.put() clear_cart(request) return render_to_response('cart/confirm_complete.html') return render_to_response('cart/show.html', {'gifts':gifts, 'total_price':total_price, 'form':form.as_widget()})
def get_gift(request, idx): gift = Gift.get_by_id(idx) if not gift: return redirect('/') additional_gifts = Gift.all().filter('subcategory =', gift.subcategory).filter( 'name !=', gift.name).fetch(4) return render_to_response('gift/get.html', {'gift': gift, 'additional_gifts': additional_gifts, 'price_modif': GlobalPriceModif.get_price_modif()})
def sync_gift_task(request, task_id): gift_id = memcache.get(task_id) memcache.delete(task_id) if gift_id: try: result = urlfetch.fetch( 'http://www.3dhero.ru/api/v2/product.json?id=%s' % gift_id ) if result.status_code == 200: result = simplejson.loads(result.content) if result['success']: model = result['result'] if model['id_1c']: category = Category.all().filter('name =', u'Развивающие Игры').get() if category: group = Group.all().filter('category =', category).filter('name =', u'Фигурки Героев').get() else: group = None gift = Gift( name = model.get('name', ''), id_1c = model['id_1c'], catalogue_id = model.get('catalogue_id', ''), barcode = model.get('barcode', ''), brand = model.get('brand', ''), country = model.get('country', ''), material = model.get('material', ''), gift_size = model.get('size', ''), weight = model.get('weight',''), box_size = model.get('box_size', ''), master_box = model.get('box_amount', ''), real_price = model.get('price_trade', 0.0), price = model.get('price_retail', 0.0), leftovers = model.get('leftovers', 0), remote_leftovers = model.get('leftovers_on_way', 0), to_sync = True ) if category and group: gift.category = category gift.group = group gift.put() images = model.get('images', []) if images: for img in images: def txn(): task_id = str(uuid.uuid4()) data = {'gift_id': gift.key().id(), 'img_url': img} memcache.add(task_id, data, TASK_LIVE_TIMEOUT) taskqueue.add( url=url_for('sync/add_image_task', task_id=task_id), transactional=True ) db.run_in_transaction(txn) except: pass return render_to_response('empty.html')
def get_menu(request): filters = request.session.get('filters', []) if request.user.is_admin: return render_to_response( 'categories_view.html', { 'subcategories': Gift.get_subcategories_list(filters, is_admin=True), 'brand_list': Gift.get_brand_list(filters, is_admin=True) }) return render_to_response( 'categories_view.html', { 'subcategories': Gift.get_subcategories_list(filters), 'brand_list': Gift.get_brand_list(filters) })
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 export(): writer = csv.writer(open('quick_export.csv', 'wb')) writer.writerow(['Идентификатор', 'Название', 'Краткое описание', 'Полное описание']) for gift in Gift.all(): name = gift.name if name: name = name.encode('utf-8') else: name = u'' short_description = gift.short_description if short_description: short_description = clear_data(short_description) else: short_description = u'' description = gift.description if description: description = clear_data(description) else: description = u'' writer.writerow([gift.uid, name, short_description.encode('utf-8'), description.encode('utf-8')])
def add_new_thumb(request): if request.method == 'POST': gift_key = request.values.get('gift_key') gift = Gift.get(gift_key) if gift is None: return redirect('/gift/admin/edit/%s/' % gift_key) new_th_form = AddNewThumb() if request.form and new_th_form.validate(request.form, request.files): thumb = new_th_form['img'] content_type = 'image/jpeg' if gift.name: title = gift.name.replace('"', '"') else: title = '' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) if not gift.thumbs.count(): thumb_img.main_gift = gift thumb_img.gift = gift thumb_img.put() return redirect('/gift/admin/edit/%s/' % gift_key) return redirect('/gift/admin/all/')
def recalculate_order(request, remove_one=None): gifts = [] total_price = 0 items_count = 0 for key in request.session['order'].keys(): if key == remove_one: del(request.session['order'][key]) continue gift_obj = Gift.get(key) if gift_obj: gift_obj.number = request.session['order'][key] if not request.user or request.user.is_anonymous(): total_price += gift_obj.price * gift_obj.number else: total_price += gift_obj.real_price * gift_obj.number gifts.append(gift_obj) items_count += request.session['order'][key] else: del(request.session['order'][key]) if gifts: request.session['order_items_number'] = len(request.session['order'].keys()) request.session['order_total_price'] = total_price request.session['order_items_count'] = items_count else: reset_order(request) return gifts
def get_counts(request): counts = memcache.get('api_get_count') if not counts: coming_soon = Gift.accept_filters(filters=['coming_soon']).count() on_sale = Gift.accept_filters(filters=['on_sale']).count() no_sale = Gift.accept_filters(filters=['no_sale']).count() total = coming_soon + on_sale + no_sale if total: counts = { 'coming_soon': coming_soon, 'on_sale': on_sale, 'no_sale': no_sale, 'total': total} if counts: memcache.set('api_get_count', counts, 7200) return render_json_response(counts)
def search(request): if request.method == 'POST': text = request.form.get('q', '') if not text: return redirect('/') gifts_list = Gift.all().filter('leftovers >', 0).search(text, properties=['name']) gifts_list = filter_leftovers(gifts_list) if not gifts_list: return redirect('/') 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/main_page.html', {'gifts': gifts, 'categories': categories[0], 'subcategories': categories[1], 'subcategories_keys': categories[2], 'price_modif': GlobalPriceModif.get_price_modif()}) else: return redirect('/')
def restore_from_trash(request, gift_key): gift = Gift.get(gift_key) if gift: gift.in_trash = False gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid)
def move_to_trash(request, gift_key): gift = Gift.get(gift_key) if gift: gift.in_trash = True gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid)
def upload_new_thumb(request): if request.method == 'POST': gift_key = request.values.get('gift_key') gift = Gift.get(gift_key) if gift is None: return redirect('/') new_th_form = AddNewThumb() if request.form and new_th_form.validate(request.form, request.files): thumb = new_th_form['img'] content_type = 'image/jpeg' if gift.name: title = gift.name.replace('"', '"') else: title = '' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) thumb_img.put() gift.thumbs.append(str(thumb_img.key())) gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid) return redirect('/')
def update_data( obj_key=None, catalogue_uid=None, name=None, name_origin=None, brand=None, rating=None, category=None, subcategory=None, group=None, price=None, barcode=None, box_size=None, master_box=None, status=None, receipt_date=None, real_price=None): obj = Gift.get(obj_key) if not obj: print "Unknown error - object not found!" exit(1) i = 0 i += update_value(obj, 'catalogue_id', catalogue_uid) i += update_value(obj, 'name_origin', name_origin) i += update_value(obj, 'name', name) i += update_value(obj, 'brand', brand) i += update_value(obj, 'category', category) i += update_value(obj, 'subcategory', subcategory) i += update_value(obj, 'group', group) i += update_value(obj, 'barcode', barcode) i += update_value(obj, 'master_box', master_box) i += update_value(obj, 'box_size', box_size) i += update_value(obj, 'price', price) i += update_value(obj, 'real_price', real_price) i += update_value(obj, 'rating', rating) i += update_value(obj, 'status', status) i += update_receipt_date(obj, receipt_date) if i: obj.put()
def add_image_task(request, task_id): data = memcache.get(task_id) memcache.delete(task_id) if not data or not data.get('gift_id') or not data.get('img_url'): return render_to_response('empty.html') img = urlfetch.fetch(data['img_url']) if img.status_code == 200: thumb = img.content gift = Gift.get_by_id(data['gift_id']) if gift: title = gift.name.replace('"', '"') content_type = 'image/jpeg' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) thumb_img.put() gift.thumbs.append(str(thumb_img.key())) if not gift.main_thumb: gift.main_thumb = str(thumb_img.key()) gift.put() return render_to_response('empty.html')
def sync_add_image(request, uid): logging.info("Add image for gift with key: %s" % uid) mem_key = 'sync/add_image/%s' % uid img_url = memcache.get(mem_key) if img_url: memcache.delete(mem_key) img = urlfetch.fetch(img_url) if img.status_code == 200: thumb = img.content gift = Gift.get_by_id(uid) if gift: title = gift.name.replace('"', '"') thumb_img = ThumbImage() content_type = 'image/jpeg' thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700,), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) if not gift.thumbs.count(): thumb_img.main_gift = gift thumb_img.gift = gift thumb_img.put() return render_json_response({'api_msg': 'Ok', 'api_success': True})
def preview_newsletter(request, key): newsletter = Newsletter.get(key) gifts = [] if newsletter.gifts: gifts_split = newsletter.gifts.split(",") for gift in gifts_split: try: gift = gift.strip() obj = Gift.all().filter("uid =", gift).get() if obj: gifts.append(obj) except Exception: pass managers = [] users = UserProfile.all().filter("is_send_newsletter =", True).filter("email !=", None) if newsletter.type != 999: users = users.filter("newsletter_type =", newsletter.type) files = [] for file_key in newsletter.n_price_list: file_ = File.get(file_key) if file_: files.append(file_) return render_to_response( "postman/admin/newsletters/preview.html", {"newsletter": newsletter, "gifts": gifts, "key": key, "files": files, "managers": managers, "users": users}, )
def get_coming_soon_list(request): list_gifts = memcache.get('api_get_coming_soon_list') if not list_gifts: gifts = Gift.accept_filters(filters=['coming_soon']) if gifts: list_gifts = [gift.uid for gift in gifts] memcache.add('api_get_coming_soon_list', list_gifts, 7200) return render_json_response(list_gifts)
def get_list_all(request): list_gifts = memcache.get('api_get_list_all') if not list_gifts: gifts = Gift.all() if gifts: list_gifts = [gift.uid for gift in gifts] memcache.add('api_get_list_all', list_gifts, 7200) return render_json_response(list_gifts)
def get_gift_mem(uid_): if not uid_: return None try: gift = Gift.all().filter('uid =', uid_).get() return prefetch_refprops([gift], Gift.category, Gift.group)[0] except Exception: return None
def html(filters_, is_admin): gifts = Gift.last_incoming(filters_, is_admin) if gifts: gifts = gifts.run(limit=DEFAULT_REQUEST_COUNT) return render_to_response('gift/last_incoming.html',{ 'gifts': gifts, 'active_category': 'last_incoming' })
def top_html(filters, is_admin): tops = Gift.top_desc(filters, is_admin) if tops: tops = tops.filter('rating >=', 0).run(limit=48) return render_to_response('gift/top_sales.html', { 'gifts': tops, 'active_category': 'top' })
def sync_gift(request): for gift in Gift.all(): if gift.uid_5studio: def txn(): taskqueue.add(url=url_for('sync/sync_gift_task', key=gift.key()), transactional=True) db.run_in_transaction(txn) return render_to_response('sync/sync_gift.html')
def index(request): brands = Gift.get_brand_list() brands_gifts = {} for brand in brands: if brands_gifts.get(brand): continue gifts = Gift.get_brand(brand) if not gifts: continue gift = gifts.get() if gift: brands_gifts[brand] = gift else: brands_gifts[brand] = None return render_to_response('brands/index.html', { 'brands': brands, 'brands_gifts': brands_gifts })
def internal_delete_gifts_json(request, task_id): name = memcache.get(task_id) memcache.delete(task_id) if name: gifts_obj = Gift.accept_filters().filter('name =', name) if gifts_obj.count(): gift = gifts_obj[0] gift.delete() return render_to_response('empty.html')
def internal_delete_gifts_json_1c(request, task_id): id_1c = memcache.get(task_id) memcache.delete(task_id) if id_1c: gifts_obj = Gift.accept_filters().filter('id_1c =', id_1c) if gifts_obj.count(): gift = gifts_obj[0] gift.delete() return render_to_response('empty.html')
def select_filters(request): if request.method == 'POST': selected_brands = request.form.getlist('brands') brands_gifts = [] if selected_brands: brands_gifts = [gift.key() for gift in Gift.all().filter('brand in', selected_brands)] subcategories_gifts = [] selected_subcategories = request.form.getlist('subcategories') if selected_subcategories: subcategories_gifts = [gift.key() for gift in Gift.all().filter('subcategory in', selected_subcategories)] selected_groups = request.form.getlist('groups') groups_gifts = [] if selected_groups: groups_gifts = [gift.key() for gift in Gift.all().filter('group in', selected_groups)] selected_dates = request.form.getlist('receipt_dates') dates_gift = [] if selected_dates and len(selected_dates) == 2: date_from = string.strip(min(selected_dates)) date_to = string.strip(max(selected_dates)) if date_from: try: date_from = datetime.date(datetime.strptime(date_from, '%Y-%m-%d')) except ValueError: date_from = '' if date_to: try: date_to = datetime.date(datetime.strptime(date_to, '%Y-%m-%d')) except ValueError: date_to = '' if date_from and date_to: dates_gift = [gift.key() for gift in Gift.all().filter('receipt_date >=', date_from).filter('receipt_date <=', date_to)] else: if date_from: dates_gift = [gift.key() for gift in Gift.all().filter('receipt_date =', date_from)] else: if date_to: dates_gift = [gift.key() for gift in Gift.all().filter('receipt_date =', date_to)] gifts = set(brands_gifts + subcategories_gifts + groups_gifts + dates_gift) request.session['mass_edit_items'] = gifts return redirect('/admin/mass_edit/edit/') request.session['mass_edit_items'] = 0 brands = Gift.get_brand_list(is_admin=True) subcategories = Gift.get_subcategories_list() return render_to_response('admin/mass_edit/select_filters.html', {'brands':brands, 'subcategories':subcategories})
def trash_ajax(request): if request.method == 'POST': last_gift = request.values.get('last_gift') if last_gift: last_gift = int(last_gift) gifts = gifts = Gift.all().filter('in_trash =', True) if gifts: gifts = gifts.fetch(50, last_gift) return render_to_response('gift/list.html', {'gifts': gifts}) return render_to_response("empty.html")