def groups(request): form = GroupForm() if request.method == 'POST': if form.validate(request.form): form.save() return redirect('category/admin/groups') return render_to_response('category/admin/groups.html', {'form':form.as_widget(), 'groups':Group.all().order('category').order('name')})
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 group_edit(request, id): group = Group.get_by_id(id) if not group: return redirect('category/admin/groups') form = GroupForm(instance=group) if request.method == 'POST': if form.validate(request.form): tmp = form.save(commit=False) if group.category != tmp.category: for gift in group.gifts: gift.category = tmp.category gift.push() group = tmp group.put() return redirect('category/admin/groups') return render_to_response('category/admin/groups.html', {'form':form.as_widget(), 'groups':Group.all().order('category').order('name'), 'edit_flag':True})
def import_data(in_file='quick_export.csv'): # reader = csv.reader(open(os.path.join(DEFAULT_PATH, in_file))) reader = csv.reader(open(in_file)) for i, line in enumerate(reader): if not i: continue uid = line[1] category = line[5].decode('utf-8') subcategory = line[6].decode('utf-8') if category: category_obj = Category.all().filter('name =', category) if category_obj.count(): category_obj = category_obj[0] else: category_obj = Category(name=category) category_obj.put() else: category_obj = None if category and subcategory: group_obj = Group.all().filter('name =', subcategory).filter('category =', category_obj) if group_obj.count(): group_obj = group_obj[0] else: group_obj = Group(name=subcategory, category=category_obj) group_obj.put() else: group_obj = None gift = Gift.all().filter('uid =', uid) if gift.count(): gift = gift[0] gift.category = category_obj gift.group = group_obj gift.put() print 'Gift %s updated' % uid else: print 'Gift %s not found' % uid
def update_gifts(task_id, create=False, except_fields=EXCEPT_FIELDS_UPDATE): set_obj = memcache.get(task_id) flag = False if set_obj: data = simplejson.loads(set_obj) memcache.delete(task_id) id_1c = data.get('id_1c', '') logging.info("API update 1c: %s" % id_1c) name = data.get('name', '') logging.info("API update name: %s" % name) if not id_1c and not name: if not create: return render_to_response('empty.html') if id_1c: gifts_obj = Gift.all().filter('id_1c =', id_1c) if not gifts_obj.count(): gifts_obj = Gift.all().filter('name =', name) else: gifts_obj = Gift.all().filter('name =', name) if gifts_obj.count() and create: return render_to_response('empty.html') gift = None if gifts_obj.count() and not create: gift = gifts_obj[0] if not gifts_obj.count() and create: gift = Gift(name=name) if gift is None: return render_to_response('empty.html') # update category and groups category = data.get('subcategory', '-') group = data.get('group', '-') try: a = gift.category.name b = gift.group.name except Exception: gift.category = gift.group = None if category != '-' and group != '-': if not category or not group: if gift.category or gift.group: gift.category = None gift.group = None flag = True else: category_obj = Category.all().filter('name =', category) if category_obj.count(): category_obj = category_obj[0] else: category_obj = Category(name=category) category_obj.put() if not gift.category or gift.category != category_obj: gift.category = category_obj flag = True group_obj = Group.all().filter( 'category =', category_obj).filter('name =', group) if group_obj.count(): group_obj = group_obj[0] else: group_obj = Group(name=group, category=category_obj) group_obj.put() if not gift.group or gift.group != group_obj: gift.group = group_obj flag = True # and other fields for key in data.keys(): if key in except_fields: continue value = data.get(key, None) if value is None: continue if key == 'receipt_date': try: new_date = datetime.date( datetime.strptime(value, '%Y-%m-%d')) if new_date == gift.receipt_date: continue gift.receipt_date = new_date flag = True except ValueError: if gift.receipt_date is not None: gift.receipt_date = None flag = True continue try: old_val = getattr(gift, key) if old_val == value: continue setattr(gift, key, value) flag = True except AttributeError: pass if flag: gift.put() gift_views.get_gift(gift.uid, True) return render_to_response('empty.html')