-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
105 lines (83 loc) · 2.69 KB
/
utils.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import re
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import View
from django.core.urlresolvers import reverse
from device.models import Device, DeviceProfile
from users.models import UserProfile
from datetime import datetime, timedelta
"""
Human Sorting
http://nedbatchelder.com/blog/200712/human_sorting.html
Sort log file name by time order
@date 02/20/2012
@author Kate Rhodes
@adder Taeyeon Ki
"""
"""
def tryint(s):
try:
return int(s)
except:
return s
"""
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [int(c) if c.isdigit() else c for c in re.split('([0-9]+)', s) ]
def sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
def re_sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
l.reverse()
"""
Login and Logout function to control session
@date 05/08/2012
@author Taeyeon Ki
"""
class ProtectedView(View):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectedView, self).dispatch(*args, **kwargs)
class AuthorProtectedView(ProtectedView):
def dispatch(self, *args, **kwargs):
post = self.get_queryset()
if not post.author != self.request.user:
return redirect('access_denied')
return super(AuthorProtectedView, self).dispatch(*args, **kwargs)
"""
update working status using Last Log info
@date 07/16/2012
@author Taeyeon Ki
"""
def update_working_status():
deviceprofiles = DeviceProfile.objects.all()
for deviceprofile in deviceprofiles:
if deviceprofile.last_log:
if deviceprofile.last_log < (datetime.today() - timedelta(2)):
#Not Working phone since a phone did not update a log file for two days
DeviceProfile.objects.filter(id=deviceprofile.id).update(status="N")
else:
DeviceProfile.objects.filter(id=deviceprofile.id).update(status="W")
"""
check valid access
@date 07/18/2012
@param String user
@param String deviceId
@author Taeyeon
"""
def is_valid_device(user, deviceId):
userprofile = UserProfile.objects.get(user_id=user.id)
#to protect wrong accesses
if userprofile.user_type == 'M' or userprofile.user_type == 'L':
return DeviceProfile.objects.filter(group = userprofile.group).filter(dev=deviceId).count() > 0
if userprofile.user_type == 'P':
return DeviceProfile.objects.filter(dev=deviceId).filter(user=user).count() > 0
if userprofile.user_type == 'A':
return True
return False