Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Vladislav Rykov
THSO.server
Commits
6220f3d4
Commit
6220f3d4
authored
Apr 17, 2020
by
Vladislav Rykov
Browse files
data,pend & user dao changed
parent
d173fa12
Changes
5
Hide whitespace changes
Inline
Side-by-side
dao/data/data.pyc
View file @
6220f3d4
No preview for this file type
dao/pend/pend.pyc
View file @
6220f3d4
No preview for this file type
dao/user/user.py
View file @
6220f3d4
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'
)
dao/user/user.pyc
View file @
6220f3d4
No preview for this file type
server.py
View file @
6220f3d4
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
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment