Commit 6220f3d4 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

data,pend & user dao changed

parent d173fa12
No preview for this file type
No preview for this file type
import psycopg2
from misc import with_psql
import bcrypt
class UserDao:
def __init__(self):
pass
# decorator implementation
def with_psql(f):
def _with_psql(*args, **kwargs):
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
try:
res = f(cur, *args, **kwargs)
except (Exception, psycopg2.DatabaseError) as error:
conn.rollback()
res = (False, error)
else:
conn.commit()
finally:
cur.close()
conn.close()
return res
return _with_psql
@staticmethod
@with_psql
def create(cur, name, password):
query = """
INSERT INTO
users
VALUES
(%s, %s)
"""
cur.execute(query, (name, bcrypt.hashpw(password, bcrypt.gensalt())))
return (True,)
@with_psql
def create(cur, name, password):
query = """
INSERT INTO
users
VALUES
(%s, %s)
"""
cur.execute(query, (name, bcrypt.hashpw(password, bcrypt.gensalt())))
return (True,)
@staticmethod
@with_psql
def delete(cur, name):
query = """
DELETE FROM
users
WHERE
name = %s
"""
cur.execute(query, (name,))
return (True,)
@with_psql
def delete(cur, name):
query = """
DELETE FROM
users
WHERE
name = %s
"""
cur.execute(query, (name,))
return (True,)
@staticmethod
@with_psql
def update_name(cur, old_name, new_name):
query = """
UPDATE users SET
name = %s
WHERE
name = %s
"""
cur.execute(query, (new_name,))
return (True,)
@with_psql
def update_name(cur, old_name, new_name):
query = """
UPDATE users SET
name = %s
WHERE
name = %s
"""
cur.execute(query, (new_name,))
return (True,)
@staticmethod
@with_psql
def update_password(cur, name, password):
query = """
UPDATE users SET
password = %s
WHERE
name = %s
"""
cur.execute(query, (password, name))
return (True,)
@with_psql
def update_password(cur, name, password):
query = """
UPDATE users SET
password = %s
WHERE
name = %s
"""
cur.execute(query, (password, name))
return (True,)
@staticmethod
@with_psql
def get(cur, name, password):
query = """
SELECT * FROM
users
WHERE
name = %s
"""
cur.execute(query, (name,))
user = cur.fetchall()[0]
if user[1].encode('utf-8') == bcrypt.hashpw(password, user[1].encode('utf-8')):
return (True, user)
else:
return (False, 'Password or username do not match')
@with_psql
def get(cur, name, password):
query = """
SELECT * FROM
users
WHERE
name = %s
"""
cur.execute(query, (name,))
user = cur.fetchall()[0]
if user[1].encode('utf-8') == bcrypt.hashpw(password, user[1].encode('utf-8')):
return (True, user)
else:
return (False, 'Password or username do not match')
No preview for this file type
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
import psycopg2
import bcrypt
import misc
import dao.user.user as ud
import dao.application.application as ad
import dao.device.device as dd
import dao.pend.pend as pend
import dao.data.data as data
import binascii
import misc
import os
......@@ -44,8 +45,7 @@ def signup():
feedback = 'Username or password fields cannot be empty'
return render_template('signup.html', feedback=feedback)
else:
uh = ud.UserDao()
res = uh.create(username, password)
res = ud.create(username, password)
if (not res[0]):
return render_template('signup.html', feedback=res[1])
else:
......@@ -67,8 +67,7 @@ def login():
feedback = 'Username or password fields cannot be empty'
return render_template('login.html', feedback=feedback)
else:
uh = ud.UserDao()
res = uh.get(username, password)
res = ud.get(username, password)
if (not res[0]):
return render_template('login.html', feedback=msg[1])
else:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment