-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathperson.py
134 lines (114 loc) · 4.35 KB
/
person.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
from database import db
import hashlib
from passlib.hash import sha512_crypt as sha
from datetime import datetime
class user:
def __init__(self, username, password):
self.db = db('aman', '127.0.0.1', 'hacker123', 'ARMS')
self.username = username
self.secret = password
self.authenticated = False
self.auth()
self.get_details()
self.get_devices()
def auth (self):
#this is the place where user will get authenticated
try:
query = 'select password from users where username = "{0}"'.format(self.username)
self.db.cursor.execute(query)
output = self.db.cursor.fetchall()
if sha.verify(self.secret, output[0][0]):
self.authenticated = True
query = 'update users set last_login = now() where username = "{0}";'.format(self.username)
self.db.cursor.execute(query)
self.db.db.commit()
return True
else:
self.authenticated = False
return False
except Exception as e:
print("[ERROR!]")
print(e)
def get_details (self):
try:
if self.authenticated:
query = 'select * from users where username = "{0}"'.format(self.username)
self.db.cursor.execute(query)
output = self.db.cursor.fetchall()
output = output[0]
self.first = output[2]
self.last = output[3]
self.email = output[4]
self.phone = output[5]
self.last_login = output[6].strftime("%d-%b-%Y (%H:%M:%S.%f)")
self.api = output[7]
return True
else:
print("User not logged in!")
return False
except Exception as e:
print("ERROR!")
print(e)
def get_devices(self):
try:
if self.authenticated:
query = 'select deviceID from Node where username = "{0}"'.format(self.username)
self.db.cursor.execute(query)
output = self.db.cursor.fetchall()
dummy = []
for dev in output:
dummy.append(dev[0])
self.device_list = dummy
return dummy
else:
return False
except Exception as e:
print("[Error!]")
print (e)
def dev_info(self, deviceID):
try:
if self.authenticated:
self.db.db.commit()
query = 'select * from Node where deviceID="{0}";'.format(deviceID)
self.db.cursor.execute(query)
output = self.db.cursor.fetchall()
print(output)
return output[0]
else:
return False
except Exception as e:
print('[ERROR!]')
print(e)
def field_values(self, fieldname):
#here we will access all the values of devices according to time
try:
if self.authenticated:
query = 'select * from (select * from {0} order by date_time desc limit 10) dummy order by date_time asc;'.format(fieldname)
self.db.cursor.execute(query)
output = self.db.cursor.fetchall()
return output
else:
return False
except Exception as e:
print('[ERROR!]')
print(e)
def device_values(self, fieldname, deviceID):
try:
if self.authenticated:
query = 'select * from (select * from (select * from {0} where deviceID = "{1}") var1 order by date_time desc limit 10) dummy order by date_time asc;'.format(fieldname, deviceID)
self.db.cursor.execute(query)
output = self.db.cursor.fetchall()
# print(output)
return output
else:
return False
except Exception as e:
print('[ERROR!]')
print(e)
#testing side for the class
# test = user("hellboy", "hello world")
# test.get_details()
# print(test.get_devices())
# print(test.dev_info("ARMS1112"))
# print(test.field_values('Rosegarden'))
# print(test.device_values("Rosegarden", "ARMS12012"))