Ejemplo n.º 1
0
    def delete_spec(self):
        """

        :return:
        """
        query = request.get_json()
        sku = query.get('sku')
        if 'ADMIN' not in current_user.roles:
            return jsonify(message='Failed',
                           desc='Sorry, you dont have permission to do this')
        try:
            ItemSpec.objects(sku=sku).delete()
            return jsonify(message='OK')
        except Exception as e:
            return jsonify(message='Failed', desc=e)
Ejemplo n.º 2
0
def get_spec_info(sku):
    """

    :param sku:
    :return:
    """
    spec = ItemSpec.objects(sku=sku).first()
    if not spec:
        return {'sku': sku, 'found': False}
    item = Item.objects(item_id=spec.item_id).first()
    brand = Brand.objects(en=item.brand).first()
    brand_json = brand and brand.to_json() or ''
    return {
        'sku': sku,
        'found': True,
        'item_id': spec.item_id,
        'title': item.title,
        'brand': brand_json,
        'primary_image': item.primary_img,
        'item_available': item.availability,
        'price': spec.price,
        'available': spec.availability,
        'attributes': spec.attributes,
        'images': spec.images,
        'can_not_return': item.extra.get('can_return') is False,
    }
Ejemplo n.º 3
0
    def add_spec(self):
        """

        :return:
        """
        query = request.get_json()
        data = {}
        for k, v in query.items():
            if v in [None, 'None', '', 'null']:
                continue
            elif 'price' in k:
                val = float(v)
            elif k == 'images':
                images = v.split(',')
                val = []
                for img in images:
                    if img.startswith('http://assets.maybe'):
                        val.append(img)
                    else:
                        path = '{}/{}.jgp'.format(query.get('brand', 'other'),
                                                  uuid4())
                        url = jobs.image.upload('maybe-img',
                                                path,
                                                url=img,
                                                make_thumbnails=True)
                        val.append(url)
            else:
                val = v
            data.update({k: val})
        data.update({'web_sku': str(time.time()).replace('.', '')})
        try:
            spec = ItemSpec(**data).save()
            return jsonify(message='OK', spec=spec)
        except Exception as e:
            return jsonify(message='Failed', desc=e)
Ejemplo n.º 4
0
    def __init__(self,
                 entries_info,
                 user=None,
                 logistic_provider=None,
                 address=None):
        entries = []
        for info in entries_info:
            spec = ItemSpec.objects(sku=info['sku'].first())
            if not spec or not spec.availability:
                continue

            item = Item.objects(item_id=info['item_id']).first()
            if not item or not item.availability:
                continue

            entry = FakeEntry(spec=spec, item=item, quantity=info['quantity'])
            entries.append(entry)
        for entry in entries:
            entry.update_amount()

        self.entries = entries
        self.customer = user
        self.logistic_provider = logistic_provider
        self.address = address
        self.coupon_codes = []
        self.coin = 0
        self.cash = 0
        self.id = 'fakecart'
Ejemplo n.º 5
0
    def is_change(self):
        head = ItemSpec.objects(sku=self.head).first()
        if not head:
            return True
        if self.modified != head.modified:
            return True

        return False
Ejemplo n.º 6
0
    def update_to_head(self):
        head = ItemSpec.objects(sku=self.head).first()
        if self.item and isinstance(self.item, ItemSnapshot):
            self.item.update_to_head()

        if head:
            data = head._data
            for k, v in data.items():
                setattr(self, k, v)
            self.save()

        else:
            return self
