def publish(cls, db, fileset_json): winnow.validate(fileset_json) path_elements = fileset_json["path"].split("/") if len(path_elements) != 6: raise OptionsExceptionFailedValidation( "Fileset path should have 5 elements %s" % path_elements) product_path = "/".join(path_elements[:-1]) product = WinnowProduct.get_from_path(db, product_path) product_doc = product.get_doc() if product is None: raise OptionsExceptionReferenceError( "Reference Error: couldn't find %s" % product_path) version = product_doc["version"] kwargs = { "product_version": "%s@%s.%s.%s" % (product_doc["path"], version[0], version[1], version[2]) } fileset = WinnowFileset.add_doc(db, fileset_json, kwargs=kwargs) # TODO check how this works now # if not product.allows(fileset): # raise OptionsExceptionNotAllowed("fileset not allowed: %s" % utils.json_dumps(fileset_json)) db.index_fileset(product_path, fileset.kwargs[u"uuid"]) return fileset
def publish(cls, db, fileset_json): winnow.validate(fileset_json) path_elements = fileset_json["path"].split("/") if len(path_elements) != 6: raise OptionsExceptionFailedValidation("Fileset path should have 5 elements %s" % path_elements) product_path = "/".join(path_elements[:-1]) product = WinnowProduct.get_from_path(db, product_path) product_doc = product.get_doc() if product is None: raise OptionsExceptionReferenceError("Reference Error: couldn't find %s" % product_path) version = product_doc["version"] kwargs = {"product_version": "%s@%s.%s.%s" % (product_doc["path"], version[0], version[1], version[2])} fileset = WinnowFileset.add_doc(db, fileset_json, kwargs=kwargs) # TODO check how this works now # if not product.allows(fileset): # raise OptionsExceptionNotAllowed("fileset not allowed: %s" % utils.json_dumps(fileset_json)) db.index_fileset(product_path, fileset.kwargs[u"uuid"]) return fileset
def test_breaking_context(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"size":{ u'default': u'birch-ply', u'description': u'Choose a material', u'name': u'Material', u'type': u'set::string', u'values': [{u'description': u'...', u'images': [{u'asset': u'../assets/publish/lean_logo.png', u'type': u'banner'}], u'name': u'Birch Ply', u'options': {u'finish': {u'default': u'/finishes/opendesk/standard-birch-ply', u'scopes': [u'maker'], u'type': u'set::resource', u'values': [u'$ref:/finishes/opendesk/standard-birch-ply', u'$ref:/finishes/opendesk/premium-birch-ply']}}, u'type': u'string', u'value': u'birch-ply'}, {u'description': u'...', u'image': {u'asset': u'../assets/publish/lean_logo.png'}, u'name': u'Standard Laminate', u'options': { u'colour': { u'default': u'white', u'description': u'Choose a colour', u'name': u'Colour', u'type': u'set::string', u'values': [{u'b': u'0', u'g': u'255', u'name': u'White', u'r': u'0', u'type': u'string', u'value': u'white'}] }, u'finish': {u'default': u'/finishes/opendesk/standard-laminate', u'scopes': [u'maker'], u'type': u'set::resource', u'values': [u'$ref:/finishes/opendesk/standard-laminate'] } }, u'type': u'string', u'value': u'standard-laminate'}, {u'description': u"Wild and whacky stuff that you'll talk to the maker about", u'images': [{u'asset': u'../assets/publish/lean_logo.png'}], u'name': u'Custom Lamination', u'options': {u'finish': {u'default': u'/finishes/opendesk/custom-lamination', u'scopes': [u'maker'], u'type': u'set::resource', u'values': [u'$ref:/finishes/opendesk/custom-lamination']}}, u'type': u'string', u'value': u'custom-lamination'}]} } } winnow.validate(doc)
def test_list_basic_set(self): # empty doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"colour":{ u'type': u'set::string', u'values': [u'blue', u'red'] } } } winnow.validate(doc)
def test_basic_set(self): # empty doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options": { u"colour": { u'type': u'set::string', u'values': u'blue' } } } winnow.validate(doc)
def test_reference_set(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"processes":{ u'scopes': [u'maker'], u'type': u'set::resource', u'values': [ u'$ref:/processes/fine-sanding', u'$ref:/processes/oiling' ] } } } winnow.validate(doc)
def test_breaking_set(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options": { u"size": { u'description': u'available sheet sizes', u'name': u'sizes', u'type': u'set::string', u'values': [{ u'type': u'string', u'value': u'1200x2400' }] } } } winnow.validate(doc)
def test_reference_set(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options": { u"processes": { u'scopes': [u'maker'], u'type': u'set::resource', u'values': [ u'$ref:/processes/fine-sanding', u'$ref:/processes/oiling' ] } } } winnow.validate(doc)
def test_breaking_resource(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"size":{ u'changes': u'Initial public version.', u'description': u'Pre laminated plywood with polypropylene coating on a lacquered surface.', u'name': u'WISA Multiwall', u'options': {}, u'path': u'/materials/opendesk/sheets/wood/composite/plywood/laminated/pre-laminated/wisa-multiwall', u'schema': u'https://opendesk.cc/schemata/material.json', u'source': u'https://github.com/opendesk/collection', u'type': u'material', u'version': [0,0, 1] } } } winnow.validate(doc)
def test_breaking_set(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"size":{ u'description': u'available sheet sizes', u'name': u'sizes', u'type': u'set::string', u'values': [ { u'type': u'string', u'value': u'1200x2400' } ] } } } winnow.validate(doc)
def test_breaking_resource(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options": { u"size": { u'changes': u'Initial public version.', u'description': u'Pre laminated plywood with polypropylene coating on a lacquered surface.', u'name': u'WISA Multiwall', u'options': {}, u'path': u'/materials/opendesk/sheets/wood/composite/plywood/laminated/pre-laminated/wisa-multiwall', u'schema': u'https://opendesk.cc/schemata/material.json', u'source': u'https://github.com/opendesk/collection', u'type': u'material', u'version': [0, 0, 1] } } } winnow.validate(doc)
def test_breaking_spec(self): doc = { "files": { "large-1": { "asset": "https://m2.smartdesk.cc/03103/0328/LYT_STD_LRG_AP_cad-1_18.00-0.00.dxf", "name": "Large", "num_sheets": 1 }, "medium-2": { "asset": "https://m1.smartdesk.cc/03104/0328/LYT_STD_MED_AP_cad-1_18.00-0.00.dxf", "name": "Medium", "num_sheets": 1 }, "small-3": { "asset": "https://m2.smartdesk.cc/03105/0328/LYT_STD_SML_AP_cad-1_18.00-0.00.dxf", "name": "Small", "num_sheets": 1 } }, "fileset": "/ranges/tetrad/layout-table/standard-public/[email protected]", "manufacturing": { "cutting": { "total_sheets": 3 }, "strategies": { "A": { "options": { "sheet-1": { "options": { "material": { "$ref": "/materials/opendesk/sheets", "override": False } }, "sheet": "$ref:~/files/large-1", "type": "string", "value": "large-1" }, "sheet-2": { "options": { "material": { "$ref": "/materials/opendesk/sheets", "override": False } }, "sheet": "$ref:~/files/medium-2", "type": "string", "value": "medium-2" }, "sheet-3": { "options": { "material": { "$ref": "/materials/opendesk/sheets", "override": False } }, "sheet": "$ref:~/files/small-3", "type": "string", "value": "small-3" } }, "type": "string", "value": "A" } }, "units": "mm" }, "options": { "material-choices": { "default": "birch-ply", "description": "Choose a material", "name": "Material", "type": "set::string", "values": { "description": "...", "images": [ { "asset": "../assets/publish/lean_logo.png", "type": "banner" } ], "name": "Birch Ply", "options": { "finish": { "default": "$ref:/finishes/opendesk/premium-birch-ply", "scopes": [ "maker" ], "type": "set::resource", "values": { "changes": "Initial version", "description": "Highest quality retail-grade birch-ply with two stage fine finishing.", "name": "Premium Birch Plywood", "options": { "material": { "changes": "Initial version", "description": "Birch-faced natural plywood.", "name": "Birch Faced Plywood", "options": { "strategy": { "default": "A", "scopes": [ "maker", "operator" ], "type": "set::string", "values": "$ref:~/manufacturing/strategies/A" } }, "path": "/materials/opendesk/sheets/wood/composite/plywood/birch-faced-plywood", "schema": "https://opendesk.cc/schemata/material.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "material", "version": [ 1, 0, 0 ] }, "processes": [ { "changes": "Initial version", "description": "", "name": "Oiling", "options": {}, "path": "/processes/opendesk/oiling", "schema": "https://opendesk.cc/schemata/process.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "process", "version": [ 1, 0, 0 ] }, { "changes": "initial version", "description": "", "name": "Fine Sanding", "options": {}, "path": "/processes/opendesk/fine-sanding", "schema": "https://opendesk.cc/schemata/process.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "process", "version": [ 1, 0, 0 ] } ] }, "path": "/finishes/opendesk/premium-birch-ply", "schema": "https://opendesk.cc/schemata/finish.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "finish", "version": [ 1, 0, 0 ] } } }, "type": "string", "value": "birch-ply" } }, "quantity": { "default": 1, "max": 100, "min": 1, "name": "Quantity", "type": "numeric::range" } }, "product": "/ranges/tetrad/layout-table/[email protected]", "schema": "https://opendesk.cc/schemata/manufacturing_specification.json", "type": "manufacturing_specification" } winnow.validate(doc)
def publish(cls, db, as_json): winnow.validate(as_json) return cls.add_doc(db, as_json)
def publish(cls, db, as_json): winnow.validate(as_json) return cls.add_doc(db, as_json)
def test_breaking_spec(self): doc = { "files": { "large-1": { "asset": "https://m2.smartdesk.cc/03103/0328/LYT_STD_LRG_AP_cad-1_18.00-0.00.dxf", "name": "Large", "num_sheets": 1 }, "medium-2": { "asset": "https://m1.smartdesk.cc/03104/0328/LYT_STD_MED_AP_cad-1_18.00-0.00.dxf", "name": "Medium", "num_sheets": 1 }, "small-3": { "asset": "https://m2.smartdesk.cc/03105/0328/LYT_STD_SML_AP_cad-1_18.00-0.00.dxf", "name": "Small", "num_sheets": 1 } }, "fileset": "/ranges/tetrad/layout-table/standard-public/[email protected]", "manufacturing": { "cutting": { "total_sheets": 3 }, "strategies": { "A": { "options": { "sheet-1": { "options": { "material": { "$ref": "/materials/opendesk/sheets", "override": False } }, "sheet": "$ref:~/files/large-1", "type": "string", "value": "large-1" }, "sheet-2": { "options": { "material": { "$ref": "/materials/opendesk/sheets", "override": False } }, "sheet": "$ref:~/files/medium-2", "type": "string", "value": "medium-2" }, "sheet-3": { "options": { "material": { "$ref": "/materials/opendesk/sheets", "override": False } }, "sheet": "$ref:~/files/small-3", "type": "string", "value": "small-3" } }, "type": "string", "value": "A" } }, "units": "mm" }, "options": { "material-choices": { "default": "birch-ply", "description": "Choose a material", "name": "Material", "type": "set::string", "values": { "description": "...", "images": [{ "asset": "../assets/publish/lean_logo.png", "type": "banner" }], "name": "Birch Ply", "options": { "finish": { "default": "$ref:/finishes/opendesk/premium-birch-ply", "scopes": ["maker"], "type": "set::resource", "values": { "changes": "Initial version", "description": "Highest quality retail-grade birch-ply with two stage fine finishing.", "name": "Premium Birch Plywood", "options": { "material": { "changes": "Initial version", "description": "Birch-faced natural plywood.", "name": "Birch Faced Plywood", "options": { "strategy": { "default": "A", "scopes": ["maker", "operator"], "type": "set::string", "values": "$ref:~/manufacturing/strategies/A" } }, "path": "/materials/opendesk/sheets/wood/composite/plywood/birch-faced-plywood", "schema": "https://opendesk.cc/schemata/material.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "material", "version": [1, 0, 0] }, "processes": [{ "changes": "Initial version", "description": "", "name": "Oiling", "options": {}, "path": "/processes/opendesk/oiling", "schema": "https://opendesk.cc/schemata/process.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "process", "version": [1, 0, 0] }, { "changes": "initial version", "description": "", "name": "Fine Sanding", "options": {}, "path": "/processes/opendesk/fine-sanding", "schema": "https://opendesk.cc/schemata/process.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "process", "version": [1, 0, 0] }] }, "path": "/finishes/opendesk/premium-birch-ply", "schema": "https://opendesk.cc/schemata/finish.json", "source": "https://opendesk.herokuapp.com/api/v1/open", "type": "finish", "version": [1, 0, 0] } } }, "type": "string", "value": "birch-ply" } }, "quantity": { "default": 1, "max": 100, "min": 1, "name": "Quantity", "type": "numeric::range" } }, "product": "/ranges/tetrad/layout-table/[email protected]", "schema": "https://opendesk.cc/schemata/manufacturing_specification.json", "type": "manufacturing_specification" } winnow.validate(doc)
def test_breaking_context(self): doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options": { u"size": { u'default': u'birch-ply', u'description': u'Choose a material', u'name': u'Material', u'type': u'set::string', u'values': [{ u'description': u'...', u'images': [{ u'asset': u'../assets/publish/lean_logo.png', u'type': u'banner' }], u'name': u'Birch Ply', u'options': { u'finish': { u'default': u'/finishes/opendesk/standard-birch-ply', u'scopes': [u'maker'], u'type': u'set::resource', u'values': [ u'$ref:/finishes/opendesk/standard-birch-ply', u'$ref:/finishes/opendesk/premium-birch-ply' ] } }, u'type': u'string', u'value': u'birch-ply' }, { u'description': u'...', u'image': { u'asset': u'../assets/publish/lean_logo.png' }, u'name': u'Standard Laminate', u'options': { u'colour': { u'default': u'white', u'description': u'Choose a colour', u'name': u'Colour', u'type': u'set::string', u'values': [{ u'b': u'0', u'g': u'255', u'name': u'White', u'r': u'0', u'type': u'string', u'value': u'white' }] }, u'finish': { u'default': u'/finishes/opendesk/standard-laminate', u'scopes': [u'maker'], u'type': u'set::resource', u'values': [u'$ref:/finishes/opendesk/standard-laminate'] } }, u'type': u'string', u'value': u'standard-laminate' }, { u'description': u"Wild and whacky stuff that you'll talk to the maker about", u'images': [{ u'asset': u'../assets/publish/lean_logo.png' }], u'name': u'Custom Lamination', u'options': { u'finish': { u'default': u'/finishes/opendesk/custom-lamination', u'scopes': [u'maker'], u'type': u'set::resource', u'values': [u'$ref:/finishes/opendesk/custom-lamination'] } }, u'type': u'string', u'value': u'custom-lamination' }] } } } winnow.validate(doc)
def test_validate_refs(self): with open(os.path.join(DATA_DIR, "product_with_components.json"), "r") as f: product_dict = json.loads(f.read()) winnow.validate(product_dict)
def test_regex(self): # empty doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ } } winnow.validate(doc) # with a string ref doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"inline": u"$ref:/something/that/should/pass" } } winnow.validate(doc) # with an object ref doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"inline": { u"$ref": u"/something/that/should/pass" } } } winnow.validate(doc) # with an object ref with options doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"inline": { u"$ref": u"/something/that/should/pass", u"options":{} } } } winnow.validate(doc) # extra attrs should fail doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"inline": { u"$ref": u"/something/that/should/pass", u"options":{}, u"should_fail": {} } } } self.assertRaises(OptionsExceptionFailedValidation, winnow.validate, doc ) # any string should pass doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"inline": u"something/that/should/pass" } } winnow.validate(doc) # except if it starts with a $ but isnt a correct ref doc = { u"schema": u"https://opendesk.cc/schemata/options.json", u"type": u"option", u"options":{ u"inline": u"$ref/something/that/should/fail" } } self.assertRaises(OptionsExceptionFailedValidation, winnow.validate, doc )
def validate(self): winnow.validate(self.get_doc())
def validate(self): winnow.validate(self.get_doc())