-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
54 lines (37 loc) · 1.36 KB
/
utils.py
File metadata and controls
54 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Helper functions used by the Drycc Passport server.
"""
import logging
import six
import datetime
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.shortcuts import render
logger = logging.getLogger(__name__)
def timestamp2datetime(timestamp):
return datetime.datetime.fromtimestamp(timestamp)
def login_required(
required=True, json_stream=True, has_version=True,
methods=["GET", "POST", "PUT", "DELETE", "PATCH"]):
def _login_required(view):
def __decorator(request, *args, **kwargs):
if request.method not in methods:
return view(request, *args, **kwargs)
if not request.user.pk or not request.user.is_authenticated:
return render(request, 'user/login.html')
else:
return view(request, *args, **kwargs)
return __decorator
return _login_required
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.is_active)
)
account_activation_token = TokenGenerator()
def get_local_host(request):
uri = request.build_absolute_uri()
return uri[0:uri.find(request.path)]
if __name__ == "__main__":
import doctest
doctest.testmod()