Completed
Push — develop ( 3a3795...1e8194 )
by Antony
03:19 queued 01:31
created

build.models.test_connection()   B

Complexity

Conditions 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
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
"""To be implemented when intergrating with database"""
137
class UserAuth(object):
138
    def hash_password(self, pwd):
139
        pass
140
141
    def verify_password(self, pwd):
142
        pass
143
144
    def generate_auth_token(self, expiration=600):
145
        pass
146
147
    @staticmethod
148
    def verify_auth_token(token):
149
        pass
150
151
if __name__ == '__main__':
152
    test_connection()