Statistics
| Branch: | Tag: | Revision:

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

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

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

    
54

    
55
def format_bool(b):
56
    return 'YES' if b else 'NO'
57

    
58

    
59
def format_date(d):
60
    if not d:
61
        return ''
62

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

    
68

    
69
def get_astakosuser_content_type():
70
    if content_type:
71
        return content_type
72

    
73
    try:
74
        return ContentType.objects.get(app_label='im',
75
                                       model='astakosuser')
76
    except:
77
        return content_type
78

    
79

    
80
def add_user_permission(user, pname):
81
    content_type = get_astakosuser_content_type()
82
    if user.has_perm(pname):
83
        return 0, None
84
    p, created = Permission.objects.get_or_create(codename=pname,
85
                                                  name=pname.capitalize(),
86
                                                  content_type=content_type)
87
    user.user_permissions.add(p)
88
    return 1, created
89

    
90

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

    
103

    
104
def remove_user_permission(user, pname):
105
    content_type = get_astakosuser_content_type()
106
    if user.has_perm(pname):
107
        return 0
108
    try:
109
        p = Permission.objects.get(codename=pname,
110
                                   content_type=content_type)
111
        user.user_permissions.remove(p)
112
        return 1
113
    except Permission.DoesNotExist:
114
        return -1
115

    
116

    
117
def remove_group_permission(group, pname):
118
    content_type = get_astakosuser_content_type()
119
    if pname not in [p.codename for p in group.permissions.all()]:
120
        return 0
121
    try:
122
        p = Permission.objects.get(codename=pname,
123
                                   content_type=content_type)
124
        group.permissions.remove(p)
125
        return 1
126
    except Permission.DoesNotExist:
127
        return -1