示例#1
0
    def add_member_payment_record(self, ctx, amount_in_cents: int, title: str, member_username: str,
                                  payment_method: str) -> None:
        LOG.debug("sql_money_repository_add_payment_record",
                  extra=log_extra(ctx, amount=amount_in_cents / 100, title=title, username=member_username,
                                  payment_method=payment_method))
        now = datetime.now()
        s = ctx.get(CTX_SQL_SESSION)
        admin = ctx.get(CTX_ADMIN)

        admin_sql = s.query(Utilisateur).filter(Utilisateur.login == admin.login).one_or_none()
        if admin_sql is None:
            raise InvalidAdmin()

        member_sql = s.query(Adherent).filter(Adherent.login == member_username).one_or_none()
        if member_sql is None:
            raise MemberNotFoundError(member_username)

        payment_method_db = PAYMENT_METHOD_TO_DB.get(payment_method)
        if payment_method_db is None:
            raise UnknownPaymentMethod()

        e = Ecriture(
            intitule=title,
            montant=amount_in_cents / 100,
            moyen=payment_method,
            date=now,
            compte_id=1,  # Compte pour les paiement des adherents.
            created_at=now,
            updated_at=now,
            utilisateur=admin_sql,
            adherent=member_sql,
        )
        s.add(e)
示例#2
0
    def update_or_create(self, ctx, mac_address: str, req: MutationRequest):
        """
        Create/Update a device from the database.

        User story: As an admin, I can register a new device, so that a member can access internet with it.

        :return: True if the device was created, false otherwise.

        :raise MemberNotFound
        :raise IPAllocationFailedError
        :raise InvalidMACAddress
        :raise InvalidIPAddress
        """

        req.validate()

        # Make sure the provided owner username is valid.
        owner, _ = self.member_repository.search_member_by(ctx, username=req.owner_username)
        if not owner:
            raise MemberNotFoundError(req.owner_username)

        # Allocate IP address.
        if req.connection_type == DeviceType.Wired:
            ip_v4_range, ip_v6_range = self._get_ip_range_for_user(ctx, req.owner_username)

            # TODO: Free addresses if cannot allocate.
            if req.ip_v4_address is None and ip_v4_range:
                req.ip_v4_address = self.ip_allocator.allocate_ip_v4(ctx, ip_v4_range)

            if req.ip_v6_address is None and ip_v6_range:
                req.ip_v6_address = self.ip_allocator.allocate_ip_v6(ctx, ip_v6_range)

        fields = {k: v for k, v in asdict(req).items()}
        result, _ = self.device_repository.search_device_by(ctx, mac_address=mac_address)
        if not result:
            # No device with that MAC address, creating one...
            self.device_repository.create_device(ctx, **fields)

            LOG.info('device_create', extra=log_extra(
                ctx,
                username=req.owner_username,
                mac=mac_address,
                mutation=json.dumps(fields, sort_keys=True)
            ))
            return True

        else:
            # A device exists, updating it.

            # The following will never throw DeviceNotFound since we check beforehand.
            self.device_repository.update_device(ctx, mac_address, **fields)

            LOG.info('device_update', extra=log_extra(
                ctx,
                username=req.owner_username,
                mac=mac_address,
                mutation=json.dumps(fields, sort_keys=True)
            ))
            return False
示例#3
0
    def get_by_username(self, ctx, username) -> Member:
        """
        User story: As an admin, I can see the profile of a member, so that I can help her/him.

        :raise MemberNotFound
        """
        result, _ = self.member_repository.search_member_by(ctx,
                                                            username=username)
        if not result:
            raise MemberNotFoundError(username)

        # Log action.
        LOG.info('member_get_by_username',
                 extra=log_extra(ctx, username='******'))
        return result[0]
