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 import bcrypt
class UserDao: @with_psql
def create(cur, name, password):
def __init__(self): query = """
pass INSERT INTO
users
# decorator implementation VALUES
def with_psql(f): (%s, %s)
def _with_psql(*args, **kwargs): """
conn = psycopg2.connect('dbname=gateway') cur.execute(query, (name, bcrypt.hashpw(password, bcrypt.gensalt())))
cur = conn.cursor() return (True,)
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,)
@staticmethod @with_psql
@with_psql def delete(cur, name):
def delete(cur, name): query = """
query = """ DELETE FROM
DELETE FROM users
users WHERE
WHERE name = %s
name = %s """
""" cur.execute(query, (name,))
cur.execute(query, (name,)) return (True,)
return (True,)
@staticmethod @with_psql
@with_psql def update_name(cur, old_name, new_name):
def update_name(cur, old_name, new_name): query = """
query = """ UPDATE users SET
UPDATE users SET name = %s
name = %s WHERE
WHERE name = %s
name = %s """
""" cur.execute(query, (new_name,))
cur.execute(query, (new_name,)) return (True,)
return (True,)
@staticmethod @with_psql
@with_psql def update_password(cur, name, password):
def update_password(cur, name, password): query = """
query = """ UPDATE users SET
UPDATE users SET password = %s
password = %s WHERE
WHERE name = %s
name = %s """
""" cur.execute(query, (password, name))
cur.execute(query, (password, name)) return (True,)
return (True,)
@staticmethod @with_psql
@with_psql def get(cur, name, password):
def get(cur, name, password): query = """
query = """ SELECT * FROM
SELECT * FROM users
users WHERE
WHERE name = %s
name = %s """
""" cur.execute(query, (name,))
cur.execute(query, (name,)) user = cur.fetchall()[0]
user = cur.fetchall()[0]
if user[1].encode('utf-8') == bcrypt.hashpw(password, user[1].encode('utf-8')):
if user[1].encode('utf-8') == bcrypt.hashpw(password, user[1].encode('utf-8')): return (True, user)
return (True, user) else:
else: return (False, 'Password or username do not match')
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 from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
import psycopg2 import psycopg2
import bcrypt
import misc
import dao.user.user as ud import dao.user.user as ud
import dao.application.application as ad import dao.application.application as ad
import dao.device.device as dd import dao.device.device as dd
import dao.pend.pend as pend import dao.pend.pend as pend
import dao.data.data as data import dao.data.data as data
import binascii
import misc
import os import os
...@@ -44,8 +45,7 @@ def signup(): ...@@ -44,8 +45,7 @@ def signup():
feedback = 'Username or password fields cannot be empty' feedback = 'Username or password fields cannot be empty'
return render_template('signup.html', feedback=feedback) return render_template('signup.html', feedback=feedback)
else: else:
uh = ud.UserDao() res = ud.create(username, password)
res = uh.create(username, password)
if (not res[0]): if (not res[0]):
return render_template('signup.html', feedback=res[1]) return render_template('signup.html', feedback=res[1])
else: else:
...@@ -67,8 +67,7 @@ def login(): ...@@ -67,8 +67,7 @@ def login():
feedback = 'Username or password fields cannot be empty' feedback = 'Username or password fields cannot be empty'
return render_template('login.html', feedback=feedback) return render_template('login.html', feedback=feedback)
else: else:
uh = ud.UserDao() res = ud.get(username, password)
res = uh.get(username, password)
if (not res[0]): if (not res[0]):
return render_template('login.html', feedback=msg[1]) return render_template('login.html', feedback=msg[1])
else: 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