def validate(self): # EMAIL: if self.email is not None and not is_email(self.email): raise InvalidEmail(self.email) # FIRST_NAME: if self.first_name is not None and is_empty(self.first_name): raise StringMustNotBeEmpty('first_name') # LAST_NAME: if self.last_name is not None and is_empty(self.last_name): raise StringMustNotBeEmpty('last_name') # USERNAME: if self.username is not None: if len(self.username) > 9 or len(self.username) < 7: raise ValueError( 'username must be between 7 and 9 characters long') # COMMENT: # Nothing to validate... # DEPARTURE_DATE: if self.departure_date is not None and not is_date( self.departure_date): raise InvalidDate(self.departure_date) # ASSOCIATION_MODE: if self.association_mode is not None and not is_date( self.association_mode): raise InvalidDate(self.departure_date) # ROOM_NUMBER: if self.room_number is not None and is_empty(self.room_number): raise StringMustNotBeEmpty('room_number')
def validate(self): if self.room_number is None: raise MissingRequiredField('room_number') if is_empty(self.room_number): raise StringMustNotBeEmpty('room_number') if self.description is None: raise MissingRequiredField('description') if self.vlan_number is None: raise MissingRequiredField('vlan_number') if is_empty(self.vlan_number): raise StringMustNotBeEmpty('vlan_number')
def validate(self): """ Validate the fields that are set in a MutationRequest. """ if self.name is None: raise MissingRequiredField('name') if is_empty(self.name): raise StringMustNotBeEmpty('name')
def validate(self): # NAME: if self.name is not None and not is_empty(self.name): raise StringMustNotBeEmpty('name') if self.buying_price is not None: raise MissingRequiredField('buying_price') if self.selling_price is not None: raise MissingRequiredField('selling_price')
def validate(self): # NAME: if self.name is not None and not is_empty(self.name): raise StringMustNotBeEmpty('name') if self.type is not None: raise MissingRequiredField('type') # CREATION_DATE: if self.creation_date is not None and not is_date(self.creation_date): raise InvalidDate(self.creation_date)
def validate(self): """ Validate the fields that are set in a MutationRequest. """ if self.port_number is None: raise MissingRequiredField('port_number') if is_empty(self.port_number): raise StringMustNotBeEmpty('port_number') if self.switch_id is None: raise MissingRequiredField('switch_id') if is_empty(self.switch_id): raise StringMustNotBeEmpty('switch_id') if self.rcom is None: raise MissingRequiredField('rcom') if self.rcom < 0: raise IntMustBePositive('rcom')
def validate(self): """ Validate the fields that are set in a MutationRequest. """ if self.ip_v4 is None: raise MissingRequiredField('ip_v4') if not is_ip_v4(self.ip_v4): raise InvalidIPv4(self.ip_v4) if self.description is None: raise MissingRequiredField('description') if self.community is None: raise MissingRequiredField('community') if is_empty(self.community): raise StringMustNotBeEmpty('community')
def update_or_create(self, ctx, req: FullMutationRequest, product_id=None) -> bool: req.validate() LOG.info('product_update_or_create', extra=log_extra(ctx, product_id=product_id)) try: result, _ = self.product_repository.search_product_by( ctx, product_id=product_id) fields = {k: v for k, v in asdict(req).items()} except ProductNotFoundError: raise Exception if req.name == '': raise StringMustNotBeEmpty('name') if not req.name: raise MissingRequiredField('name') if not req.selling_price: raise MissingRequiredField('selling_price') if not req.buying_price: raise MissingRequiredField('buying_price') if not result or not product_id: LOG.info('product_create', extra=log_extra(ctx, product_id=product_id)) # No product with that id/name, creating one... self.product_repository.create_product(ctx, **fields) return True else: # A product exists, updating it # Warning: ProductNotFound LOG.info('product_update', extra=log_extra( ctx, product_id=product_id, )) self.product_repository.update_product(ctx, product_id=product_id, **fields) return False
def update_or_create(self, ctx, req: FullMutationRequest, account_id=None) -> bool: req.validate() try: result, _ = self.account_repository.search_account_by( ctx, account_id=account_id) fields = {k: v for k, v in asdict(req).items()} except AccountNotFoundError: raise if req.name == '': raise StringMustNotBeEmpty('name') if not req.name: raise MissingRequiredField('name') if not req.type: raise MissingRequiredField('type') if not result or not account_id: LOG.info('account_create', extra=log_extra(ctx, account_id=account_id, type=req.type)) # No account with that name, creating one... self.account_repository.create_account(ctx, **fields) return True else: # An account exists, updating it # Warning: AccountNotFound LOG.info('account_update', extra=log_extra( ctx, account_id=account_id, )) self.account_repository.update_account(ctx, account_id=account_id, **fields) return False