Passed
Push — develop ( 1e8194...5d1e13 )
by Antony
44s
created

build.resources.models.find_by_username()   B

Complexity

Conditions 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 16
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
nop 1
1
import flask
2
import datetime
3
import psycopg2
4
5
from passlib.hash import pbkdf2_sha256 as sha256
6
from run import create_app
7
from config import dbconfig, filename , section
8
from flask_bcrypt import Bcrypt
9
10
#app = create_app('production')
11
#bcrypt = Bcrypt(app)
12
13
def insert_to_db(self, username, password, firstname, lastname, created_on):
14
    """insert a new user into database"""
15
16
    query = """INSERT INTO tb_users (username, password, firstname, lastname, created_on)
17
                VALUES(%s,%s,%s,%s,%s)"""
18
    
19
    conn = None
20
    user_id = None
21
    try:
22
        params = dbconfig(filename, section)
23
        conn = psycopg2.connect(**params)
24
25
        cur = conn.cursor()
26
        cur.execute(query,(username, password, firstname, 
27
                    lastname, created_on,))
28
29
               
30
        conn.commit()
31
        user_id = cur.fetchone()[0]
32
        cur.close()
33
    except (Exception, psycopg2.DatabaseError) as error:
34
        print(error)
35
    finally:
36
        if conn is not None:
37
            conn.close()
38
    return user_id
39
40 View Code Duplication
def find_by_username(username):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
41
    query = """SELECT username,password FROM tb_users WHERE username=(%s)"""
42
    
43
    conn = None
44
    result = None
45
    try:
46
        params = dbconfig(filename, section)
47
        conn = psycopg2.connect(**params)
48
49
        cur = conn.cursor()
50
        cur.execute(query,(username,))
51
52
        result = cur.fetchone()
53
        
54
        #print(result)
55
        
56
        cur.close()
57
58
    except (Exception, psycopg2.DatabaseError) as error:
59
        print(error)
60
    finally:
61
        if conn is not None:
62
            conn.close()
63
    return result
64
65 View Code Duplication
def return_all():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
66
    query = """select array_to_json(array_agg(row_to_json(t))) from (  
67
                SELECT * FROM tb_users) t"""
68
    
69
    conn = None
70
    result = None
71
72
    try:
73
        params = dbconfig(filename, section)
74
75
        conn = psycopg2.connect(**params)
76
77
        cur = conn.cursor()
78
        cur.execute(query)
79
80
        result = cur.fetchall()
81
82
        cur.close()
83
84
85
    except (Exception, psycopg2.DatabaseError) as error:
86
        print (error)
87
    finally:
88
        if conn is not None:
89
            conn.close()
90
    return result
91
92
93
def hash_password(password):
94
    #generate hashed string to store in db
95
    return sha256.hash(password)
96
97
98
def verify_hash(password, hash):
99
    #check the given pass
100
    return sha256.verify(password, hash)
101
102
class RevokedTokenModel(object):
103
    pass