Exemplo n.º 1
0
class AccountNotificationSaw(SelenolService):
    """Mark a notification as saw."""
    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountNotificationSaw,
              self).__init__(['account', 'notification', 'saw'], connection,
                             session)

    @selenol_params(
        user=get_user_from_session(),
        notification=get_object_from_content(AccountNotification,
                                             ['notification_id']),
    )
    def on_request(self, user, notification):
        """Request method.

        :param user: User that is executing the request.
        :param notification: Notification to be mark as saw.
        """
        if notification.user_id != user.id:
            raise SelenolUnauthorizedException()

        notification.saw_at = datetime.now()
        self.session.commit()

        return notification_serializer(notification)
class AccountGroupAcceptPendingRequest(SelenolService):
    """Accepts a group pending request."""
    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountGroupAcceptPendingRequest,
              self).__init__(['account', 'group', 'accept_pending_request'],
                             connection, session)

    @selenol_params(user=get_user_from_session(),
                    membership_request=get_object_from_content(
                        AccountMembershipRequest, ['membership_request_id']))
    def on_request(self, user, membership_request):
        """Request method.

        :param message: Original message.
        :param user: User that is executing the request.
        :param user_content: User to be accepted to the group.
        :param group: Group where the user was requesting membership.
        """
        if membership_request.group.creator_id != user.id:
            raise SelenolUnauthorizedException()

        if membership_request.replied_at:
            raise SelenolInvalidArgumentException("membership_request_id",
                                                  membership_request.id)

        membership_request.replied_at = datetime.now()
        self.session.commit()

        return group_serializer(membership_request.group)
class AccountGroupWaitingRequests(SelenolService):
    """Returns all the waiting requests for a group."""

    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountGroupWaitingRequests, self).__init__(
            ['account', 'group', 'waiting_requests'], connection, session)

    @selenol_params(
        user=get_user_from_session(),
        user_content=get_object_from_content(AccountUser, ['user_id']),
    )
    def on_request(self, user, user_content):
        """Request method.

        :param user: User that is executing the request.
        :param user_content: User to be accepted to the group.
        """
        if user.id != user_content.id:
            raise SelenolUnauthorizedException()

        return [group_membership_request_serializer(unreplied_request)
                for unreplied_request in
                user_content.membership_requests.filter(
                    AccountMembershipRequest.replied_at.is_(None))]
Exemplo n.º 4
0
class AccountGroupUser(SelenolService):
    """Returns all the users in a given group."""
    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountGroupUser, self).__init__(['account', 'group', 'user'],
                                               connection, session)

    @selenol_params(
        user=get_user_from_session(),
        user_content=get_object_from_content(AccountUser, ['user_id']),
    )
    def on_request(self, user, user_content):
        """Request method.

        :param user: User that is executing the request.
        :param user_content: User to be accepted to the group.
        """
        if user.id != user_content.id:
            raise SelenolUnauthorizedException()

        return user_groups_serializer(user)
Exemplo n.º 5
0
class AccountGroupCreate(SelenolService):
    """Creates groups."""
    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountGroupCreate,
              self).__init__(['account', 'group', 'create'], connection,
                             session)

    @selenol_params(user=get_user_from_session(),
                    user_content=get_object_from_content(
                        AccountUser, ['user_id']),
                    group_name=get_value_from_content(['name']))
    def on_request(self, user, user_content, group_name):
        """Request method.

        :param user: User that is executing the request.
        :param user_content: User to be accepted to the group.
        :param group_name: Name of the new group.
        """
        if user.id != user_content.id:
            raise SelenolUnauthorizedException()

        if len(group_name) <= 0:
            raise SelenolInvalidArgumentException('name', group_name)

        if self.session.query(AccountGroup).filter(
                AccountGroup.name == group_name).one_or_none():
            raise SelenolInvalidArgumentException('name', group_name)

        group = AccountGroup(name=group_name,
                             created=datetime.now(),
                             creator_id=user_content.id)
        self.session.add(group)
        self.session.commit()

        return group_serializer(group)
Exemplo n.º 6
0
class AccountGroupRequest(SelenolService):
    """Creates a group membership request for a user."""
    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountGroupRequest,
              self).__init__(['account', 'group', 'request'], connection,
                             session)

    @selenol_params(user=get_user_from_session(),
                    user_content=get_object_from_content(
                        AccountUser, ['user_id']),
                    group=get_object_from_content(AccountGroup, ['group_id']))
    def on_request(self, user, user_content, group):
        """Request method.

        :param user: User that is executing the request.
        :param user_content: User to be accepted to the group.
        :param group: Group where the user was requesting membership.
        """
        if user.id != user_content.id:
            raise SelenolUnauthorizedException()

        if group.creator_id == user.id or group.users.filter(
                AccountUser.id == user.id).count():
            raise SelenolInvalidArgumentException('group_id', group.id)

        membership = AccountMembershipRequest(user_id=user_content.id,
                                              group_id=group.id,
                                              requested_at=datetime.now())
        self.session.add(membership)
        self.session.commit()

        return group_membership_request_serializer(membership)
Exemplo n.º 7
0
class AccountNotificationsActive(SelenolService):
    """Returns all the active user notifications."""
    def __init__(self, connection=None, session=None):
        """Constructor.

        :param connection: Backend string connection.
        :param session: Database session creator.
        """
        super(AccountNotificationsActive,
              self).__init__(['account', 'notifications', 'active'],
                             connection, session)

    @selenol_params(
        user=get_user_from_session(), )
    def on_request(self, user):
        """Request method.

        :param user: User that is executing the request.
        """
        return [
            notification_serializer(notification)
            for notification in user.notifications.filter(
                AccountNotification.saw_at.is_(None))
        ]