Revision 5ce3ce4f snf-astakos-app/astakos/im/util.py
b/snf-astakos-app/astakos/im/util.py | ||
---|---|---|
1 | 1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved. |
2 |
#
|
|
2 |
# |
|
3 | 3 |
# Redistribution and use in source and binary forms, with or |
4 | 4 |
# without modification, are permitted provided that the following |
5 | 5 |
# conditions are met: |
6 |
#
|
|
6 |
# |
|
7 | 7 |
# 1. Redistributions of source code must retain the above |
8 | 8 |
# copyright notice, this list of conditions and the following |
9 | 9 |
# disclaimer. |
10 |
#
|
|
10 |
# |
|
11 | 11 |
# 2. Redistributions in binary form must reproduce the above |
12 | 12 |
# copyright notice, this list of conditions and the following |
13 | 13 |
# disclaimer in the documentation and/or other materials |
14 | 14 |
# provided with the distribution. |
15 |
#
|
|
15 |
# |
|
16 | 16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
17 | 17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | 18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
... | ... | |
25 | 25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
26 | 26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | 27 |
# POSSIBILITY OF SUCH DAMAGE. |
28 |
#
|
|
28 |
# |
|
29 | 29 |
# The views and conclusions contained in the software and |
30 | 30 |
# documentation are those of the authors and should not be |
31 | 31 |
# interpreted as representing official policies, either expressed |
... | ... | |
52 | 52 |
|
53 | 53 |
logger = logging.getLogger(__name__) |
54 | 54 |
|
55 |
|
|
55 | 56 |
class UTC(tzinfo): |
56 | 57 |
def utcoffset(self, dt): |
57 | 58 |
return timedelta(0) |
58 |
|
|
59 |
|
|
59 | 60 |
def tzname(self, dt): |
60 | 61 |
return 'UTC' |
61 |
|
|
62 |
|
|
62 | 63 |
def dst(self, dt): |
63 | 64 |
return timedelta(0) |
64 | 65 |
|
66 |
|
|
65 | 67 |
def isoformat(d): |
66 | 68 |
"""Return an ISO8601 date string that includes a timezone.""" |
67 |
|
|
69 |
|
|
68 | 70 |
return d.replace(tzinfo=UTC()).isoformat() |
69 | 71 |
|
72 |
|
|
70 | 73 |
def epoch(datetime): |
71 |
return int(time.mktime(datetime.timetuple())*1000) |
|
74 |
return int(time.mktime(datetime.timetuple()) * 1000) |
|
75 |
|
|
72 | 76 |
|
73 | 77 |
def get_context(request, extra_context=None, **kwargs): |
74 | 78 |
extra_context = extra_context or {} |
75 | 79 |
extra_context.update(kwargs) |
76 | 80 |
return RequestContext(request, extra_context) |
77 | 81 |
|
82 |
|
|
78 | 83 |
def get_invitation(request): |
79 | 84 |
""" |
80 | 85 |
Returns the invitation identified by the ``code``. |
81 |
|
|
86 |
|
|
82 | 87 |
Raises ValueError if the invitation is consumed or there is another account |
83 | 88 |
associated with this email. |
84 | 89 |
""" |
... | ... | |
87 | 92 |
code = request.POST.get('code') |
88 | 93 |
if not code: |
89 | 94 |
return |
90 |
invitation = Invitation.objects.get(code = code)
|
|
95 |
invitation = Invitation.objects.get(code=code)
|
|
91 | 96 |
if invitation.is_consumed: |
92 | 97 |
raise ValueError(_('Invitation is used')) |
93 | 98 |
if reserved_email(invitation.username): |
94 | 99 |
raise ValueError(_('Email: %s is reserved' % invitation.username)) |
95 | 100 |
return invitation |
96 | 101 |
|
102 |
|
|
97 | 103 |
def prepare_response(request, user, next='', renew=False): |
98 | 104 |
"""Return the unique username and the token |
99 | 105 |
as 'X-Auth-User' and 'X-Auth-Token' headers, |
100 | 106 |
or redirect to the URL provided in 'next' |
101 | 107 |
with the 'user' and 'token' as parameters. |
102 |
|
|
108 |
|
|
103 | 109 |
Reissue the token even if it has not yet |
104 | 110 |
expired, if the 'renew' parameter is present |
105 | 111 |
or user has not a valid token. |
... | ... | |
111 | 117 |
try: |
112 | 118 |
user.save() |
113 | 119 |
except ValidationError, e: |
114 |
return HttpResponseBadRequest(e)
|
|
115 |
|
|
120 |
return HttpResponseBadRequest(e) |
|
121 |
|
|
116 | 122 |
if FORCE_PROFILE_UPDATE and not user.is_verified and not user.is_superuser: |
117 | 123 |
params = '' |
118 | 124 |
if next: |
119 | 125 |
params = '?' + urlencode({'next': next}) |
120 | 126 |
next = reverse('edit_profile') + params |
121 |
|
|
127 |
|
|
122 | 128 |
response = HttpResponse() |
123 |
|
|
129 |
|
|
124 | 130 |
# authenticate before login |
125 | 131 |
user = authenticate(email=user.email, auth_token=user.auth_token) |
126 | 132 |
login(request, user) |
127 | 133 |
set_cookie(response, user) |
128 | 134 |
request.session.set_expiry(user.auth_token_expires) |
129 |
|
|
135 |
|
|
130 | 136 |
if not next: |
131 | 137 |
next = reverse('index') |
132 |
|
|
138 |
|
|
133 | 139 |
response['Location'] = next |
134 | 140 |
response.status_code = 302 |
135 | 141 |
return response |
136 | 142 |
|
143 |
|
|
137 | 144 |
def set_cookie(response, user): |
138 | 145 |
expire_fmt = user.auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z') |
139 | 146 |
cookie_value = quote(user.email + '|' + user.auth_token) |
140 | 147 |
response.set_cookie(COOKIE_NAME, value=cookie_value, |
141 |
expires=expire_fmt, path='/', |
|
142 |
domain=COOKIE_DOMAIN, secure=COOKIE_SECURE |
|
143 |
) |
|
148 |
expires=expire_fmt, path='/',
|
|
149 |
domain=COOKIE_DOMAIN, secure=COOKIE_SECURE
|
|
150 |
)
|
|
144 | 151 |
msg = 'Cookie [expiring %s] set for %s' % ( |
145 | 152 |
user.auth_token_expires, |
146 | 153 |
user.email |
147 | 154 |
) |
148 | 155 |
logger.log(LOGGING_LEVEL, msg) |
149 | 156 |
|
157 |
|
|
150 | 158 |
class lazy_string(object): |
151 | 159 |
def __init__(self, function, *args, **kwargs): |
152 |
self.function=function
|
|
153 |
self.args=args
|
|
154 |
self.kwargs=kwargs
|
|
155 |
|
|
160 |
self.function = function
|
|
161 |
self.args = args
|
|
162 |
self.kwargs = kwargs
|
|
163 |
|
|
156 | 164 |
def __str__(self): |
157 | 165 |
if not hasattr(self, 'str'): |
158 |
self.str=self.function(*self.args, **self.kwargs)
|
|
166 |
self.str = self.function(*self.args, **self.kwargs)
|
|
159 | 167 |
return self.str |
160 | 168 |
|
169 |
|
|
161 | 170 |
def reverse_lazy(*args, **kwargs): |
162 | 171 |
return lazy_string(reverse, *args, **kwargs) |
163 | 172 |
|
173 |
|
|
164 | 174 |
def reserved_email(email): |
165 |
return AstakosUser.objects.filter(email = email).count() != 0 |
|
175 |
return AstakosUser.objects.filter(email=email).count() != 0 |
|
176 |
|
|
166 | 177 |
|
167 | 178 |
def get_query(request): |
168 | 179 |
try: |
169 | 180 |
return request.__getattribute__(request.method) |
170 | 181 |
except AttributeError: |
171 |
return request.GET |
|
182 |
return request.GET |
Also available in: Unified diff