views.py 3.1 KB
Newer Older
cahrens committed
1 2 3
"""
Methods for interacting programmatically with the user creator table.
"""
cahrens committed
4
from course_creators.models import CourseCreator
5 6
from student.roles import CourseCreatorRole
from student import auth
cahrens committed
7

cahrens committed
8

9
def add_user_with_status_unrequested(user):
cahrens committed
10 11
    """
    Adds a user to the course creator table with status 'unrequested'.
cahrens committed
12

cahrens committed
13
    If the user is already in the table, this method is a no-op
14
    (state will not be changed).
15 16 17

    If the user is marked as is_staff, this method is a no-op (user
    will not be added to table).
cahrens committed
18
    """
19
    _add_user(user, CourseCreator.UNREQUESTED)
cahrens committed
20 21


cahrens committed
22
def add_user_with_status_granted(caller, user):
cahrens committed
23 24
    """
    Adds a user to the course creator table with status 'granted'.
cahrens committed
25

26
    If appropriate, this method also adds the user to the course creator group maintained by authz.py.
27 28 29 30
    Caller must have staff permissions.

    If the user is already in the table, this method is a no-op
    (state will not be changed).
31 32 33

    If the user is marked as is_staff, this method is a no-op (user
    will not be added to table, nor added to authz.py group).
cahrens committed
34
    """
35 36
    if _add_user(user, CourseCreator.GRANTED):
        update_course_creator_group(caller, user, True)
37 38 39 40 41 42 43 44 45


def update_course_creator_group(caller, user, add):
    """
    Method for adding and removing users from the creator group.

    Caller must have staff permissions.
    """
    if add:
46
        auth.add_users(caller, CourseCreatorRole(), user)
47
    else:
48
        auth.remove_users(caller, CourseCreatorRole(), user)
cahrens committed
49

cahrens committed
50

cahrens committed
51 52
def get_course_creator_status(user):
    """
53
    Returns the status for a particular user, or None if user is not in the table.
cahrens committed
54 55

    Possible return values are:
56 57 58 59
        'unrequested' = user has not requested course creation rights
        'pending' = user has requested course creation rights
        'granted' = user has been granted course creation rights
        'denied' = user has been denied course creation rights
60
        None = user does not exist in the table
cahrens committed
61
    """
62 63 64 65 66 67
    user = CourseCreator.objects.filter(user=user)
    if user.count() == 0:
        return None
    else:
        # User is defined to be unique, can assume a single entry.
        return user[0].state
cahrens committed
68 69


70 71 72 73
def user_requested_access(user):
    """
    User has requested course creator access.

74 75
    This changes the user state to CourseCreator.PENDING, unless the user
    state is already CourseCreator.GRANTED, in which case this method is a no-op.
76 77
    """
    user = CourseCreator.objects.get(user=user)
78 79 80
    if user.state != CourseCreator.GRANTED:
        user.state = CourseCreator.PENDING
        user.save()
81 82


83
def _add_user(user, state):
cahrens committed
84 85
    """
    Adds a user to the course creator table with the specified state.
cahrens committed
86

87 88
    Returns True if user was added to table, else False.

cahrens committed
89
    If the user is already in the table, this method is a no-op
90 91 92
    (state will not be changed, method will return False).

    If the user is marked as is_staff, this method is a no-op (False will be returned).
cahrens committed
93
    """
94
    if not user.is_staff and CourseCreator.objects.filter(user=user).count() == 0:
95
        entry = CourseCreator(user=user, state=state)
cahrens committed
96
        entry.save()
97 98 99
        return True

    return False