Example #1
0
def delete_wireless_device(ctx, s, mac_address):
    """ Delete a wireless device from the database """
    q = s.query(Portable).filter(Portable.mac == mac_address)
    dev = q.one()

    with track_modifications(ctx, s, dev):
        s.delete(dev)
Example #2
0
def delete_wired_device(ctx, s, mac_address):
    """ Delete a wired device from the databse """
    q = s.query(Ordinateur).filter(Ordinateur.mac == mac_address)
    dev = q.one()

    with track_modifications(ctx, s, dev):
        s.delete(dev)
Example #3
0
    def create_account(self,
                       ctx,
                       name=None,
                       actif=None,
                       type=None,
                       creation_date=None):
        """
        Create an account.

        :raise AccountTypeNotFound ?
        """

        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_account_repository_create_account_called",
                  extra=log_extra(ctx, name=name, type=type))

        now = datetime.now()

        account = SQLAccount(
            name=name,
            actif=actif,
            type=type,
            creation_date=now,
        )

        with track_modifications(ctx, s, account):
            s.add(account)

        return account
Example #4
0
    def create_product(self,
                       ctx,
                       name=None,
                       selling_price=None,
                       buying_price=None):
        """
        Create a product .

        :raise ProductTypeNotFound ?
        """

        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_product_repository_create_product_called",
                  extra=log_extra(ctx, name=name))

        product = SQLProduct(
            name=name,
            buying_price=buying_price,
            selling_price=selling_price,
        )

        with track_modifications(ctx, s, product):
            s.add(product)

        return product
Example #5
0
def create_wireless_device(ctx, mac_address, username, s):
    """ Create a wireless device in the database """
    dev = Portable(
        mac=mac_address,
        adherent=s.query(Adherent).filter(Adherent.login == username).one(),
    )

    with track_modifications(ctx, s, dev):
        s.add(dev)

    return dev
Example #6
0
    def create_transaction(self,
                           ctx,
                           src=None,
                           dst=None,
                           name=None,
                           value=None,
                           paymentMethod=None,
                           attachments=None):
        LOG.debug("sql_device_repository_create_transaction_called",
                  extra=log_extra(ctx, name=name))
        """
        Create a transaction.

        :raise AccountNotFound
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_transaction_repository_create_transaction_called",
                  extra=log_extra(ctx, name=name))

        now = datetime.now()

        account_src = None
        if src is not None:
            account_src = s.query(Account).filter(
                Account.id == src).one_or_none()
            if not account_src:
                raise AccountNotFoundError(src)

        account_dst = None
        if dst is not None:
            account_dst = s.query(Account).filter(
                Account.id == dst).one_or_none()
            if not account_dst:
                raise AccountNotFoundError(dst)

        method = None
        if paymentMethod is not None:
            method = s.query(PaymentMethod).filter(
                PaymentMethod.id == paymentMethod).one_or_none()
            if not method:
                raise PaymentMethodNotFoundError(paymentMethod)

        transaction = SQLTransaction(src_account=account_src,
                                     dst_account=account_dst,
                                     value=value,
                                     name=name,
                                     timestamp=now,
                                     attachments="",
                                     payment_method=method)

        with track_modifications(ctx, s, transaction):
            s.add(transaction)
        pass
Example #7
0
def create_wired_device(ctx, mac_address, ip_v4_address, ip_v6_address,
                        username, s):
    """ Create a wired device in the database """
    dev = Ordinateur(
        mac=mac_address,
        ip=ip_v4_address,
        ipv6=ip_v6_address,
        adherent=s.query(Adherent).filter(Adherent.login == username).one(),
    )

    with track_modifications(ctx, s, dev):
        s.add(dev)

    return dev
Example #8
0
    def create_member(self,
                      ctx,
                      last_name=None,
                      first_name=None,
                      email=None,
                      username=None,
                      comment=None,
                      room_number=None,
                      departure_date=None,
                      association_mode=None) -> None:
        """
        Create a member.

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

        now = datetime.now()

        room = None
        if room_number is not None:
            room = s.query(Chambre).filter(
                Chambre.numero == room_number).one_or_none()
            if not room:
                raise RoomNotFoundError(room_number)

        member = s.query(Adherent).filter(
            Adherent.login == username).one_or_none()
        if member is not None:
            raise MemberAlreadyExist()

        member = Adherent(
            nom=last_name,
            prenom=first_name,
            mail=email,
            login=username,
            chambre=room,
            created_at=now,
            updated_at=now,
            commentaires=comment,
            date_de_depart=_parse_date_or_none(departure_date),
            mode_association=_parse_date_or_none(association_mode),
        )

        with track_modifications(ctx, s, member):
            s.add(member)
Example #9
0
def update_wireless_device(ctx,
                           s,
                           device_to_update,
                           mac_address=None,
                           username=None):
    """ Update a wireless device in the database """
    q = s.query(Portable).filter(Portable.mac == device_to_update)
    dev = q.one()

    with track_modifications(ctx, s, dev):
        dev.mac = mac_address or dev.mac
        if username:
            dev.adherent = s.query(Adherent).filter(
                Adherent.login == username).one()

    return dev
Example #10
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.
Example #11
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)
Example #12
0
    def delete_product(self, ctx, name=None):
        """
        Delete a product.

        :raise ProductNotFound (one day)
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_product_repository_delete_product_called",
                  extra=log_extra(ctx, name=name))

        # Find the soon-to-be deleted user
        product = _get_product_by_name(s, name)
        if not product:
            raise ProductNotFoundError(name)

        with track_modifications(ctx, s, product):
            # Actually delete it
            s.delete(product)
Example #13
0
def update_wired_device(ctx,
                        s,
                        device_to_update,
                        mac_address=None,
                        username=None,
                        ip_v4_address=None,
                        ip_v6_address=None):
    """ Update a wired device in the database """
    q = s.query(Ordinateur).filter(Ordinateur.mac == device_to_update)
    dev = q.one()

    with track_modifications(ctx, s, dev):
        dev.ip = ip_v4_address or dev.ip
        dev.ipv6 = ip_v6_address or dev.ipv6
        dev.mac = mac_address or dev.mac
        if username:
            dev.adherent = s.query(Adherent).filter(
                Adherent.login == username).one()

    return dev
Example #14
0
    def update_product(self,
                       ctx,
                       name=None,
                       selling_price=None,
                       buying_price=None,
                       product_id=None) -> None:
        """
        Update a product.
        Will raise (one day) ProductNotFound
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_product_repository_update_product_called",
                  extra=log_extra(ctx, product_id=product_id))

        product = _get_product_by_id(s, product_id)
        if product is None:
            raise ProductNotFoundError(product_id)

        with track_modifications(ctx, s, product):
            product.name = name or product.name
            product.buying_price = buying_price or product.buying_price
            product.selling_price = selling_price or product.selling_price
Example #15
0
    def update_account(self,
                       ctx,
                       name=None,
                       type=None,
                       actif=None,
                       creation_date=None,
                       account_id=None) -> None:
        """
        Update an account.
        Will raise (one day) AccountNotFound
        """
        s = ctx.get(CTX_SQL_SESSION)
        LOG.debug("sql_account_repository_update_account_called",
                  extra=log_extra(ctx, account_id=account_id, actif=actif))

        account = _get_account_by_id(s, account_id)
        if account is None:
            raise AccountNotFoundError(account_id)

        with track_modifications(ctx, s, account):
            account.name = name or account.name
            account.type = type or account.type
            account.actif = actif
            account.creation_date = creation_date or account.creation_date