go_extras.py 1.74 KB
Newer Older
David Haynes's avatar
David Haynes committed
1
2
3
4
"""
go/templatetags/go_extras.py
"""

5
# Future Imports
David Haynes's avatar
David Haynes committed
6
7
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
8

9
# Django Imports
10
from django import template
11
12

# App Imports
13
14
from go.models import RegisteredUser

15
16
17
18
# To be a valid tag library, the module must contain a module-level variable
# named register that is a template.Library instance, in which all the tags and
# filters are registered.
register = template.Library()
19

20
@register.filter
21
22
23
24
25
26
27
28
def is_registered(given_user):
    """
        Helper template function to check if a user is registered.

        given_user: The User object that we are checking to see if they are registered
        or not.
    """

29
    # try getting the RegisteredUser of the current user
30
    try:
31
        getRegisteredUser = RegisteredUser.objects.get(user=given_user)
32
        # if it works then the user is registered
David Haynes's avatar
David Haynes committed
33
34
        return getRegisteredUser.registered
    # This should never happen
35
    except RegisteredUser.DoesNotExist as ex:
David Haynes's avatar
David Haynes committed
36
        print(ex)
37
        # if they don't exist then they are not registered
38
        return False
39
40

@register.filter
41
42
43
44
45
46
47
48
def is_approved(given_user):
    """
        Helper template function to check if a user is approved.

        given_user: The User object that we are checking to see if they are approved
        or not.
    """

49
    # try getting the RegisteredUser of the current user
50
    try:
51
        get_registered_user = RegisteredUser.objects.get(user=given_user)
52
        # if they exist, return whether or not they are approved (boolean)
53
        return get_registered_user.approved
David Haynes's avatar
David Haynes committed
54
    # This should never happen
55
    except RegisteredUser.DoesNotExist as ex:
David Haynes's avatar
David Haynes committed
56
        print(ex)
57
        # if they don't exist then they are not approved
58
        return False