def apply(self, obj, req, resp, **kwargs): cls = self.model_class all = cls.filter(cls.type == 'switch pay per minute').all() data = {} for i in all: i_as_dict = PackageSwitchMinuteScheme().dump(i).data if i.minute_count is None: continue if i.sub_type is None: continue if i.minute_count in data: data[i.minute_count][i.sub_type] = i_as_dict else: data[i.minute_count] = { 'minute_count': i.minute_count, i.sub_type: i_as_dict } items = list(data.values()) items = sorted(items, key=lambda i: i['minute_count'] if 'minute_count' in i and i['minute_count'] else 0) # for k,v in data.items(): # items.append(v) self.set_response( resp, responses.SuccessResponseObjectInfo(data={'items': items})) return False
def on_patch(self, req, resp, **kwargs): self.init_req(req) kwargs = user_context(self, req, resp, **kwargs) if not self.has_modify_operation: # pragma: no cover return self.method_not_allowed_response(resp, 'PATCH') if not check_permission( self, req, 'modify', self.get_object(resp, self.model_class, **kwargs)): self.set_response( resp, responses.ForbiddenErrorResponse( data=errors.AuthErrors.Forbidden)) return try: scheme = self.scheme_class_modify( ) if self.scheme_class_modify else self.scheme_class() if self.check_request_data(req, resp, scheme): if self.update_object( req, resp, self.model_class, self.scheme_class_modify if self.scheme_class_modify else self.scheme_class, **kwargs): data = self.get_object_data(resp, self.model_class, self.scheme_class_get, **kwargs) if data: self.set_response( resp, responses.SuccessResponseObjectInfo(data=data)) else: self.set_response(resp, responses.SuccessResponseJustOk()) except IntegrityError as e: try: msg = str(e).split('\n')[1].split(':')[1] except: msg = str(e) self.set_response(resp, AlreadyExistsResponse(self.entity, msg)) except FkConstraintViolation as e: self.set_response( resp, responses.FkConstraintViolationResponse( data=errors.CommonErrors.get_fk_violation_error( e.table_name, e.column_name, e.value))) except ValidationError as e: self.set_response( resp, responses.ValidationErrorResponse(data=e.messages)) except NoResultFound: self.set_response(resp, responses.ObjectNotFoundErrorResponse()) except Exception as e: log.error(e) #self.set_response(resp, responses.OperationErrorResponse(data=errors.CommonErrors.DataError)) self.set_response(resp, OperationalError(e))
def delete_object(self, req, resp, model_class, **kwargs): try: query = self.get_query_filter(req, resp, model_class, **kwargs) result = query.delete(synchronize_session='fetch') model_class.session().commit() self.set_response( resp, responses.SuccessResponseObjectInfo(data={'deleted': result})) except Exception as e: model_class.query().session.rollback() log.error('Mass delete error:{}'.format(str(e))) return False
def apply(self, obj, req, resp, **kwargs): lic = self.model_class.get(kwargs['license_switch_uuid']) if not lic: raise ValidationError({'license_switch_uuid': ['license_switch_uuid not found']}) obj.renew() obj.save() self.scheme_class.get_object_created_response() data = self.get_object_data(resp, self.model_class, self.scheme_class, **kwargs) if data: self.set_response(resp, responses.SuccessResponseObjectInfo(data=data)) return False return True
def get_spec_delete(self): return swagger.specify.get_spec( method='delete', description='Deletes multiple found {}'.format( self.entity.lower()), path_parameters=self.get_path_parameters(), query_parameters=List.get_all_fields_from_scheme( self.scheme_class)[1], responses=(responses.SuccessResponseObjectInfo( payload_scheme=self.scheme_class_get), responses.ObjectNotFoundErrorResponse()) + self.get_additional_responses(method='delete'), security=self.get_security(method='delete', action='delete'))
class PackageSwitchMinuteTableResource(CustomGetAction): model_class = model.PackageSwitch security = (DEFAULT_SECURITY) restrict = () additional_responses = (responses.SuccessResponseObjectInfo( payload_scheme=PackageSwitchMinuteTableScheme), ) no_auth_needed = True def on_get(self, req, resp, **kwargs): self.init_req(req) return self.proceed(req, resp, **kwargs) def apply(self, obj, req, resp, **kwargs): cls = self.model_class all = cls.filter(cls.type == 'switch pay per minute').all() data = {} for i in all: i_as_dict = PackageSwitchMinuteScheme().dump(i).data if i.minute_count is None: continue if i.sub_type is None: continue if i.minute_count in data: data[i.minute_count][i.sub_type] = i_as_dict else: data[i.minute_count] = { 'minute_count': i.minute_count, i.sub_type: i_as_dict } items = list(data.values()) items = sorted(items, key=lambda i: i['minute_count'] if 'minute_count' in i and i['minute_count'] else 0) # for k,v in data.items(): # items.append(v) self.set_response( resp, responses.SuccessResponseObjectInfo(data={'items': items})) return False
def update_object(self, req, resp, model_class, scheme_class, **kwargs): self.init_req(req) if hasattr(self.model_class, 'update_by'): self.req_data['update_by'] = self.get_user().name if hasattr(self.model_class, 'update_on'): self.req_data['update_by'] = datetime.now(UTC) if hasattr(self.model_class, 'update_at'): self.req_data['update_at'] = datetime.now(UTC) scheme = scheme_class().load(self.req_data) if scheme.errors: self.set_response( resp, responses.ValidationErrorResponse(data=scheme.errors)) return False #result = self.model_class.query().update(self.req_data) query = self.get_query_filter(req, resp, model_class, **kwargs) result = query.update(self.req_data, synchronize_session='fetch') self.result = result self.model_class.session().commit() self.set_response( resp, responses.SuccessResponseObjectInfo(data={'updated': result})) return False