Ejemplo n.º 7
0
    def create_from_skus(cls,
                         customer_id,
                         skus,
                         logistic_provider,
                         coupon_codes,
                         coin=0,
                         cash=0,
                         address=None,
                         **kwargs):
        entries = []
        for s in skus:
            availability = check_availability_and_update_stock(
                s['item_id'], s['sku'], s['quantity'])
            if not availability:
                return s
            spec = ItemSpec.objects(sku=s['sku']).first()
            item = Item.objects(item_id=['item_id']).first()
            entry = OrderEntry(spec=spec, item=item,
                               quantity=s['quantity']).save()
            entries.append(entry)

        order = cls(customer_id=customer_id,
                    entries=entries,
                    logistic_provider=logistic_provider,
                    coupon_codes=coupon_codes,
                    coin=coin,
                    cash=cash,
                    **kwargs)
        if not order.forex:
            order.forex = ForexRate.get()

        order.update_amount()
        order.reload()
        # 简介
        for e in order.entries:
            e.create_snapshot()

        if address:
            order.set_address(address)

        order_created.send('system', order=order)
        return order
Ejemplo n.º 8
0
    def update_spec(self):
        """

        :return:
        """
        query = request.get_json()
        data = {}
        for k, v in query.items():
            if v in [None, 'None', '', 'null']:
                continue
            if k != 'attributes' and type(v) == dict:
                continue
            elif 'price' in k:
                val = float(v)
            elif k == 'images':
                val = []
                if type(v) == 'unicode':
                    v = v.split(',')
                for img in v:
                    if img.startswith('http://assets.maybe'):
                        val.append(img)
                    else:
                        path = '{}/{}.jpg'.format(query.get('brand', 'other'),
                                                  uuid4())
                        url = jobs.image.upload('maybe-img',
                                                path,
                                                url=img,
                                                make_thumbnails=True)
                        val.append(url)

                else:
                    val = v
                data.update({k: val})

            try:
                spec = ItemSpec.objects(sku=data['_id']).first()
                spec.update_spec(data)
                return jsonify(message='OK', spec=spec)
            except Exception as e:
                return jsonify(message='Failed', desc=e)
Ejemplo n.º 9
0
def import_items(ls):
    """

    :param ls:
    :return:
    """
    translator = MSTranslate('maybe', '')
    for row in ls:
        f = row.split('\t')
        url = f[0]
        title = f[1]
        title_en = translator.translate(title, 'en', 'zh')
        china_price = float(float(f[2] + float(f[3])))
        price = float(str(china_price / 6.3).split('.')[0] + '.99')
        origin_price = float(str(price * 1.2).split('.')[0] + '.99')
        weight = float(f[4])
        attributes = f[8].replace('{', '').replace('}', '').split(':')
        vendor = 'taobao'
        brand = 'other'
        main_category = 'home'
        sub_category = 'storage box'
        primary_img = f[9].replace(', ', ',').split(',')
        description = f[10]
        ps = parse.urlparse(url)
        web_id = parse.parse_qs(ps.query)['id'][0]

        images = []
        for img in primary_img:
            path = '{}/{}.jpeg'.format(brand, uuid4())
            url = upload('maybe-img', path, url=img, make_thumbnails=True)
            images.append(url)

        item = Item.objects(web_id=web_id).first()
        if item:
            web_sku = 'taobao:%s' % primary_img[0].split('/')[-2]
            ItemSpec(
                **{
                    'item_id': item.item_id,
                    'web_sku': web_sku,
                    'images': images,
                    'original_price': origin_price,
                    'price': price,
                    'attribute': {
                        attributes[0]: attributes[1]
                    }
                }).save()
            continue

        data = {
            'meta': {
                'url': url,
                'web_id': web_id,
                'title': title,
                'title_en': title_en,
                'china_price': china_price,
                'original_price': origin_price,
                'main_category': main_category,
                'sub_category': sub_category,
                'price': price,
                'weight': weight,
                'attributes': [attributes[0]],
                'vendor': vendor,
                'brand': brand,
                'primary_img': primary_img[0],
                'description': description,
                'currency': 'USD',
                'sex_tag': 'UNCLASSIFIED',
                'tags': []
            },
            'specs': [{
                'web_sku': 'taobao: %s' % primary_img[0].split('/')[-2],
                'image': images,
                'original_price': origin_price,
                'price': price,
                'attribute': {
                    attributes[0]: attributes[1]
                }
            }],
        }
        Item.create(data)