Blame progit/lib/login.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
"""
Pierre-Yves Chibon 9073e5
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
 Authors:
Pierre-Yves Chibon 9073e5
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
"""
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 3194ac
import random
Pierre-Yves Chibon 3194ac
import string
Pierre-Yves Chibon 3194ac
Pierre-Yves Chibon 9073e5
from progit.lib import model
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 3194ac
def id_generator(size=15, chars=string.ascii_uppercase + string.digits):
Pierre-Yves Chibon 3194ac
    """ Generates a random identifier for the given size and using the
Pierre-Yves Chibon 3194ac
    specified characters.
Pierre-Yves Chibon 3194ac
    If no size is specified, it uses 15 as default.
Pierre-Yves Chibon 3194ac
    If no characters are specified, it uses ascii char upper case and
Pierre-Yves Chibon 3194ac
    digits.
Pierre-Yves Chibon 3194ac
    :arg size: the size of the identifier to return.
Pierre-Yves Chibon 3194ac
    :arg chars: the list of characters that can be used in the
Pierre-Yves Chibon 3194ac
        idenfitier.
Pierre-Yves Chibon 3194ac
    """
Pierre-Yves Chibon 3194ac
    return ''.join(random.choice(chars) for x in range(size))
Pierre-Yves Chibon 3194ac
Pierre-Yves Chibon 3194ac
Pierre-Yves Chibon 9073e5
def get_session_by_visitkey(session, sessionid):
Pierre-Yves Chibon 9073e5
    ''' Return a specified VisitUser via its session identifier (visit_key).
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    :arg session: the session with which to connect to the database.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    '''
Pierre-Yves Chibon 9073e5
    query = session.query(
Pierre-Yves Chibon 9073e5
        model.ProgitUserVisit
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.ProgitUserVisit.visit_key == sessionid
Pierre-Yves Chibon 9073e5
    )
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    return query.first()
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
def get_groups(session):
Pierre-Yves Chibon 9073e5
    ''' Return the list of groups present in the database.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    :arg session: the session with which to connect to the database.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    '''
Pierre-Yves Chibon 9073e5
    query = session.query(
Pierre-Yves Chibon 9073e5
        model.ProgitGroup
Pierre-Yves Chibon 9073e5
    ).order_by(
Pierre-Yves Chibon 9073e5
        model.ProgitGroup.group_name
Pierre-Yves Chibon 9073e5
    )
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    return query.all()
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
def get_group(session, group):
Pierre-Yves Chibon 9073e5
    ''' Return a specific group for the specified group name.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    :arg session: the session with which to connect to the database.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    '''
Pierre-Yves Chibon 9073e5
    query = session.query(
Pierre-Yves Chibon 9073e5
        model.ProgitGroup
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.ProgitGroup.group_name == group
Pierre-Yves Chibon 9073e5
    ).order_by(
Pierre-Yves Chibon 9073e5
        model.ProgitGroup.group_name
Pierre-Yves Chibon 9073e5
    )
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    return query.first()
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
def get_users_by_group(session, group):
Pierre-Yves Chibon 9073e5
    ''' Return the list of users for a specified group.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    :arg session: the session with which to connect to the database.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    '''
Pierre-Yves Chibon 9073e5
    query = session.query(
Pierre-Yves Chibon 9073e5
        model.User
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.User.id == model.ProgitUserGroup.user_id
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.ProgitUserGroup.group_id == model.ProgitGroup.id
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.ProgitGroup.group_name == group
Pierre-Yves Chibon 9073e5
    ).order_by(
Pierre-Yves Chibon 9073e5
        model.User.user
Pierre-Yves Chibon 9073e5
    )
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    return query.all()
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
def get_user_group(session, userid, groupid):
Pierre-Yves Chibon 9073e5
    ''' Return a specific user_group for the specified group and user
Pierre-Yves Chibon 9073e5
    identifiers.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    :arg session: the session with which to connect to the database.
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    '''
Pierre-Yves Chibon 9073e5
    query = session.query(
Pierre-Yves Chibon 9073e5
        model.ProgitUserGroup
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.ProgitUserGroup.user_id == userid
Pierre-Yves Chibon 9073e5
    ).filter(
Pierre-Yves Chibon 9073e5
        model.ProgitUserGroup.group_id == groupid
Pierre-Yves Chibon 9073e5
    )
Pierre-Yves Chibon 9073e5
Pierre-Yves Chibon 9073e5
    return query.first()