Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.3 kB)

1 397d5cbe Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 397d5cbe Giorgos Verigakis
#
3 397d5cbe Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 397d5cbe Giorgos Verigakis
# without modification, are permitted provided that the following
5 397d5cbe Giorgos Verigakis
# conditions are met:
6 397d5cbe Giorgos Verigakis
#
7 397d5cbe Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 397d5cbe Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 397d5cbe Giorgos Verigakis
#      disclaimer.
10 397d5cbe Giorgos Verigakis
#
11 397d5cbe Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 397d5cbe Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 397d5cbe Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 397d5cbe Giorgos Verigakis
#      provided with the distribution.
15 397d5cbe Giorgos Verigakis
#
16 397d5cbe Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 397d5cbe Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 397d5cbe Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 397d5cbe Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 397d5cbe Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 397d5cbe Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 397d5cbe Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 397d5cbe Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 397d5cbe Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 397d5cbe Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 397d5cbe Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 397d5cbe Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 397d5cbe Giorgos Verigakis
#
29 397d5cbe Giorgos Verigakis
# The views and conclusions contained in the software and
30 397d5cbe Giorgos Verigakis
# documentation are those of the authors and should not be
31 397d5cbe Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 397d5cbe Giorgos Verigakis
# or implied, of GRNET S.A.
33 397d5cbe Giorgos Verigakis
34 397d5cbe Giorgos Verigakis
from datetime import datetime
35 397d5cbe Giorgos Verigakis
36 397d5cbe Giorgos Verigakis
from django.utils.timesince import timesince, timeuntil
37 30dc8c1a Sofia Papagiannaki
from django.contrib.auth.models import Permission
38 30dc8c1a Sofia Papagiannaki
from django.contrib.contenttypes.models import ContentType
39 397d5cbe Giorgos Verigakis
40 397d5cbe Giorgos Verigakis
from astakos.im.models import AstakosUser
41 397d5cbe Giorgos Verigakis
42 30dc8c1a Sofia Papagiannaki
content_type = None
43 397d5cbe Giorgos Verigakis
44 0a569195 Sofia Papagiannaki
def get_user(email_or_id, **kwargs):
45 397d5cbe Giorgos Verigakis
    try:
46 397d5cbe Giorgos Verigakis
        if email_or_id.isdigit():
47 397d5cbe Giorgos Verigakis
            return AstakosUser.objects.get(id=int(email_or_id))
48 397d5cbe Giorgos Verigakis
        else:
49 0a569195 Sofia Papagiannaki
            return AstakosUser.objects.get(email=email_or_id, **kwargs)
50 0a569195 Sofia Papagiannaki
    except AstakosUser.DoesNotExist, AstakosUser.MultipleObjectsReturned:
51 397d5cbe Giorgos Verigakis
        return None
52 397d5cbe Giorgos Verigakis
53 397d5cbe Giorgos Verigakis
def format_bool(b):
54 397d5cbe Giorgos Verigakis
    return 'YES' if b else 'NO'
55 397d5cbe Giorgos Verigakis
56 397d5cbe Giorgos Verigakis
57 397d5cbe Giorgos Verigakis
def format_date(d):
58 397d5cbe Giorgos Verigakis
    if not d:
59 397d5cbe Giorgos Verigakis
        return ''
60 397d5cbe Giorgos Verigakis
61 397d5cbe Giorgos Verigakis
    if d < datetime.now():
62 397d5cbe Giorgos Verigakis
        return timesince(d) + ' ago'
63 397d5cbe Giorgos Verigakis
    else:
64 397d5cbe Giorgos Verigakis
        return 'in ' + timeuntil(d)
65 30dc8c1a Sofia Papagiannaki
66 30dc8c1a Sofia Papagiannaki
def get_astakosuser_content_type():
67 30dc8c1a Sofia Papagiannaki
    if content_type:
68 30dc8c1a Sofia Papagiannaki
        return content_type
69 30dc8c1a Sofia Papagiannaki
    
70 30dc8c1a Sofia Papagiannaki
    try:
71 30dc8c1a Sofia Papagiannaki
        return ContentType.objects.get(app_label='im',
72 30dc8c1a Sofia Papagiannaki
                                       model='astakosuser')
