Ejemplo n.º 1
0
def get_user_stats():
    """ Get basic database statistics.

    :return: users and groups counts from the database
    :rtype: dictionary
    """
    usercount = DBSession.query(AuthUser.user_id).count()
    groupcount = DBSession.query(AuthGroup.group_id).count()
    return dict(usercount=usercount, groupcount=groupcount)
Ejemplo n.º 2
0
def get_user_stats():
    """ Get basic database statistics.

    :return: users and groups counts from the database
    :rtype: dictionary
    """
    usercount = DBSession.query(AuthUser.user_id).count()
    groupcount = DBSession.query(AuthGroup.group_id).count()
    return dict(usercount=usercount, groupcount=groupcount)
Ejemplo n.º 3
0
def get_zone_stats():
    """ Get basic database statistics.

    :return: zones and groups counts from the database
    :rtype: dictionary
    """
    zonecount = DBSession.query(Zone.zone_id).count()
    groupcount = DBSession.query(Group.group_id).count()
    rockcount = DBSession.query(Rock.rock_id).count()
    engravingcount = None
    return dict(zonecount=zonecount, groupcount=groupcount,
                rockcount=rockcount, engravingcount=engravingcount)
Ejemplo n.º 4
0
def user_list_view(request):
    """ Render the user list page.

    Return a paged user list from the database. The paged list can be
    ordered by username, first name or last name. Add an error flash message
    if the list is empty. Return also basic users statistics.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    stats = get_user_stats()
    sortable_columns = ['username', 'first_name', 'last_name']
    column = request.params.get('sort')
    search = request.params.get('search')
    # construct the query
    users = DBSession.query(AuthUser)
    if column and column in sortable_columns:
        users = users.order_by(column)
    else:
        users = users.order_by(AuthUser.username)
    if search:
        users = users.filter(AuthUser.username.like('%' + search + '%'))
    # add a flash message for empty results
    if users.count() == 0:
        request.session.flash(_(u"There is no results!"), 'error')
    # paginate results
    page_url = paginate.PageURL_WebOb(request)
    users = paginate.Page(users,
                          page=int(request.params.get("page", 1)),
                          items_per_page=20,
                          url=page_url)
    return dict(users=users, stats=stats)
Ejemplo n.º 5
0
def user_list_view(request):
    """ Render the user list page.

    Return a paged user list from the database. The paged list can be
    ordered by username, first name or last name. Add an error flash message
    if the list is empty. Return also basic users statistics.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    stats = get_user_stats()
    sortable_columns = ['username', 'first_name', 'last_name']
    column = request.params.get('sort')
    search = request.params.get('search')
    # construct the query
    users = DBSession.query(AuthUser)
    if column and column in sortable_columns:
        users = users.order_by(column)
    else:
        users = users.order_by(AuthUser.username)
    if search:
        users = users.filter(AuthUser.username.like('%' + search + '%'))
    # add a flash message for empty results
    if users.count() == 0:
        request.session.flash(_(u"There is no results!"), 'error')
    # paginate results
    page_url = paginate.PageURL_WebOb(request)
    users = paginate.Page(users,
                          page=int(request.params.get("page", 1)),
                          items_per_page=20,
                          url=page_url)
    return dict(users=users, stats=stats)
Ejemplo n.º 6
0
    def get_by_id(cls, group_id):
        """ Query the `auth_group` table by `group_id`.

        :param group_id: the group id
        :type group_id: integer
        :return: a ``sqlalchemy.orm.query.Query`` object
        """
        return DBSession.query(cls).get(group_id)
Ejemplo n.º 7
0
    def get_by_email(cls, email=None):
        """ Query the auth_user table by email.

        :param username: the user email
        :type username: unicode
        :return: a ``sqlalchemy.orm.query.Query`` object
        """
        if email:
            return DBSession.query(cls).filter(cls.email == email).first()
Ejemplo n.º 8
0
    def get_by_id(cls, user_id=None):
        """ Query the `auth_user` table by `user_id`.

        :param user_id: the user id
        :type username: integer
        :return: a ``sqlalchemy.orm.query.Query`` object
        """
        if user_id:
            return DBSession.query(cls).get(user_id)
Ejemplo n.º 9
0
def get_grouplist():
    """ Generate a list of groups from the database.

    :return: a list of pair values (`group_id`, `groupname`)
    :rtype: list
    """
    # For use in group select forms.
    groups = DBSession.query(AuthGroup).order_by(AuthGroup.groupname).all()
    grouplist = [(group.group_id, group.groupname) for group in groups]
    return grouplist
Ejemplo n.º 10
0
    def get_by_username(cls, username=None):
        """ Query the `auth_user` table by username.

        :param username: the user username
        :type username: unicode
        :return: a ``sqlalchemy.orm.query.Query`` object
        """
        if username:
            return DBSession.query(cls).filter(
                       cls.username == username).first()
Ejemplo n.º 11
0
def get_grouplist():
    """ Generate a list of groups from the database.

    :return: a list of pair values (`group_id`, `groupname`)
    :rtype: list
    """
    # For use in group select forms.
    groups = DBSession.query(AuthGroup).order_by(AuthGroup.groupname).all()
    grouplist = [(group.group_id, group.groupname) for group in groups]
    return grouplist
