forked from alebas-fr/BadgeuseDokos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.py
48 lines (42 loc) · 1.66 KB
/
authentication.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
import config
import json
from datetime import datetime as dt
from dokos_connector import DokosConnector
class RfidNotFound(Exception):
pass
class Authentication:
def __init__(self, rfid):
self.rfid = rfid
self.connector = DokosConnector(config.api_url, config.dokos_client, config.dokos_token)
def get_user_and_customer_for_rfid(self):
document = self.connector.get_resources_by_filter(resource=config.badge_allocation_resource_name,
filters=[["rfid", "=", self.rfid]]).json()['data']
print(document,len(document))
if len(document)==0:
raise RfidNotFound
return None
else:
result = \
self.connector.get_resource(resource=config.badge_allocation_resource_name, name=document[0]['name']).json()[
'data']
return result['user'], result['customer']
def add_passage_to_log(self, date=None,type="None"):
try :
user,customer = self.get_user_and_customer_for_rfid()
except RfidNotFound:
user = None
customer = None
raise RfidNotFound
if user==None and customer==None:
raise RfidNotFound
if date==None:
date = str(dt.now())
data = {
'user': user,
'customer': customer,
'rfid': self.rfid,
'date': date,
'type': type
}
print("J'ai noté le passge dans log")
return self.connector.insert_resource(resource=config.passage_log_resource_name, data=json.dumps(data))