Exemple #1
0
    def post(self, request, format=None):

        #cat = Category.objects.get(pk=request.data.get('cat'))
        #subcat = SubCategory.objects.get(pk=request.data.get('subcat'))
        p = Product()
        #p.category = cat
        p.price = request.data.get('price')
        p.user = request.user.userprofile
        #p.subcategory = subcat
        p.name = request.data.get('name')
        

        if "image" in request.data:
            p.image = (request.data['image'])

        if "image_base64" in request.data:
            try:
                format, imgstr = request.data.get('image_base64').split(';base64,')
                ext = format.split('/')[-1]
                data = ContentFile(base64.b64decode(imgstr))
                file_name = '%s_user.%s' % (p.id,ext) 
                p.image.save(file_name, data, save=True)
            except:
                pass

        p.save()

        return Response(ProductSerializer(p).data)
Exemple #2
0
def product_insert(request):
    # Check if Request is POST
    if request.method != 'POST':
        raise Exception('Http Method is not POST')

    # Get JSON Data from request
    json_data = json.loads(request.body)

    # Check existence of fields
    if ('code' or 'name' or 'price') not in json_data:
        return JsonResponse({"message": "some fields are not given"},
                            status=400)

    # Check value type of fields
    if (type(json_data['code']) != str) or (type(json_data['name']) != str) or (type(json_data['price']) != int) \
            or (len(json_data['code']) > 10) or (len(json_data['name']) > 100):
        return JsonResponse({"message": "value of fields have problem"},
                            status=400)

    # Check if code is unique
    if Product.objects.all().filter(code=json_data['code']).exists():
        return JsonResponse({"message": "code must be unique"}, status=400)

    # Check if price is positive
    if json_data['price'] < 0:
        return JsonResponse({"message": "price value must be greater than 0"},
                            status=400)

    # Create a product
    product = Product()
    product.code = json_data['code']
    product.name = json_data['name']
    product.price = json_data['price']

    # Check if inventory filed is given and is positive
    if 'inventory' not in json_data:
        product.inventory = 0
    else:
        if json_data['inventory'] < 0:
            return JsonResponse(
                {"message": "value of inventory must be greater than 0"},
                status=400)
        product.inventory = json_data['inventory']

    # Save product
    product.save()
    return JsonResponse({"id": product.id}, status=201)
Exemple #3
0
def get_product_description(link, category):
    URL = 'https://tainabox.com.ua'
    print('Start importing from %s' % URL + link)
    # rez = requests.get(URL + link, verify=False)
    rez = requests.get(URL + link)
    soup = BeautifulSoup(rez.text, 'html.parser')

    for desc in soup.findAll('div', {'class': 'product__big-item'}):
        image = desc.find('div', {
            'class': 'product__big-item_right'
        }).find('img')
        consist = desc.find('div',
                            {'class': 'product__item__composition__value'})
        price = desc.find('div', {'class': 'to-order__value'})
        name = desc.find('div', {'product__big-item__name'})

        in_stop = False
        for w in STOP_WORDS:
            if name.text.find(w) > -1:
                in_stop = True
            if consist.text.find(w) > -1:
                in_stop = True

        if not in_stop:
            p = Product()
            p.name = re.sub('\n', '', name.text)
            p.price = re.split(r'\n', price.text)[0]
            p.consist = consist.text
            p.category = category
            img_url = URL + image['src']

            img_temp = NamedTemporaryFile(delete=True)
            req = urllib.request.Request(
                img_url,
                data=None,
                headers={
                    'User-Agent':
                    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
                })

            img_temp.write(urllib.request.urlopen(req).read())
            img_temp.flush()
            img_res = re.split(r'\.', image['src'])
            p.image.save("image_.{}".format(img_res[-1]), File(img_temp))
            p.save()