-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
193 lines (154 loc) · 3.95 KB
/
models.py
File metadata and controls
193 lines (154 loc) · 3.95 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
incomplete
implemented using data structures
#creating dtrequest, dtuser and dtlogin list with dictionary
to simulate data store
"""
import psycopg2
from config import dbconfig, basedir, filename , section
import os
import jwt
from flask import json, jsonify
from itsdangerous import (TimedJSONWebSignatureSerializer
as Serializer, BadSignature, SignatureExpired)
def test_connection():
"""Test connection to the postgresql server"""
conn = None
try:
#read conection parameters
params = dbconfig(filename,section)
#connect to server
print('Conecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
#create a cursor
cur = conn.cursor()
#execute a statement
print('Postgres database version: ')
cur.execute('SELECT version()')
#display the postgress db server version
db_version = cur.fetchone()
print(db_version)
#close comm with pgSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
#User sample data
dtusers = [
{
"id": 1,
"fname": "John",
"lname": "Doe",
"email": "john@gmail.com"
},
{
"id": 2,
"fname": "Susan",
"lname": "Sue",
"email": "sue@gmail.com"
},
{
"id": 3,
"fname": "Mary",
"lname": "Doe",
"email": "mary@gmail.com"
},
{
"id": 4,
"fname": "Anto",
"lname": "Denis",
"email": "anto@gmail.com"
}
]
#requests sample data
dtrequest = [
{
"id": 1,
"requestor":"Anto kish",
"email": "anto@gmail.com",
"type": "maintenance",
"status":"Approved",
"desc": "Description goes here"
},
{
"id": 2,
"requestor":"John Doe",
"email": "john@gmail.com",
"type": "repair",
"status":"Pending",
"desc": "Description goes here"
},
{
"id": 3,
"requestor":"Anto kish",
"email": "anto@gmail.com",
"type": "maintenance",
"status":"Pending",
"desc": "Description goes here"
},
{
"id": 4,
"requestor":"John Doe",
"email": "john@gmail.com",
"type": "maintenance",
"status":"Approved",
"desc": "Description goes here"
}
]
#login data
dtlogin = [
{
"id": 1,
"username": "john@gmail.com",
"password": "pass"
},
{
"id": 2,
"username": "sue@gmail.com",
"password": "pass"
}
]
def find_by_username(username):
query = """SELECT username,password FROM tb_users WHERE username=(%s)"""
conn = None
result = None
try:
params = dbconfig(filename, section)
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute(query,(username,))
result = cur.fetchone()
#print(result)
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return result
def return_all():
query = """select array_to_json(array_agg(row_to_json(t))) from (
SELECT * FROM tb_users) t"""
conn = None
result = None
try:
params = dbconfig(filename, section)
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute(query)
result = cur.fetchall()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print (error)
finally:
if conn is not None:
conn.close()
return result
current_user = find_by_username("antokish@gmail.com")
if __name__ == '__main__':
#test_connection()
current_user[0]
print(return_all())