示例#4
0
    def create_membership(self, ctx, username, start: datetime,
                          end: datetime) -> None:
        """
        Add a membership record.

        :raise MemberNotFound
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_member_repository_add_membership_called",
                  extra=log_extra(ctx, username=username))

        member = _get_member_by_login(s, username)
        if member is None:
            raise MemberNotFoundError(username)

        s.add(Adhesion(adherent=member, depart=start, fin=end))
示例#5
0
    def update_member(self,
                      ctx,
                      member_to_update,
                      last_name=None,
                      first_name=None,
                      email=None,
                      username=None,
                      comment=None,
                      room_number=None,
                      departure_date: str = None,
                      association_mode: str = None,
                      password=None) -> None:
        """
        Update a member.

        :raise MemberNotFound
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_member_repository_update_member_called",
                  extra=log_extra(ctx, username=member_to_update))

        member = _get_member_by_login(s, member_to_update)
        if member is None:
            raise MemberNotFoundError(member_to_update)

        with track_modifications(ctx, s, member):
            member.nom = last_name or member.nom
            member.prenom = first_name or member.prenom
            member.mail = email or member.mail
            member.commentaires = comment or member.commentaires
            member.login = username or member.login

            if departure_date is not None:
                member.date_de_depart = _parse_date_or_none(departure_date)

            if association_mode is not None:
                member.mode_association = _parse_date_or_none(association_mode)

            if room_number is not None:
                member.chambre = s.query(Chambre).filter(
                    Chambre.numero == room_number).one()

            member.updated_at = datetime.now()

        member.password = password or member.password  # Will not be tracked.
示例#6
0
    def delete_member(self, ctx, username=None) -> None:
        """
        Delete a member.

        :raise MemberNotFound
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_member_repository_delete_member_called",
                  extra=log_extra(ctx, username=username))

        # Find the soon-to-be deleted user
        member = _get_member_by_login(s, username)
        if not member:
            raise MemberNotFoundError(username)

        with track_modifications(ctx, s, member):
            # Actually delete it
            s.delete(member)
示例#7
0
    def update_or_create(self, ctx, name: str, actif: bool, type: AccountType, creation_date: str):
        req.validate()
        owner, _ = self.member_repository.search_member_by(ctx, name=req.owner_username)
        '''
        Pour créer un compte, il faut un membre (même pour Soirée MiNET 2018)
        '''
        if not owner:
            raise MemberNotFoundError(req.owner_username)

        result, _ = self.account_repository_search_account_by(ctx, name=name)
        if not result:
            # No account with that name, creating one...
            self.account_repository.create_account(ctx, name=name, type=type, actif=actif, creation_date=creation_date)
            #TODO: LOG.info
            return True

        else:
            # An account exists, updating it
            # Warning: AccountNotFound
            self.account_repository.update_account(ctx, name=name, type=type, actif=actif, creation_date=creation_date)
            #TODO: LOG.info
            return False
示例#8
0
    def get_logs(self, ctx, username) -> List[str]:
        """
        User story: As an admin, I can retrieve the logs of a member, so I can help him troubleshoot their connection
        issues.

        :raise MemberNotFound
        """
        # Fetch all the devices of the member to put them in the request
        # all_devices = get_all_devices(s)
        # q = s.query(all_devices, Adherent.login.label("login"))
        # q = q.join(Adherent, Adherent.id == all_devices.columns.adherent_id)
        # q = q.filter(Adherent.login == username)
        # mac_tbl = list(map(lambda x: x.mac, q.all()))

        # Check that the user exists in the system.
        result, _ = self.member_repository.search_member_by(ctx,
                                                            username=username)
        if not result:
            raise MemberNotFoundError(username)

        # Do the actual log fetching.
        try:
            # TODO: Fetch all the devices and put them into this request.
            logs = self.logs_repository.get_logs(ctx, username, [])

            LOG.info('member_get_logs',
                     extra=log_extra(
                         ctx,
                         username=username,
                     ))

            return logs

        except LogFetchError:
            LOG.warning("log_fetch_failed",
                        extra=log_extra(ctx, username=username))
            return []  # We fail open here.