Beispiel #1
0
def save_product(request):
    """ handles both update and create requests """
    data = request.json_body
    for item in data['items']:
        item_ = json.loads(item)
        try:
            validate(item_, json.loads(request.context.product_schema))
        except ValidationError as e:
            log.info("invalid data {}".format(e.message))
            continue
        if request.subpath:
            try:
                product = request.context.get_product(request.subpath[0])
            except DoesNotExist:
                log.info("product not found")
                continue
        else:
            product = Hardware()
            product._id = ObjectId()
            product.category = request.context.name

        for field in item_:
            # there can not be a . in a field name so we remove that.
            new_field = field.replace(".", "")
            setattr(product, new_field, item_[field])

        request.context.products.append(product)
    request.context.products.save()
    return {"message": "products saved"}
Beispiel #2
0
 def create_hardware():
     hardware = Hardware()
     hardware.name = 1