def handle_put(self, request, user, *args, **kwargs): """Treat requests PUT to edit Script Type. URL: scripttype/<id_script_type>/ """ try: self.log.info("Edit Script Type") # User permission if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_script_type = kwargs.get('id_script_type') # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error(3, u'There is no value to the networkapi tag of XML request.') script_type_map = networkapi_map.get('script_type') if script_type_map is None: return self.response_error(3, u'There is no value to the script_type tag of XML request.') # Get XML data type = script_type_map.get('type') description = script_type_map.get('description') # Valid ID Script Type if not is_valid_int_greater_zero_param(id_script_type): self.log.error( u'The id_script_type parameter is not a valid value: %s.', id_script_type) raise InvalidValueError(None, 'id_script_type', id_script_type) # Valid type if not is_valid_string_minsize(type, 3) or not is_valid_string_maxsize(type, 40): self.log.error(u'Parameter type is invalid. Value: %s', type) raise InvalidValueError(None, 'type', type) # Valid description if not is_valid_string_minsize(description, 3) or not is_valid_string_maxsize(description, 100): self.log.error( u'Parameter description is invalid. Value: %s', description) raise InvalidValueError(None, 'description', description) # Find Script Type by ID to check if it exist script_type = TipoRoteiro.get_by_pk(id_script_type) with distributedlock(LOCK_SCRIPT_TYPE % id_script_type): try: if script_type.tipo.lower() != type.lower(): TipoRoteiro.get_by_name(type) raise TipoRoteiroNameDuplicatedError( None, u'Já existe um tipo de roteiro com o tipo %s.' % type) except TipoRoteiroNotFoundError: pass # set variables script_type.tipo = type script_type.descricao = description try: # update Script Type script_type.save() except Exception, e: self.log.error(u'Failed to update the Script Type.') raise RoteiroError(e, u'Failed to update the Script Type.') return self.response(dumps_networkapi({})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)
def handle_put(self, request, user, *args, **kwargs): """Treat requests PUT to edit Script Type. URL: scripttype/<id_script_type>/ """ try: self.log.info('Edit Script Type') # User permission if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_script_type = kwargs.get('id_script_type') # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error(3, u'There is no value to the networkapi tag of XML request.') script_type_map = networkapi_map.get('script_type') if script_type_map is None: return self.response_error(3, u'There is no value to the script_type tag of XML request.') # Get XML data type = script_type_map.get('type') description = script_type_map.get('description') # Valid ID Script Type if not is_valid_int_greater_zero_param(id_script_type): self.log.error( u'The id_script_type parameter is not a valid value: %s.', id_script_type) raise InvalidValueError(None, 'id_script_type', id_script_type) # Valid type if not is_valid_string_minsize(type, 3) or not is_valid_string_maxsize(type, 40): self.log.error(u'Parameter type is invalid. Value: %s', type) raise InvalidValueError(None, 'type', type) # Valid description if not is_valid_string_minsize(description, 3) or not is_valid_string_maxsize(description, 100): self.log.error( u'Parameter description is invalid. Value: %s', description) raise InvalidValueError(None, 'description', description) # Find Script Type by ID to check if it exist script_type = TipoRoteiro.get_by_pk(id_script_type) with distributedlock(LOCK_SCRIPT_TYPE % id_script_type): try: if script_type.tipo.lower() != type.lower(): TipoRoteiro.get_by_name(type) raise TipoRoteiroNameDuplicatedError( None, u'Já existe um tipo de roteiro com o tipo %s.' % type) except TipoRoteiroNotFoundError: pass # set variables script_type.tipo = type script_type.descricao = description try: # update Script Type script_type.save() except Exception, e: self.log.error(u'Failed to update the Script Type.') raise RoteiroError(e, u'Failed to update the Script Type.') return self.response(dumps_networkapi({})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)
def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add Script Type. URL: scripttype/ """ try: self.log.info('Add Script Type') # User permission if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'There is no value to the networkapi tag of XML request.' ) script_type_map = networkapi_map.get('script_type') if script_type_map is None: return self.response_error( 3, u'There is no value to the script_type tag of XML request.' ) # Get XML data type = script_type_map.get('type') description = script_type_map.get('description') # Valid type if not is_valid_string_minsize( type, 3) or not is_valid_string_maxsize(type, 40): self.log.error(u'Parameter type is invalid. Value: %s', type) raise InvalidValueError(None, 'type', type) # Valid description if not is_valid_string_minsize(description, 3) or not is_valid_string_maxsize( description, 100): self.log.error(u'Parameter description is invalid. Value: %s', description) raise InvalidValueError(None, 'description', description) try: TipoRoteiro.get_by_name(type) raise TipoRoteiroNameDuplicatedError( None, u'Já existe um tipo de roteiro com o tipo %s.' % type) except TipoRoteiroNotFoundError: pass script_type = TipoRoteiro() # set variables script_type.tipo = type script_type.descricao = description try: # save Script Type script_type.save() except Exception, e: self.log.error(u'Failed to save the Script Type.') raise RoteiroError(e, u'Failed to save the Script Type.') script_map = dict() script_map['script_type'] = model_to_dict( script_type, exclude=['tipo', 'descricao']) return self.response(dumps_networkapi(script_map))
def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add Script Type. URL: scripttype/ """ try: self.log.info("Add Script Type") # User permission if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error(3, u'There is no value to the networkapi tag of XML request.') script_type_map = networkapi_map.get('script_type') if script_type_map is None: return self.response_error(3, u'There is no value to the script_type tag of XML request.') # Get XML data type = script_type_map.get('type') description = script_type_map.get('description') # Valid type if not is_valid_string_minsize(type, 3) or not is_valid_string_maxsize(type, 40): self.log.error(u'Parameter type is invalid. Value: %s', type) raise InvalidValueError(None, 'type', type) # Valid description if not is_valid_string_minsize(description, 3) or not is_valid_string_maxsize(description, 100): self.log.error( u'Parameter description is invalid. Value: %s', description) raise InvalidValueError(None, 'description', description) try: TipoRoteiro.get_by_name(type) raise TipoRoteiroNameDuplicatedError( None, u'Já existe um tipo de roteiro com o tipo %s.' % type) except TipoRoteiroNotFoundError: pass script_type = TipoRoteiro() # set variables script_type.tipo = type script_type.descricao = description try: # save Script Type script_type.save(user) except Exception, e: self.log.error(u'Failed to save the Script Type.') raise RoteiroError(e, u'Failed to save the Script Type.') script_map = dict() script_map['script_type'] = model_to_dict( script_type, exclude=["tipo", "descricao"]) return self.response(dumps_networkapi(script_map))