Ejemplo n.º 12
0
def get_alembic_revision(config_uri):
    """ Check the existence of an alembic revision in the database. If the
    database is versioned, then return the current revision.

    :param config_uri: an .ini file
    :return: the alembic revision value or None
    """
    try:
        revision = DBSession.query(Migration.version_num).first()
    except OperationalError:
        revision = None
    return revision
Ejemplo n.º 13
0
def zone_list_view(request):

    _ = request.translate
    stats = get_zone_stats()
    # construct the query
    zones = DBSession.query(Zone)
    zones = zones.order_by(Zone.zone_number)
    # add a flash message for empty results
    if zones.count() == 0:
        request.session.flash(_(u"There is no results!"), 'error')
    # paginate results
    page_url = paginate.PageURL_WebOb(request)
    zones = paginate.Page(zones,
                          page=int(request.params.get("page", 1)),
                          items_per_page=20,
                          url=page_url)
    return dict(zones=zones, stats=stats)
Ejemplo n.º 14
0
def user_delete_view(request):
    """ Delete an user.

    Seek the database for the user datas based on user_id used in the route. If
    the user did not exist then add an error flash message and redirect to the
    user list. If the user exist then delete the user in the database, add a
    warning flash message and then redirect to the user list.

    :param request: a ``pyramid.request`` object
    """
    # The confirm delete must be managed by modal messages in the templates,
    # and we forbid direct deletion from the address bar (no referer)
    _ = request.translate
    if not request.referer:
        request.session.flash(_(u"Insufficient permissions!"),
                              'error')
        return HTTPFound(location=request.route_path('home'))

    user_id = request.matchdict['user_id']
    user = AuthUser.get_by_id(user_id)
    if not user:
        request.session.flash(_(u"This user did not exist!"), 'error')
        return HTTPFound(location=request.route_path('tools.user_list'))

    #forbid the deletion if it's the only admin user
    if user.group.groupname == u'admins':
        adminscount = DBSession.query(AuthUser.user_id).join(AuthGroup).\
                                filter(AuthGroup.groupname == u'admins').\
                                count()
        if adminscount == 1:
            request.session.flash(_(u"Deletion of the only admin forbidden!"),
                                  'error')
            return HTTPFound(location=request.route_path('tools.user_list'))

    DBSession.delete(user)
    request.session.flash(_(u"User deleted."), 'warn')
    return HTTPFound(location=request.route_path('tools.user_list'))
Ejemplo n.º 15
0
def group_list_view(request):

    _ = request.translate
    stats=None
    sortable_columns = ['group_number', 'zone_number']
    #TODO correct zone_number sorting and listing
    column = request.params.get('sort')
    # construct the query
    groups = DBSession.query(Group)
    if column and column in sortable_columns:
        groups = groups.order_by(column)
    else:
        groups = groups.order_by(Group.group_number)
    # add a flash message for empty results
    if groups.count() == 0:
        request.session.flash(_(u"There is no results!"), 'error')

    # paginate results
    page_url = paginate.PageURL_WebOb(request)
    groups = paginate.Page(groups,
                           page=int(request.params.get("page", 1)),
                           items_per_page=20,
                           url=page_url)
    return dict(groups=groups, stats=stats)
Ejemplo n.º 16
0
def user_delete_view(request):
    """ Delete an user.

    Seek the database for the user datas based on user_id used in the route. If
    the user did not exist then add an error flash message and redirect to the
    user list. If the user exist then delete the user in the database, add a
    warning flash message and then redirect to the user list.

    :param request: a ``pyramid.request`` object
    """
    # The confirm delete must be managed by modal messages in the templates,
    # and we forbid direct deletion from the address bar (no referer)
    _ = request.translate
    if not request.referer:
        request.session.flash(_(u"Insufficient permissions!"), 'error')
        return HTTPFound(location=request.route_path('home'))

    user_id = request.matchdict['user_id']
    user = AuthUser.get_by_id(user_id)
    if not user:
        request.session.flash(_(u"This user did not exist!"), 'error')
        return HTTPFound(location=request.route_path('tools.user_list'))

    #forbid the deletion if it's the only admin user
    if user.group.groupname == u'admins':
        adminscount = DBSession.query(AuthUser.user_id).join(AuthGroup).\
                                filter(AuthGroup.groupname == u'admins').\
                                count()
        if adminscount == 1:
            request.session.flash(_(u"Deletion of the only admin forbidden!"),
                                  'error')
            return HTTPFound(location=request.route_path('tools.user_list'))

    DBSession.delete(user)
    request.session.flash(_(u"User deleted."), 'warn')
    return HTTPFound(location=request.route_path('tools.user_list'))
Ejemplo n.º 17
0
def get_groups():
    return DBSession.query(Group).order_by(Group.group_number).all()
Ejemplo n.º 18
0
def get_zones():
    return DBSession.query(Zone).order_by(Zone.zone_number).all()