def post(self, *ar, **kw): accountId_id = ar[0] accountId_key = ndb.Key(models.AccountId, accountId_id) accountId_obj = accountId_key.get() if not accountId_obj: self.abort(404) accountBody_obj = None if accountId_obj.accountBody_key: accountBody_obj = accountId_obj.accountBody_key.get() if not accountBody_obj: self.abort(404) authToken = self.get_authToken(accountBody_obj.key) img_key = models.Image(title=self.request.POST['title'].encode('ascii', 'ignore'), full_size_image=self.request.POST['image'].file.read(), parent=accountBody_obj.key).put() img_id = urlhash.int_to_base62(img_key.integer_id()) self.response.headers.add_header('Access-Control-Allow-Origin', '*') self.response.headers.add_header('Location', '/accnt/' + urlhash.int_to_base62(accountBody_obj.key.integer_id()) + '/img/' + img_id) self.response.status = '201 Created'
def authenticate(cls, id, password): authToken = None accountId_key = ndb.Key(cls, id) accountId_obj = accountId_key.get() if accountId_obj: accountBody_obj = accountId_obj.accountBody_key.get() if accountBody_obj: accountBody_key = accountId_obj.accountBody_key accountBody_id = urlhash.int_to_base62( accountBody_key.integer_id()) if account_utils.valid_pw(accountBody_id, password, accountBody_obj.passwordHash): authToken = Session.create(accountBody_key=accountBody_key) message = 'Login successful.' else: message = 'Incorrect account id or password.' else: message = 'Account does not exist.' else: message = 'Account does not exist.' return authToken, message
def create(cls, id, name, password): authToken = None id_error = account_utils.check_id(id) name_error = account_utils.check_name(name) password_error = account_utils.check_password(password) if not (id_error and name_error and password_error): accountId_key = ndb.Key(cls, id) accountId_obj = accountId_key.get() if not accountId_obj: accountBody_key = AccountBody().put() accountBody_obj = accountBody_key.get() accountBody_obj.name = name accountBody_id = urlhash.int_to_base62( accountBody_key.integer_id()) accountBody_obj.passwordHash = account_utils.make_pw_hash( accountBody_id, password) accountBody_obj.put() cls(key=accountId_key, accountBody_key=accountBody_key).put() authToken = Session.create(accountBody_key=accountBody_key) return authToken
def post(self, *ar, **kw): business_id = ar[0] data = self.validate_body(schema1) business_model = self.get_model(models.Business, business_id) self.check_if_non_empty_string(data['product']['name']) self.check_if_zero_length( models.Product.fetch_by_name(data['product']['name'], business_model.key)) intents = [] for intent in data['product']['intents']: intents.append( models.Intent(action=intent.action, name=intent.name, value=intent.value)) if data['product']['primaryImage']: match_obj = self.match_regex(business_img_pattern, data['product']['primaryImage']) if business_id == match_obj.group(1): image_model = self.get_model(models.Image, match_obj.group(2), parent=business_model) primaryImage = image_model.key else: self.abort(400) else: primaryImage = None product_key = models.Product( parent=business_model.key, description=data['product']['description'], intents=intents, isPromo=data['product']['isPromo'], isVisible=data['product']['isVisible'], name=data['product']['name'], order=data['product']['order'], primaryImage=primaryImage).put() product_model_url = '/businesses/' + business_id + '/products/' + urlhash.int_to_base62( product_key.integer_id()) product_model = product_key.get() product_model.url = product_model_url product_model.put() self.set_response('201 Created', extra_headers=[['Location', product_model_url]])
def post(self, *ar, **kw): business_id = ar[0] business_model = self.get_model(models.Business, business_id) image_key = models.Image( title=self.request.POST['title'].encode('ascii', 'ignore'), full_size_image=self.request.POST['image'].file.read(), parent=business_model.key).put() image_model_url = '/businesses/' + business_id + '/images/' + urlhash.int_to_base62( image_key.integer_id()) image_model = image_key.get() image_model.url = image_model_url image_model.put() self.set_response('201 Created', extra_headers=[['Location', image_model_url]])
def get_model(self, model_class, model_id, parent=None, strict=True): model = None parent_key = None if parent: parent_key = parent.key try: model = model_class.get_by_id(id=urlhash.base62_to_int(model_id), parent=parent_key) except: self.abort(404) if strict and not model: message = model_class.__name__ + ' : ' + model_id if parent: message += '\nwith parent ' + parent.__class__.__name__ + ' : ' + urlhash.int_to_base62( parent_key.integer_id()) self.abort(404, detail=message) else: return model
def post(self, *ar, **kw): business_id = ar[0] data = self.validate_body(schema1) business_model = self.get_model(models.Business, business_id) self.check_if_non_empty_string(data['page']['title']) self.check_if_zero_length(models.Page.fetch_by_title(data['page']['title'], business_model.key)) if data['page']['primaryImage']: matchObj = self.match_regex(business_img_pattern, data['page']['primaryImage']) if business_id == matchObj.group(1): image_model = self.get_model(models.Image, matchObj.group(2), parent=business_model) primaryImage = image_model.key else: self.abort(400) else: primaryImage = None products = [] for product in data['page']['products']: matchObj = self.match_regex(product_pattern, product) if business_id == matchObj.group(1): product_model = self.get_model(models.Product, matchObj.group(2), parent=business_model) products.append(product_model.key) else: self.abort(400) page_key = models.Page( description=data['page']['description'], order=data['page']['order'], primaryImage=primaryImage, products=products, title=data['page']['title']).put() page_model_url = '/businesses/' + business_id + '/pages/' + urlhash.int_to_base62(page_key.integer_id()) page_model = page_key.get() page_model.url = page_model_url page_model.put() self.set_response('201 Created', extra_headers=[['Location', page_model_url]])
def post(self, *ar, **kw): data = self.validate_body(schema1) self.check_if_non_empty_string(data['category']['name']) self.check_if_zero_length( models.Category.fetch_by_name(data['category']['name'])) category_key = models.Category( description=data['category']['description'], name=data['category']['name'], pluralName=data['category']['pluralName']).put() category_model_url = '/categories/' + urlhash.int_to_base62( category_key.integer_id()) category_model = category_key.get() category_model.url = category_model_url category_model.put() self.set_response('201 Created', extra_headers=[['Location', category_model_url]])
def post(self, *ar, **kw): data = self.validate_body(schema1) self.check_if_non_empty_string(data['business']['registeredName']) self.check_if_zero_length( models.Business.fetch_by_registeredName( data['business']['registeredName'])) business_key = models.Business( adminUsers=data['business']['adminUsers'], description=data['business']['description'], name=data['business']['name'], registeredName=data['business']['registeredName']).put() business_model_url = '/businesses/' + urlhash.int_to_base62( business_key.integer_id()) business_model = business_key.get() business_model.url = business_model_url business_model.put() return [['Location', business_model_url]], None
def post(self, *ar, **kw): business_id = ar[0] data = self.validate_body(schema1) business_model = self.get_model(models.Business, business_id) self.check_if_non_empty_string(data['establishment']['name']) self.check_if_zero_length( models.Establishment.fetch_by_name(data['establishment']['name'], business_model.key)) categories = [] for category in data['establishment']['categories']: matchObj = self.match_regex(category_pattern, category) if matchObj.group(0): category_model = self.get_model(models.Category, matchObj.group(1)) categories.append(category_model.key) else: self.abort(400) if data['establishment']['primaryImage']: matchObj = self.match_regex(business_img_pattern, data['establishment']['primaryImage']) if business_id == matchObj.group(1): image_model = self.get_model(models.Image, matchObj.group(2), parent=business_model) primaryImage = image_model.key else: self.abort(400) else: primaryImage = None establishment_key = models.Establishment( parent=business_model.key, categories=categories, contact=models.Contact( formattedPhone=data['establishment']['contact'] ['formattedPhone'], phone=data['establishment']['contact']['phone']), location=models.Location( address=data['establishment']['location']['address'], cc=data['establishment']['location']['cc'], cityOrMunicipality=data['establishment']['location'] ['cityOrMunicipality'], country=CountryCodeCountryDict[data['establishment'] ['location']['cc']], position=ndb.GeoPt( data['establishment']['location']['position']['lat'], data['establishment']['location']['position']['lon']), slippyPosition=from_coords( lat=data['establishment']['location']['position']['lat'], lon=data['establishment']['location']['position']['lon'])), name=data['establishment']['name'], primaryImage=primaryImage).put() establishment_model_url = '/businesses/' + business_id + '/establishments/' + urlhash.int_to_base62( establishment_key.integer_id()) establishment_model = establishment_key.get() establishment_model.url = establishment_model_url establishment_model.put() self.set_response('201 Created', extra_headers=[['Location', establishment_model_url]])