Example #1
0
def save_monochrome_image(c, data):
    if 'image' in data:
        # read the new image and upload it
        monochrome_logo = import_monochrome_image(data.get('image'), g.IMAGE_DIMENSIONS['monochrome_logo'], 'fit')

        if not monochrome_logo:
            return JsonError(_("No file sent"))

        if c.monochrome_logo.name:
            # the logo exists already, delete it
            try:
                os.remove(c.monochrome_logo.path)
            except (OSError, ValueError):
                pass

        f = create_file_from_image(monochrome_logo)

        c.monochrome_logo.save(f['name'], f['file'], save=False)
        c.save()

        return JsonResponse({'status': 'ok', 'logo_url': c.monochrome_logo.url})
    else:
        # there is no image, remove the existing one
        try:
            os.remove(c.monochrome_logo.path)
            c.monochrome_logo.delete()
            c.save()
        except (OSError, ValueError):
            pass

        return JsonOk()
Example #2
0
def edit_product_(request, c, android=False):
    # sellers can edit product
    if not has_permission(request.user, c, 'product', 'edit'):
        return JsonError(_("You have no permission to edit products"))

    data = JsonParse(request.POST['data'])

    # see if product exists in database
    product_id = data['id']
    try:
        product = Product.objects.get(company=c, id=product_id)
    except:
        return JsonError(_("Product does not exist"))
    
    # validate data
    valid = validate_product(request.user, c, data)
    if not valid['status']:
        return JsonError(valid['message'])
    data = valid['data']
    
    # update product:
    product.name = data.get('name')
    product.category = data.get('category')
    product.unit_type = data.get('unit_type')
    product.code = data.get('code')
    product.shortcut = data.get('shortcut')
    product.description = data.get('description')
    product.private_notes = data.get('private_notes')
    # product.stock = data.get('stock')
    product.tax = data.get('tax')
    
    # update discounts
    product.update_discounts(request.user, data['discount_ids'])
    
    # image
    if data['change_image'] == True:
        if data.get('image'):  # new image is uploaded
            # create a file from the base64 data and save it to product.image
            if product.image:
                product.image.delete()
            # save a new image (conversion is done in validate_product)
            f = create_file_from_image(data['image'])
            product.image = f['file']
        else:  # delete the old image
            product.image.delete()
    
    # category
    if data['category']:
        product.category = data['category']

    # price has to be updated separately
    product.price = product.update_price(Price, request.user, data['price'])
    # if data.get('purchase_price'):
    #     product.price = product.update_price(PurchasePrice, request.user, data['purchase_price'])

    product.updated_by = request.user
    product.save()

    return JsonOk(extra=product_to_dict(request.user, c, product, android))
Example #3
0
def create_product_(request, c, android=False):
    # sellers can add product
    if not has_permission(request.user, c, 'product', 'edit'):
        return JsonError(_("You have no permission to add products"))

    data = JsonParse(request.POST['data'])
    
    # validate data
    valid = validate_product(request.user, c, data)
    if not valid['status']:
        return JsonError(valid['message'])
    data = valid['data']
    
    # save product:
    product = Product(
        company=c,
        created_by=request.user,
        category=data.get('category'),
        name=data.get('name'),
        code=data.get('code'),
        shortcut=data.get('shortcut'),
        description=data.get('description'),
        private_notes=data.get('private_notes'),
        stock=data.get('stock'),
        tax=data.get('tax'),
    )
    product.save()
    
    # update discounts
    product.update_discounts(request.user, data['discount_ids'])
    
    # prices have to be updated separately
    price = product.update_price(Price, request.user, data['price']) # purchase price
    if not price:
        product.delete()
        return JsonError(_("Error while setting purchase price"))


    #if data.get('purchase_price'):
    #    price = product.update_price(PurchasePrice, request.user, data['purchase_price'])
    #    if not price:
    #        product.delete()
    #        return JsonError(_("Error while setting sell price"))
    
    # add image, if it's there
    if data['change_image']:
        if 'image' in data:
            f = create_file_from_image(data['image'])
            product.image = f['file']
            product.save()
    
    return JsonOk(extra=product_to_dict(request.user, c, product, android))