root / snf-astakos-app / astakos / im / management / commands / _common.py @ 9a06d96f
History | View | Annotate | Download (4.2 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 |
DEFAULT_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 |
try:
|
71 |
return ContentType.objects.get(app_label='im', |
72 |
model='astakosuser')
|
73 |
except:
|
74 |
return DEFAULT_CONTENT_TYPE
|
75 |
|
76 |
|
77 |
def add_user_permission(user, pname): |
78 |
content_type = get_astakosuser_content_type() |
79 |
if user.has_perm(pname):
|
80 |
return 0, None |
81 |
p, created = Permission.objects.get_or_create(codename=pname, |
82 |
name=pname.capitalize(), |
83 |
content_type=content_type) |
84 |
user.user_permissions.add(p) |
85 |
return 1, created |
86 |
|
87 |
|
88 |
def add_group_permission(group, pname): |
89 |
content_type = get_astakosuser_content_type() |
90 |
if pname in [p.codename for p in group.permissions.all()]: |
91 |
return 0, None |
92 |
content_type = ContentType.objects.get(app_label='im',
|
93 |
model='astakosuser')
|
94 |
p, created = Permission.objects.get_or_create(codename=pname, |
95 |
name=pname.capitalize(), |
96 |
content_type=content_type) |
97 |
group.permissions.add(p) |
98 |
return 1, created |
99 |
|
100 |
|
101 |
def remove_user_permission(user, pname): |
102 |
content_type = get_astakosuser_content_type() |
103 |
if user.has_perm(pname):
|
104 |
return 0 |
105 |
try:
|
106 |
p = Permission.objects.get(codename=pname, |
107 |
content_type=content_type) |
108 |
user.user_permissions.remove(p) |
109 |
return 1 |
110 |
except Permission.DoesNotExist:
|
111 |
return -1 |
112 |
|
113 |
|
114 |
def remove_group_permission(group, pname): |
115 |
content_type = get_astakosuser_content_type() |
116 |
if pname not in [p.codename for p in group.permissions.all()]: |
117 |
return 0 |
118 |
try:
|
119 |
p = Permission.objects.get(codename=pname, |
120 |
content_type=content_type) |
121 |
group.permissions.remove(p) |
122 |
return 1 |
123 |
except Permission.DoesNotExist:
|
124 |
return -1 |