forked from abztrakt/labgeeksrpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
141 lines (124 loc) · 4.98 KB
/
views.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from django.contrib.auth import authenticate, login, logout
from django.core.context_processors import csrf
from django.shortcuts import render_to_response, HttpResponseRedirect
from django.template import RequestContext
from labgeeksrpg.forms import LoginForm
import datetime
from labgeeksrpg.labgeeksrpg_config.models import Notification
from labgeeksrpg.labgeeksrpg_config.forms import NotificationForm
def hello(request):
""" The site dashboard.
"""
#when a user is logged-in correctly
if request.user.is_authenticated():
locations = request.user.location_set.all()
shifts = request.user.shift_set.all()
clockin_time = 0
if locations and shifts:
clockin_time = shifts[len(shifts) - 1].intime
now = datetime.datetime.now()
notifications = Notification.objects.all()
events = []
alerts = []
for noti in notifications:
if noti.due_date:
if now.date() - noti.due_date.date() >= datetime.timedelta(days=1):
noti.archived = True
elif not noti.due_date - now > datetime.timedelta(days=7) and not noti.archived:
events.append(noti)
else:
if not noti.archived:
alerts.append(noti)
events.sort(key=lambda x: x.due_date)
c = {}
c.update(csrf(request))
can_Add = False
if request.user.has_perm('labgeeksrpg_config.add_notification'):
can_Add = True
form_is_valid = True
if request.method == 'POST':
archive_ids = request.POST.getlist('pk')
if archive_ids:
for archive_id in archive_ids:
notif = Notification.objects.get(pk=archive_id)
notif.archived = True
notif.save()
return HttpResponseRedirect('/')
form = NotificationForm(request.POST)
if form.is_valid():
form_is_valid = True
notification = form.save(commit=False)
notification.user = request.user
if notification.due_date:
if now.date() - notification.due_date.date() >= datetime.timedelta(days=1):
notification.archived = True
notification.save()
return HttpResponseRedirect('/')
else:
form_is_valid = False
else:
form = NotificationForm()
workshifts = request.user.workshift_set.all()
today_past_shifts = []
today_future_shifts = []
for shift in workshifts:
in_time = shift.scheduled_in
out_time = shift.scheduled_out
if (in_time.year == now.year and in_time.month == now.month and in_time.day == now.day):
if now - out_time > datetime.timedelta(seconds=1):
today_past_shifts.append(shift)
else:
today_future_shifts.append(shift)
args = {
'request': request,
'locations': locations,
'clockin_time': clockin_time,
'today_past_shifts': today_past_shifts,
'today_future_shifts': today_future_shifts,
'events': events,
'alerts': alerts,
'can_Add': can_Add,
}
return render_to_response('dashboard.html', locals(), context_instance=RequestContext(request))
else:
return render_to_response('hello.html', locals())
def labgeeks_login(request):
""" Login a user. Called by the @login_required decorator.
"""
# Get a token to protect from cross-site request forgery
c = {}
c.update(csrf(request))
if request.user.is_authenticated():
try:
return HttpResponseRedirect(request.GET['next'])
except:
return HttpResponseRedirect('/')
elif request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
try:
return HttpResponseRedirect(request.GET['next'])
except:
#Redirects to the dashboard.
return HttpResponseRedirect('/')
else:
# Return a disabled account error
return HttpResponseRedirect('/inactive/')
else:
form = LoginForm()
return render_to_response('login.html', locals(), context_instance=RequestContext(request))
def labgeeks_logout(request):
""" Manually log a user out.
"""
logout(request)
return HttpResponseRedirect('/')
def inactive(request):
""" Return if a user's account has been disabled.
"""
return render_to_response('inactive.html', locals())