73 30dc8c1a Sofia Papagiannaki
    except:
74 30dc8c1a Sofia Papagiannaki
        return content_type
75 30dc8c1a Sofia Papagiannaki
    
76 30dc8c1a Sofia Papagiannaki
def add_user_permission(user, pname):
77 30dc8c1a Sofia Papagiannaki
    content_type = get_astakosuser_content_type()
78 30dc8c1a Sofia Papagiannaki
    if user.has_perm(pname):
79 30dc8c1a Sofia Papagiannaki
        return 0, None
80 30dc8c1a Sofia Papagiannaki
    p, created = Permission.objects.get_or_create(codename=pname,
81 30dc8c1a Sofia Papagiannaki
                                                  name=pname.capitalize(),
82 30dc8c1a Sofia Papagiannaki
                                                  content_type=content_type)
83 30dc8c1a Sofia Papagiannaki
    user.user_permissions.add(p)
84 30dc8c1a Sofia Papagiannaki
    return 1, created
85 30dc8c1a Sofia Papagiannaki
86 30dc8c1a Sofia Papagiannaki
def add_group_permission(group, pname):
87 30dc8c1a Sofia Papagiannaki
    content_type = get_astakosuser_content_type()
88 30dc8c1a Sofia Papagiannaki
    if pname in [p.codename for p in group.permissions.all()]:
89 30dc8c1a Sofia Papagiannaki
        return 0, None
90 30dc8c1a Sofia Papagiannaki
    content_type = ContentType.objects.get(app_label='im',
91 30dc8c1a Sofia Papagiannaki
                                           model='astakosuser')
92 30dc8c1a Sofia Papagiannaki
    p, created = Permission.objects.get_or_create(codename=pname,
93 30dc8c1a Sofia Papagiannaki
                                                  name=pname.capitalize(),
94 30dc8c1a Sofia Papagiannaki
                                                  content_type=content_type)
95 30dc8c1a Sofia Papagiannaki
    group.permissions.add(p)
96 30dc8c1a Sofia Papagiannaki
    return 1, created
97 30dc8c1a Sofia Papagiannaki
98 30dc8c1a Sofia Papagiannaki
def remove_user_permission(user, pname):
99 30dc8c1a Sofia Papagiannaki
    content_type = get_astakosuser_content_type()
100 30dc8c1a Sofia Papagiannaki
    if user.has_perm(pname):
101 30dc8c1a Sofia Papagiannaki
        return 0
102 30dc8c1a Sofia Papagiannaki
    try:
103 30dc8c1a Sofia Papagiannaki
        p = Permission.objects.get(codename=pname,
104 30dc8c1a Sofia Papagiannaki
                                    content_type=content_type)
105 30dc8c1a Sofia Papagiannaki
        user.user_permissions.remove(p)
106 30dc8c1a Sofia Papagiannaki
        return 1
107 30dc8c1a Sofia Papagiannaki
    except Permission.DoesNotExist, e:
108 30dc8c1a Sofia Papagiannaki
        return -1
109 30dc8c1a Sofia Papagiannaki
110 30dc8c1a Sofia Papagiannaki
def remove_group_permission(group, pname):
111 30dc8c1a Sofia Papagiannaki
    content_type = get_astakosuser_content_type()
112 30dc8c1a Sofia Papagiannaki
    if pname not in [p.codename for p in group.permissions.all()]:
113 30dc8c1a Sofia Papagiannaki
        return 0
114 30dc8c1a Sofia Papagiannaki
    try:
115 30dc8c1a Sofia Papagiannaki
        p = Permission.objects.get(codename=pname,
116 30dc8c1a Sofia Papagiannaki
                                    content_type=content_type)
117 30dc8c1a Sofia Papagiannaki
        group.permissions.remove(p)
118 30dc8c1a Sofia Papagiannaki
        return 1
119 30dc8c1a Sofia Papagiannaki
    except Permission.DoesNotExist, e:
120 30dc8c1a Sofia Papagiannaki
        return -1