Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / _common.py @ fc1e2f02

History | View | Annotate | Download (4.3 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from datetime import datetime
35

    
36
from django.utils.timesince import timesince, timeuntil
37
from django.contrib.auth.models import Permission
38
from django.contrib.contenttypes.models import ContentType
39

    
40
from astakos.im.models import AstakosUser
41

    
42
content_type = None
43

    
44
def get_user(email_or_id, **kwargs):
45
    try:
46
        if email_or_id.isdigit():
47
            return AstakosUser.objects.get(id=int(email_or_id))
48
        else:
49
            return AstakosUser.objects.get(email=email_or_id, **kwargs)
50
    except AstakosUser.DoesNotExist, AstakosUser.MultipleObjectsReturned:
51
        return None
52

    
53
def format_bool(b):
54
    return 'YES' if b else 'NO'
55

    
56

    
57
def format_date(d):
58
    if not d:
59
        return ''
60

    
61
    if d < datetime.now():
62
        return timesince(d) + ' ago'
63
    else:
64
        return 'in ' + timeuntil(d)
65

    
66
def get_astakosuser_content_type():
67
    if content_type:
68
        return content_type
69
    
70
    try:
71
        return ContentType.objects.get(app_label='im',
72
                                       model='astakosuser')
73
    except:
74
        return content_type
75
    
76
def add_user_permission(user, pname):
77
    content_type = get_astakosuser_content_type()
78
    if user.has_perm(pname):
79
        return 0, None
80
    p, created = Permission.objects.get_or_create(codename=pname,
81
                                                  name=pname.capitalize(),
82
                                                  content_type=content_type)
83
    user.user_permissions.add(p)
84
    return 1, created
85

    
86
def add_group_permission(group, pname):
87
    content_type = get_astakosuser_content_type()
88
    if pname in [p.codename for p in group.permissions.all()]:
89
        return 0, None
90
    content_type = ContentType.objects.get(app_label='im',
91
                                           model='astakosuser')
92
    p, created = Permission.objects.get_or_create(codename=pname,
93
                                                  name=pname.capitalize(),
94
                                                  content_type=content_type)
95
    group.permissions.add(p)
96
    return 1, created
97

    
98
def remove_user_permission(user, pname):
99
    content_type = get_astakosuser_content_type()
100
    if user.has_perm(pname):
101
        return 0
102
    try:
103
        p = Permission.objects.get(codename=pname,
104
                                    content_type=content_type)
105
        user.user_permissions.remove(p)
106
        return 1
107
    except Permission.DoesNotExist:
108
        return -1
109

    
110
def remove_group_permission(group, pname):
111
    content_type = get_astakosuser_content_type()
112
    if pname not in [p.codename for p in group.permissions.all()]:
113
        return 0
114
    try:
115
        p = Permission.objects.get(codename=pname,
116
                                    content_type=content_type)
117
        group.permissions.remove(p)
118
        return 1
119
    except Permission.DoesNotExist:
120
        return -1