def handle_get_options(self, state, field): (request, response, session) = state.unfold() if not self.permission_checker.has_permission(state, self.prefix + ".edit"): raise HttpUnauthorizedException with closing(self.database_session_maker()) as database_session: if (field not in self.fields or self.types[field] != 'relation' or not self.large_number_of_options[field] ) and not self.custom_options.get(field): raise HttpNotFoundException() search = request.query.get("search") if request.query.get("id"): instance = database_session.query(self.model).get( int(request.query["id"])) if not instance: raise HttpNotFoundException() else: instance = None options = self._get_relation_options(field, database_session, instance, search, limit=40) response.setJsonBody(json.dumps(options)) return state
def _findroute(self, state): """Internal function to find the first matching route and execute that route. """ # Find first matching route for (forwarding, compiled_regexp, appfunc, methods) in self._routes: # See if the path info matches the reqular expression match = compiled_regexp.match(state.request.path_info) if match and state.request.method in methods: # See if we have to forward or not if forwarding: # Set the path_info and script_name right: This is such that apps forwarded to # have the idea they are on root. state = self._changeRequestForForwarding( state, match.group(0)) # Forward request to the app return appfunc(state) else: # Map request to function and pass variables that matched return appfunc(state, *(match.groups())) # No match found raise a HTTP Not Found Exception raise HttpNotFoundException()
def handle_get(self, state): (request, response, session) = state.unfold() if not self.permission_checker.has_permission(state, self.prefix + ".edit"): raise HttpUnauthorizedException id_str = request.query.get("id") try: id = int(id_str) except ValueError: id = None if not id: raise HttpBadRequestException() with closing(self.database_session_maker()) as database_session: instance = database_session.query(self.model).get(id) if not instance: raise HttpNotFoundException() data = self._get_data(instance, database_session) response.setJsonBody(json.dumps(data)) return state
def handle(self, state): """Handles a request to a static file. Serves the file """ (request, response, session) = state.unfold() try: # Try to open the file in binary mode file = open(self._folder + request.path_info, 'rb') # Try to guess the content type response.content_type = mimetypes.guess_type(self._folder + request.path_info)[0] # Set a default content type if no guess was found if not response.content_type: response.content_type = "application/octet-stream" # We write binary data so return no charset response.charset = None # Read the contents of the file to the body of the response response.body = file.read() except: raise HttpNotFoundException() else: file.close() return state
def _findroute(self, state): """Internal function to find the first matching route and execute that route. """ # Find first matching route for (wsgi, forwarding, compiled_regexp, appfunc, methods) in self._routes: # See if the path info matches the reqular expression match = compiled_regexp.match(state.request.path_info) # See if a wsgi handler was added for this match or not if match and wsgi: state.response_handled_externally = True state.environment["state"] = state state.response.body = appfunc(state.environment, state.start_response_wrapper) return state elif match and state.request.method in methods: # See if we have to forward or not if forwarding: # Set the path_info and script_name right: This is such that apps forwarded to # have the idea they are on root. state = self._changeRequestForForwarding(state, match.group(0)) # Forward request to the app return appfunc(state) else: # Map request to function and pass variables that matched return appfunc(state, *(match.groups())) # No match found raise a HTTP Not Found Exception raise HttpNotFoundException()
def handle_get_form_definition(self, state): (request, response, session) = state.unfold() if not self.permission_checker.has_permission(state, self.prefix + ".edit"): raise HttpUnauthorizedException with closing(self.database_session_maker()) as database_session: if request.query.get("id"): instance = database_session.query(self.model).get( int(request.query["id"])) if not instance: raise HttpNotFoundException() else: instance = None definition = {} definition["fields"] = [] for name in self.fields: field_dict = {} field_dict["name"] = name field_dict["type"] = self.types[name] if callable(self.read_only[name]): field_dict["read_only"] = self.read_only[name](instance) else: field_dict["read_only"] = self.read_only[name] field_dict["description"] = self.descriptions[name] if self.default_value.get(name): if callable(self.default_value[name]): field_dict["default_value"] = self.default_value[name]( database_session, instance) else: field_dict["default_value"] = self.default_value[name] if self.types[name] == self.TYPE_RELATION: field_dict["large"] = self.large_number_of_options[name] if not self.large_number_of_options[name]: field_dict["options"] = self._get_relation_options( name, database_session, instance) else: field_dict[ "options"] = self._get_relation_options_from_instance( name, database_session, instance) field_dict["multiple"] = self.relation_many[name] if self.types[name] == self.TYPE_CUSTOM and self.custom_def.get( name): field_dict["data"] = self.custom_def[name]( self, database_session, instance) elif self.types[name] == SqlTypeConverter.TYPE_ENUM: field_dict["options"] = self._get_enum_options(name) definition["fields"].append(field_dict) response.setJsonBody(json.dumps(definition)) return state
def handle_submit(self, state): (request, response, session) = state.unfold() if not self.permission_checker.has_permission(state, self.prefix + ".edit"): raise HttpUnauthorizedException with closing(self.database_session_maker( autoflush=False)) as database_session: if request.body.get("id"): instance = database_session.query(self.model).get( int(request.body["id"])) if not instance: raise HttpNotFoundException() else: instance = self.model() result = dict() result["errors"] = dict() has_errors = False for field in self.fields: # Do not change read_only fields if callable(self.read_only[field]): if self.read_only[field](instance): continue elif self.read_only[field]: continue raw_data = request.body["data"].get(field, "") if self.types[field] == GenericFormController.TYPE_CUSTOM: errors = self.custom_submit[field](self, database_session, instance, raw_data) else: data = self._parse_field(field, raw_data, instance, database_session) errors = self._validate_field(field, data, instance, database_session) if not errors: if self.types[field] == GenericFormController.TYPE_CUSTOM: # No need to set attribute here, the custom_submit function should take care of any state changes pass else: setattr(instance, field, data) else: result["errors"][field] = [error for error in errors] has_errors = True (state, instance, result, has_errors) = self.after_submit(state, instance, result, has_errors, database_session) result["has_errors"] = has_errors if not has_errors: database_session.add(instance) database_session.commit() (instance, result, has_errors) = self.after_commit(instance, result, has_errors, database_session) result["data"] = self._get_data(instance, database_session) result["id"] = instance.id response.setJsonBody(json.dumps(result)) return state