root / crm / robocrm / views.py @ e4313fa2
History | View | Annotate | Download (1.94 KB)
1 |
# Create your views here.
|
---|---|
2 |
from django.http import HttpResponse, Http404 |
3 |
from django.core.exceptions import PermissionDenied, DoesNotExist |
4 |
from django.contrib.auth import authenticate, login |
5 |
from robocrm.models import * |
6 |
|
7 |
def index(request): |
8 |
return HttpResponse("Hello again, world!") |
9 |
|
10 |
def roboauth(request, rfid_tag, mach_num): |
11 |
r = RoboUser.objects.filter(rfid=rfid_tag) |
12 |
if r.count() > 0: |
13 |
us = r[0]
|
14 |
else:
|
15 |
return HttpResponse("0") |
16 |
auth_machines = us.machines.filter(id=mach_num) |
17 |
if auth_machines.count() > 0 : |
18 |
return HttpResponse("1") |
19 |
else :
|
20 |
return HttpResponse("0") |
21 |
|
22 |
def roboauthall(request, rfid_tag): |
23 |
r = RoboUser.objects.filter(rfid=rfid_tag) |
24 |
if r.count() > 0: |
25 |
us = r[0]
|
26 |
else:
|
27 |
return HttpResponse("0") |
28 |
auth = 0
|
29 |
for mach in us.machines.all(): |
30 |
auth |= 1 << int(mach.id) |
31 |
return HttpResponse(str(auth)) |
32 |
|
33 |
def add_card_event(request): |
34 |
if request.method != 'POST': |
35 |
raise Http404
|
36 |
if 'username' in request.POST and 'password' in request.POST: |
37 |
user = authenticate(username=request.POST['username'],
|
38 |
password=request.POST['password'])
|
39 |
if user is not None and user.is_active: |
40 |
login(request, user) |
41 |
|
42 |
if not request.user.is_authenticated() \ |
43 |
or not request.user.has_perm('robocrm.add_event'): |
44 |
raise PermissionDenied
|
45 |
|
46 |
tstart = request.POST['tstart'] # TODO convert to date |
47 |
tend = request.POST['tend']
|
48 |
user_id = request.POST['user_id']
|
49 |
succ = request.POST['succ'] == '1' |
50 |
imgurl = '' # TODO find url based on tstart |
51 |
machine_id = int(request.POST['machine_id']) |
52 |
|
53 |
try:
|
54 |
robouser = RoboUser.objects.get(rfid__iexact=user_id) |
55 |
except DoesNotExist:
|
56 |
robouser = None
|
57 |
|
58 |
try:
|
59 |
machine = Machine.objects.get(id__exact=machine_id) |
60 |
except DoesNotExist:
|
61 |
machine = None
|
62 |
|
63 |
ev = Event(type='card',
|
64 |
tstart=tstart, |
65 |
tend=tend, |
66 |
user=robouser, |
67 |
succ=succ, |
68 |
imgurl=imgurl, |
69 |
machine=machine, |
70 |
project=None,
|
71 |
matuse='')
|
72 |
|
73 |
ev.save() |
74 |
|
75 |
return HttpResponse()
|