Exemple #1
0
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')
Exemple #2
0
def get_gift(request):
    ip = get_ip(request)
    if ip:
        logging.info("Remote ip: %s" % ip)
    gift_key = request.values.get("uid", None)
    if gift_key is None:
        return Response(status=404)
    gift = gift_views.get_gift(gift_key)
    result = {}
    if gift:
        gift.main_thumb_url = get_main_thumb_url(gift)
        gift.mini_main_thumb_url = get_mini_main_thumb_url(gift)
        gift.main_img_url = get_main_img_url(gift)
        gift.thumbs_url = []
        gift.imgs_url = []
        gift.mini_thumbs_url = []
        for key in gift.thumbs:
            thumb = get_thumb_url(key)
            if thumb:
                gift.thumbs_url.append(get_thumb_url(key))
                gift.mini_thumbs_url.append(get_thumb_url(key, thumb_size="100x100"))
                gift.imgs_url.append(get_img_url(key))
        result['status'] = gift.status
        if result['status'] is None:
            result['status'] = 90
        result["uid"] = gift.uid
        result['id_1c'] = gift.id_1c
        result['catalogue_id'] = gift.catalogue_id
        if result['catalogue_id'] is None:
            result['catalogue_id'] = ''
        result['is_novelty'] = gift.is_novelty
        result['badge'] = gift.badge
        if result['badge'] is None:
            result['badge'] = u''
        result['master_box'] = gift.master_box
        if result['master_box'] is None:
            result['master_box'] = ''
        result['country'] = gift.country
        if result['country'] is None:
            result['country'] = ''
        result['material'] = gift.material
        if result['material'] is None:
            result['material'] = ''
        result['box_type'] = gift.box_type
        if result['box_type'] is None:
            result['box_type'] = ''
        result['gift_size'] = json_quotes_markup(None, gift.gift_size)
        result['gift_box_size'] = json_quotes_markup(None, gift.gift_box_size)
        result['weight'] = json_quotes_markup(None, gift.weight)
        result['gift_box_weight'] = json_quotes_markup(None, gift.gift_box_weight)
        result['rating'] = gift.rating
        if result['rating'] is None:
            result['rating'] = 0
        result["name"] = json_quotes_markup(None, gift.name)
        result["name_origin"] = json_quotes_markup(None, gift.name_origin)
        result["brand"] = json_quotes_markup(None, gift.brand)
        if gift.barcode:
            result["barcode"] = gift.barcode
        else:
            result["barcode"] = ""
        result["block_amount"] = gift.block_amount if gift.block_amount else ''
        result["box_size"] = json_quotes_markup(None, gift.box_size)
        result["box_volume"] = json_quotes_markup(None, gift.box_volume)
        result['box_weight'] = json_quotes_markup(None, gift.box_weight)
        result['box_amount_pallet'] = gift.box_amount_pallet if gift.box_amount_pallet else ''
        result["price"] = gift.price
        result["real_price"] = gift.real_price
        if gift.vat:
            result['vat'] = gift.vat
        else:
            result['vat'] = 0
        result["modif_time"] = gift.modif_time.isoformat()
        result["add_time"] = gift.add_time.isoformat()
        if not gift.receipt_date:
            result["receipt_date"] = ""
        else:
            result["receipt_date"] = gift.receipt_date.isoformat()
        if gift.description is None:
            result["description"] = ""
        else:
            result["description"] = json_quotes_markup(None, clear_data(gift.description))
        if not gift.equipment:
            result["equipment"] = ""
        else:
            result["equipment"] = json_quotes_markup(None, clear_data(gift.equipment))
        result["main_thumb_url"] = gift.main_thumb_url
        result["thumbs_url"] = gift.thumbs_url

        result["mini_main_thumb_url"] = gift.mini_main_thumb_url

        imgs = []
        if gift.mini_main_thumb_url:
            imgs.append(gift.mini_main_thumb_url)
        for img_url in gift.mini_thumbs_url:
            if img_url != gift.mini_main_thumb_url:
                imgs.append(img_url)
        result["mini_thumbs_url"] = imgs

        result["main_img_url"] = gift.main_img_url

        imgs = []
        if gift.main_img_url:
            imgs.append(gift.main_img_url)
        for img_url in gift.imgs_url:
            if img_url != gift.main_img_url:
                imgs.append(img_url)
        result["imgs_url"] = imgs

        try:
            if gift.category:
                result["subcategory"] = gift.category.name
            else:
                result["subcategory"] = ''
        except Exception:
            result["subcategory"] = ''
        try:
            if gift.group:
                result["group"] = gift.group.name
            else:
                result["group"] = ''
        except Exception:
            result["group"] = ''

        if not gift.leftovers or gift.in_trash:
            result["leftovers"] = 0
        else:
            result["leftovers"] = gift.leftovers

        result['remote_leftovers'] = gift.remote_leftovers
        if result['remote_leftovers'] is None:
            result['remote_leftovers'] = 0
        result['youtube_id'] = gift.youtube_id
    return render_json_response(result)