def build_and_sign(self, params, state=None, redirect_uri=None, cancel_uri=None): """Builds a url and returns it as a string :param params: A Params class corresponding to the resource for which you wish to create a url. For example, to create a Subscription url pass an instance of SubscriptionParams. :param state: The state argument to be encoded in the query string. :param redirect_uri: The redirect uri the user will be sent to after the resource has been created. :param cancel_uri: The uri the user will be redirected to if they cancel the resource creation """ param_dict = {} resource_name = utils.singularize(params.resource_name) param_dict[resource_name] = params.to_dict().copy() if state: param_dict["state"] = state if redirect_uri: param_dict["redirect_uri"] = redirect_uri if cancel_uri: param_dict["cancel_uri"] = cancel_uri param_dict["client_id"] = self.client._app_id iso_time = datetime.datetime.utcnow().isoformat() param_dict["timestamp"] = iso_time[:-7] + "Z" param_dict["nonce"] = base64.b64encode(os.urandom(40)) signature = utils.generate_signature(param_dict, self.client._app_secret) param_dict["signature"] = signature url = "{0}/connect/{1}/new?{2}".format( self.client.get_base_url(), params.resource_name, utils.to_query(param_dict), ) return url
def post(self): # check the setup errors = self.check_setup() if errors: self.write(json.dumps(errors, indent=4)) return # load in the filters body = self.decode_and_load(self.request.body) response = {} # get the singular version of the model name singular_model_name = singularize(self.ember_model_name) # grab the new record data record_data = body[singular_model_name] with closing(self.Session()) as session: # perform any specified checks errors = self.perform_checks(session, record_data) if errors: # report any errors response['errors'] = errors else: # create a new record using the data new_record = self.table(**record_data) session.add(new_record) session.commit() logging.info( 'added {} {} to db'.format( singular_model_name, record_data)) self.set_status(201) # created self.write(json.dumps(response, indent=4))
def _get_klass_from_name(self, name): module = sys.modules[self.__module__] klass = getattr(module, utils.singularize(utils.camelize(name))) return klass