def add_variant(self, row): k = "#variant-%s" % self.variant_count self.variant_count += 1 v = self.make_variant(row) self.document[k] = v self.document['#product'][SUS + 'variants'].append(URI(k)) image_key = row[self.key_map['Image Src']] if image_key not in self.images: k = "#image-%s" % self.image_count self.image_count += 1 self.images[image_key] = k v = self.make_image(row) self.document[k] = v self.document['#product'][SUS + 'images'].append(URI(k)) for i in range(1, 3): option_name = row[self.key_map['Option%s Name' % i]] if option_name == '': continue k = "#option-%s" % i if k not in self.document: v = self.make_option(option_name, i) self.document[k] = v self.document['#product'][SUS + 'options'].append(URI(k)) option_value = row[self.key_map['Option%s Value' % i]] value_list = self.document[k][SUS + 'option_values'] if option_value not in value_list: value_list.append(option_value)
def make_image(self, row): image = { RDF + 'type': URI(SUS + 'AnnotatedImage'), SUS + 'image_source': URI(row[self.key_map['Image Src']]), SUS + 'image_alt_text': row[self.key_map['Image Alt Text']] } return image
def account_user2(): body = { '': { RDF + 'type': URI(CE + 'Account'), CE + 'account_id': 'user2', CE + 'password': '******', VCARD + 'email': '*****@*****.**', CE + 'user': URI('#owner'), VCARD + 'adr': BNode('_:address'), DC + 'title': 'user2 account', AC + 'resource-group': URI( '' ) # have this account be it's own resource group (default is /account) }, '#owner': { RDF + 'type': URI(FOAF + 'Person'), FOAF + 'givenName': 'FName', FOAF + 'familyName': 'LName', FOAF + 'nick': 'NName' }, '_:address': { RDF + 'type': URI(VCARD + 'Work'), VCARD + 'street-address': 'unknown', VCARD + 'locality': 'Seville', VCARD + 'region': 'Andalusia', VCARD + 'postal-code': '00000', VCARD + 'country-name': 'Spain' } } user2_r_doc = test_helper.create(container_url=account_container_url, post_body=body) return user2_r_doc
def make_category(self, row, id_prefix): category = { \ '' : { RDF+'type': URI(SUS+'Category'), CE+'id': id_prefix + row[self.key_map['Category']].lower().replace(' ', '-'), DC+'title': row[self.key_map['Category']], SUS+'image_source': URI(row[self.key_map['Image Src']]), SUS+'image_alt_text': row[self.key_map['Image Alt Text']] } } return category
def store_capability(): post_body = { '': { RDF + 'type': URI(CE + 'Capability'), DC + 'title': 'Example Capability', CE + 'improvement_container': URI('/cat/stores'), CE + 'improvement_type': URI('/types/OnlineStore') } } capability_rdoc = test_helper.create(mt_capabilities_url, post_body, username=ADMIN_USER) return capability_rdoc
def make_variant(self, row): variant = { RDF + 'type': URI(SUS + 'Variant'), DC + 'title': self.get_variant_title(row), DC + 'description': row[self.key_map['Description']], SUS + 'sku': row[self.key_map['Variant SKU']], SUS + 'inventory_tracker': row[self.key_map['Variant Inventory Tracker']], SUS + 'inventory_qty': int(row[self.key_map['Variant Inventory Qty']]), SUS + 'inventory_policy': row[self.key_map['Variant Inventory Policy']], SUS + 'fulfillment_service': row[self.key_map['Variant Fulfillment Service']], SUS + 'price': int(100 * float(row[self.key_map['Variant Price']])), SUS + 'compare_at_price': int(100 * float(row[self.key_map['Variant Compare At Price']])), SUS + 'requires_shipping': row[self.key_map['Variant Requires Shipping']] == 'TRUE', SUS + 'taxable': row[self.key_map['Variant Taxable']] == 'TRUE', SUS + 'barcode': row[self.key_map['Variant Barcode']] } return variant
def execute_action(self, body): #TODO: check if post location is valid and body is a form form = body if 'file_to_import' in form: item = form["file_to_import"] if item.file and (item.headers['Content-type'] == 'application/vnd.ms-excel' or item.headers['Content-type'] == 'text/csv'): status, headers, document = self.recursive_get_document() if status == 200: if document.get_value(RDF + 'type') == URI(SUS + 'BackOffice'): store = document.get_value(SUS + 'store') cat_categories_post_url = str( document.get_value(SUS + 'categories', store)) cat_products_post_url = str( url_policy.construct_url(self.request_hostname, self.tenant, self.namespace, 'products')) id_prefix = self.document_id + '-' thread = Thread(target=threaded_import_products, args=(item, cat_categories_post_url, cat_products_post_url, id_prefix, self.user)) thread.start() return (202, [], 'Product import started ...') return super(Domain_Logic, self).execute_action(body)
def make_option(self, option_name, option_index): option = { RDF + 'type': URI(SUS + 'Option'), SUS + 'option_name': option_name, SUS + 'option_index': option_index, SUS + 'option_values': [] } return option
def complete_document_for_storage_insertion(self, document): document_url = document.graph_url types = document.get_values(RDF + 'type', document_url) if URI( SUS + 'OnlineStore' ) in types: # When we create an OnlineStore, we also create a front and back office for it back_office = rdf_json.RDF_JSON_Document( {'': { RDF + 'type': URI(SUS + 'BackOffice') }}, '') status, headers, result = self.create_document(back_office) if status != 201: raise Exception back_office_url = base.get_header('Location', headers) document.add_triples('', {SUS + 'backOffice': URI(back_office_url)}) #If there is a system failure before insertion of the OnlineStore, then the back_office will be a harmless orphan not linked to anything #The stored representation of the back_office does not reference the store (which could fail to get created), but a triple that references the store #is calculated at runtime on GET and added to the representation. See complete_result_document method for details return super(Domain_Logic, self).complete_document_for_storage_insertion(document)
def make_product(self, row, category_url, id_prefix): product = { '': { RDF + 'type': URI(SUS + 'ProductDescription'), CE + 'id': id_prefix + row[self.key_map['Handle']] }, '#product': { RDF + 'type': URI(SUS + 'Product'), RDF + 'isDefinedBy': URI(''), DC + 'title': row[self.key_map['Title']], DC + 'description': row[self.key_map['Description']], SUS + 'vendor': row[self.key_map['Vendor']], SUS + 'category': [URI(category_url)], SUS + 'tags': [row[self.key_map['Tags']]], SUS + 'featured_image': URI('#image-1'), SUS + 'images': [], SUS + 'options': [], SUS + 'variants': [] } } return product
def test_basic_crud(): post_body = { '': { RDF + 'type': URI('http://example.org/todo#Item'), DC + 'title': 'test todo', } } patch_prop = DC + 'title' patch_val = 'updated test todo' test_helper.container_crud_test(ITEMS_URL, post_body, patch_prop, patch_val)
def test_basic_crud(): post_body = { '': { RDF + 'type': URI(SUS + 'Category'), CE + 'id': 'just_some_id', DC + 'title': 'test category', } } patch_prop = DC + 'title' patch_val = 'updated test category' test_helper.container_crud_test(cs_cat_categories_url, post_body, patch_prop, patch_val)
def complete_result_document(self, document): # in this section we add any calculated triples document_url = document.graph_url types = document.get_values(RDF + 'type') if URI(SUS + 'OnlineStore') in types: document.add_triples(document_url, CE + 'user', URI(self.user)) document.add_triples( self.user, SUS + 'cart', URI('/cart/current' + hashlib.sha224(self.user).hexdigest())) self.add_owned_container(document, SUS + 'categories', 'categories', SUS + 'store') if URI(SUS + 'Category') in types: self.add_owned_container(document, SUS + 'categoryProducts', 'products', SUS + 'category') if URI(SUS + 'BackOffice') in types: # add the triples for the store itself #the stored representation of the back_office does not reference the store, but the reference and store triples are added here self.add_resource_triples(document, document_url, SUS + 'backOffice', MEMBER_IS_SUBJECT) store_url = document.get_subject(SUS + "backOffice", document_url) document.add_triples(document_url, SUS + 'store', store_url) return super(Domain_Logic, self).complete_result_document(document)
def run(): requests.delete(hs_mt_app_url, headers=DELETE_HEADERS) requests.delete(cs_cat_app_url, headers=DELETE_HEADERS) requests.delete(hs_ac_app_url, headers=DELETE_HEADERS) requests.delete(cs_ac_app_url, headers=DELETE_HEADERS) requests.delete(cs_cart_url, headers=DELETE_HEADERS) body = { '': { RDF + 'type': URI(AC + 'UserGroup'), AC + 'who': [ URI(ADMIN_USER), URI('http://martin-nally.name'), URI('http://frank-budinsky.name'), URI('http://dave-tropeano.name'), URI('http://paul-matchen.name'), URI('http://mark-archer.name') ], AC + 'may': [URI('#permission_1')], }, '#permission_1': { AC + 'do': AC_ALL, AC + 'to': [URI('/'), URI('/mt/cloudsupplements'), URI('/mt/testsite')] } } r = requests.post(hs_ac_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE user group! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) body['#permission_1'][AC + 'to'] = [URI('/')] r = requests.post(cs_ac_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE access control! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) body = { '': { RDF + 'type': URI(AC + 'UserGroup'), AC + 'who': [URI(ANY_USER)], AC + 'may': [URI('#permission_1'), URI('#permission_2')] }, '#permission_1': { AC + 'do': AC_R, AC + 'to': [URI('/')] }, '#permission_2': { AC + 'do': AC_C, AC + 'to': [URI('/account'), URI('/mt/sites')] } } r = requests.post(hs_ac_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE user group! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) body['#permission_2'][AC + 'to'] = [URI('/cart')] r = requests.post(cs_ac_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE access control! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) body = { '': { RDF + 'type': URI(CE + 'Capability'), DC + 'title': 'IBM Shopping Capability', CE + 'improvement_container': URI('/cat/stores'), CE + 'improvement_type': URI(SUS + 'OnlineStore') } } r = requests.post(mt_capabilities_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE Shopping Service Provider at %s! %s' % ( mt_capabilities_url, r.text) return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) store_capability_url = r.headers['location'] store_capability = RDF_JSON_Document( json.loads(r.text, object_hook=rdf_json_decoder), store_capability_url) body = { '': { RDF + 'type': URI(CE + 'Capability'), DC + 'title': 'IBM Blogging Capability', CE + 'improvement_container': URI('/cat/blogs'), CE + 'improvement_type': URI(BG + 'BlogService') } } r = requests.post(mt_capabilities_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE Blogging service provider! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) blog_capability_url = r.headers['location'] blog_capability = RDF_JSON_Document( json.loads(r.text, object_hook=rdf_json_decoder), blog_capability_url) body = { '': { RDF + 'type': URI(SUS + 'OnlineStore'), DC + 'title': 'Cloud Supplements Shop, LLC', CE + 'capability': URI(store_capability_url) } } r = requests.post(cs_cat_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE Online Store! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) store_url = r.headers['location'] store = RDF_JSON_Document(json.loads(r.text, object_hook=rdf_json_decoder), store_url) cs_cat_categories_url = str(store.get_value(SUS + 'categories')) backoffice_url = str(store.get_value(SUS + 'backOffice')) id_prefix = backoffice_url[backoffice_url.rfind('/') + 1:] + '-' body = { '': { RDF + 'type': URI(BG + 'BlogPost'), DC + 'title': 'First Post', BG + 'content': 'Hello. This is the first blog post', } } r = requests.post(cs_cat_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE BLOG POST! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) blogpost_url = r.headers['location'] body = { '': { RDF + 'type': URI(BG + 'Blog'), DC + 'title': 'News', BG + 'blog_posts': [URI(blogpost_url)] } } r = requests.post(cs_cat_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE BLOG! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) blog_url = r.headers['location'] body = { '': { RDF + 'type': URI(BG + 'BlogService'), DC + 'title': 'Cloud Supplements Blogging Service', BG + 'blogs': [URI(blog_url)], CE + 'capability': URI(blog_capability_url) } } r = requests.post(cs_cat_app_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE BLOG SERVICE! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) blogservice_url = r.headers['location'] body = { '': { CE + 'site_id': CS_TENANT, CE + 'public': True, RDF + 'type': { 'type': 'uri', 'value': CE + 'Site' }, CE + 'site_home': URI(store_url), DC + 'title': 'Cloud Supplements Site', CE + 'improvements': [URI(store_url), URI(blogservice_url)] } } r = requests.post(sites_post_url, headers=POST_HEADERS, data=json.dumps(body, cls=RDF_JSON_Encoder), verify=False) if r.status_code != 201: print '######## FAILED TO CREATE SITE! ' + r.text return print '######## POSTed resource: %s, status: %d' % (r.headers['location'], r.status_code) site_url = r.headers['location'] site = RDF_JSON_Document(json.loads(r.text, object_hook=rdf_json_decoder), site_url) site_service_url = str(site.get_value(CE + 'services')) csv_importer = CSVImporter(cs_cat_categories_url, cs_cat_products_url, id_prefix) if csv_importer.import_products(csv_file_name): print 'Done.' else: print 'Data Import FAILED!'