def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None) -> (List[Room], int): """ Search a room in the database. User story: As an admin, I can search rooms, so that see the room information. :raise IntMustBePositiveException """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.room_repository.search_room_by(ctx, limit=limit, offset=offset, terms=terms) LOG.info('room_search', extra=log_extra( ctx, terms=terms, )) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, room_number=None, terms=None) -> (List[Member], int): """ search member in the database. user story: as an admin, i want to have a list of members with some filters, so that i can browse and find members. :raise intmustbepositiveexception """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.member_repository.search_member_by( ctx, limit=limit, offset=offset, room_number=room_number, terms=terms) # Log action. LOG.info('member_search', extra=log_extra( ctx, room_number=room_number, terms=terms, )) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, product_id=None, terms=None) -> (List[Product], int): """ search product in the database. user story: as an admin, i want to have a list of products with some filters, so that i can browse and find products. :raise intmustbepositiveexception """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.product_repository.search_product_by( ctx, limit=limit, offset=offset, product_id=product_id, terms=terms) # Log action. LOG.info('product_search', extra=log_extra( ctx, product_id=product_id, terms=terms, )) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, account_type_id=None, terms=None) -> (List[AccountType], int): """ search account_type in the database. A une utilité ?? :raise IntMustBePositiveException """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.account_type_repository.search_account_type_by( ctx, account_type_id=account_type_id, terms=terms) # Log action. LOG.info('account_type_search', extra=log_extra(ctx, account_type_id=account_type_id, terms=terms)) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, payment_method_id=None, terms=None) -> (List[PaymentMethod], int): """ Search payment methods in the database. """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.payment_method_repository.search_payment_method_by( ctx, limit=limit, offset=offset, payment_method_id=payment_method_id, terms=terms) LOG.info("payment_method_search", extra=log_extra(ctx, payment_method_id=payment_method_id, terms=terms)) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, account_id=None, terms=None) -> (List[Transaction], int): """ search transactions in the database. :raise IntMustBePositiveException """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.transaction_repository.search_transaction_by( ctx, limit=limit, offset=offset, account_id=account_id, terms=terms) # Log action. LOG.info('transaction_search', extra=log_extra( ctx, account_id=account_id, terms=terms, )) return result, count
def get_by_name(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, name=None, terms=None) -> (List[Account], int): """ Search an account in the database. """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('limit') result, count = self.account_repository.search_account_by(ctx, limit=limit, offset=offset, name=name, terms=terms) #TODO: LOG.info return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None) -> (List[Switch], int): """ Search switches in the database. User story: As an admin, I can search switches in the database, so I find the IP associated with a switch. :raise IntMustBePositiveException """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.switch_repository.search_switches_by( ctx, limit=limit, offset=offset, terms=terms) LOG.info("switch_search", extra=log_extra(ctx, terms=terms)) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, port_id=None, switch_id=None, room_number=None, terms=None) -> (List[Port], int): """ Search ports in the database. User story: As an admin, I can search ports in the database, so I get all the ports from a room. :return: The list of the ports and the number of matches in the entire database. :raise IntMustBePositiveException """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.port_repository.search_port_by( ctx, limit=limit, offset=offset, port_id=port_id, switch_id=switch_id, room_number=room_number, terms=terms) LOG.info("port_search", extra=log_extra(ctx, port_id=port_id, switch_id=switch_id, room_number=room_number, terms=terms)) return result, count
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, username=None, terms=None) -> (List[Device], int): """ Search a device in the database. User story: As an admin, I can search all the devices, so I can see the device list of a member. :raise IntMustBePositiveException """ if limit < 0: raise IntMustBePositive('limit') if offset < 0: raise IntMustBePositive('offset') result, count = self.device_repository.search_device_by(ctx, limit=limit, offset=offset, username=username, terms=terms) LOG.info("device_search", extra=log_extra( ctx, limit=limit, terms=terms, )) return result, count
def update_or_create(self, ctx, mutation_request: FullMutationRequest) -> bool: """ Create/Update a transaction from the database. :return: True if the transaction was created, false otherwise. :raise IntMustBePositiveException :raise AccountNotFound :raise InvalidAdmin :raise PaymentMethodNotFound :raise MissingRequiredFieldError """ # Make sure all the field objects set are valid. mutation_request.validate() if mutation_request.value < 0: raise IntMustBePositive('value') if mutation_request.src == mutation_request.dst: raise UserInputError( 'source and destination accounts must not be the same') # Build a dict that will be transformed into a transaction. If a field is not set, consider that it should be # None. fields = asdict(mutation_request) fields = {k: v for k, v in fields.items()} try: self.transaction_repository.create_transaction(ctx, **fields) except Exception: raise # Log action LOG.info('transaction_create', extra=log_extra(ctx, mutation=json.dumps(fields, sort_keys=True, default=str))) return True
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 new_membership(self, ctx, username, duration, payment_method, start_str=None) -> None: """ Core use case of ADH. Registers a membership. User story: As an admin, I can create a new membership record, so that a member can have internet access. :param payment_method: :param ctx: context :param username: username :param duration: duration of the membership in days :param start_str: optional start date of the membership :raise IntMustBePositiveException :raise NoPriceAssignedToThatDurationException :raise MemberNotFound :raise InvalidAdmin :raise UnknownPaymentMethod """ if start_str is None: return self.new_membership( ctx, username, duration, payment_method, start_str=datetime.datetime.now().isoformat()) if duration < 0: raise IntMustBePositive('duration') if duration not in self.config.PRICES: LOG.warning("create_membership_record_no_price_defined", extra=log_extra(ctx, duration=duration)) raise NoPriceAssignedToThatDuration(duration) start = string_to_date(start_str) end = start + datetime.timedelta(days=duration) # TODO check price. try: price = self.config.PRICES[duration] # Expresed in EUR. price_in_cents = price * 100 # Expressed in cents of EUR. duration_str = self.config.DURATION_STRING.get(duration, '') title = f'Internet - {duration_str}' self.money_repository.add_member_payment_record( ctx, price_in_cents, title, username, payment_method) self.membership_repository.create_membership( ctx, username, start, end) self.member_repository.update_member( ctx, username, departure_date=end.isoformat()) except InvalidAdmin: LOG.warning("create_membership_record_admin_not_found", extra=log_extra(ctx)) raise except UnknownPaymentMethod: LOG.warning("create_membership_record_unknown_payment_method", extra=log_extra(ctx, payment_method=payment_method)) raise LOG.info("create_membership_record", extra=log_extra(ctx, username=username, duration_in_days=duration, start_date=start.isoformat()))