build.models.return_all()   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
nop 0
1
"""
2
incomplete
3
implemented using data structures
4
#creating dtrequest, dtuser and dtlogin list with dictionary 
5
 to simulate data store
6
7
"""
8
9
import psycopg2
10
from config import dbconfig, basedir, filename , section
11
import os
12
import jwt
13
14
from flask import json, jsonify
15
from itsdangerous import (TimedJSONWebSignatureSerializer 
16
                            as Serializer, BadSignature, SignatureExpired)
17
18
19
20
21
22
def test_connection():
23
    """Test connection to the postgresql server"""
24
25
    conn = None
26
    try:
27
        #read conection parameters
28
        params =  dbconfig(filename,section)
29
        
30
        #connect to server
31
        print('Conecting to the PostgreSQL database...')
32
        conn = psycopg2.connect(**params)
33
        #create a cursor
34
        cur = conn.cursor()
35
36
        #execute a statement
37
        print('Postgres database version: ')
38
        cur.execute('SELECT version()')
39
40
        #display the postgress db server version
41
        db_version = cur.fetchone()
42
        print(db_version)
43
44
        #close comm with pgSQL
45
        cur.close()
46
    except (Exception, psycopg2.DatabaseError) as error:
47
        print(error)
48
    finally:
49
        if conn is not None:
50
            conn.close()
51
            print('Database connection closed.')
52
53
54
55
56
57
58
#User sample data
59
dtusers = [
60
{
61
        "id": 1,
62
        "fname": "John", 
63
        "lname": "Doe",
64
        "email": "[email protected]"  
65
    },
66
    {
67
        "id": 2,
68
        "fname": "Susan", 
69
        "lname": "Sue",
70
        "email": "[email protected]" 
71
    },
72
    {
73
        "id": 3,
74
        "fname": "Mary", 
75
        "lname": "Doe",
76
        "email": "[email protected]" 
77
    },
78
    {
79
        "id": 4,
80
        "fname": "Anto", 
81
        "lname": "Denis",
82
        "email": "[email protected]"
83
    }
84
]
85
86
#requests sample data
87
dtrequest = [
88
    {
89
        "id": 1,
90
        "requestor":"Anto kish",
91
        "email": "[email protected]",
92
        "type": "maintenance",
93
        "status":"Approved",
94
        "desc": "Description goes here"
95
    },
96
    {
97
        "id": 2,
98
        "requestor":"John Doe",
99
        "email": "[email protected]",
100
        "type": "repair",
101
        "status":"Pending",
102
        "desc": "Description goes here"
103
    },
104
    {
105
        "id": 3,
106
        "requestor":"Anto kish",
107
        "email": "[email protected]",
108
        "type": "maintenance",
109
        "status":"Pending",
110
        "desc": "Description goes here"
111
    },
112
    {
113
        "id": 4,
114
        "requestor":"John Doe",
115
        "email": "[email protected]",
116
        "type": "maintenance",
117
        "status":"Approved",
118
        "desc": "Description goes here"
119
    }
120
]
121
#login data
122
dtlogin = [
123
    {
124
        "id": 1,
125
        "username": "[email protected]",
126
        "password": "pass"
127
    },
128
    {
129
        "id": 2,
130
        "username": "[email protected]",
131
        "password": "pass"
132
    }
133
134
]
135
136
def find_by_username(username):
137
    query = """SELECT username,password FROM tb_users WHERE username=(%s)"""
138
    
139
    conn = None
140
    result = None
141
    try:
142
        params = dbconfig(filename, section)
143
        conn = psycopg2.connect(**params)
144
145
        cur = conn.cursor()
146
        cur.execute(query,(username,))
147
148
        result = cur.fetchone()
149
        
150
        #print(result)
151
        
152
        cur.close()
153
154
    except (Exception, psycopg2.DatabaseError) as error:
155
        print(error)
156
    finally:
157
        if conn is not None:
158
            conn.close()
159
    return result
160
161
def return_all():
162
    query = """select array_to_json(array_agg(row_to_json(t))) from (  
163
                SELECT * FROM tb_users) t"""
164
    
165
    conn = None
166
    result = None
167
168
    try:
169
        params = dbconfig(filename, section)
170
171
        conn = psycopg2.connect(**params)
172
173
        cur = conn.cursor()
174
        cur.execute(query)
175
176
        result = cur.fetchall()
177
178
        cur.close()
179
180
181
    except (Exception, psycopg2.DatabaseError) as error:
182
        print (error)
183
    finally:
184
        if conn is not None:
185
            conn.close()
186
    return result
187
188
current_user = find_by_username("[email protected]")
189
190
if __name__ == '__main__':
191
    #test_connection()
192
    current_user[0]
193
    print(return_all())