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
6b335653
Commit
6b335653
authored
Apr 17, 2020
by
Vladislav Rykov
Browse files
app dao changed
parent
4040f3f9
Changes
5
Hide whitespace changes
Inline
Side-by-side
dao/application/application.py
View file @
6b335653
import
psycopg2
import
bcrypt
from
misc
import
with_psql
@
with_psql
def
create
(
cur
,
name
,
appkey
,
username
,
desc
):
query
=
"""
INSERT INTO
applications
VALUES
(%s, %s, %s, %s)
"""
cur
.
execute
(
query
,
(
name
,
appkey
,
username
,
desc
))
return
(
True
,)
class
ApplicationDao
:
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
,
appkey
,
username
,
desc
):
query
=
"""
INSERT INTO
applications
VALUES
(%s, %s, %s, %s)
"""
cur
.
execute
(
query
,
(
name
,
appkey
,
username
,
desc
))
return
(
True
,)
@
staticmethod
@
with_psql
def
delete
(
cur
,
appkey
):
query
=
"""
DELETE FROM
applications
WHERE
app_key = %s
"""
cur
.
execute
(
query
,
(
appkey
,))
@
with_psql
def
delete
(
cur
,
appkey
):
query
=
"""
DELETE FROM
applications
WHERE
app_key = %s
"""
cur
.
execute
(
query
,
(
appkey
,))
return
(
True
,)
return
(
True
,)
@
staticmethod
@
with_psql
def
get
(
cur
,
appkey
):
query
=
"""
SELECT * FROM
applications
WHERE
app_key = %s
"""
cur
.
execute
(
query
,
(
appkey
,))
app
=
cur
.
fetchone
()
@
with_psql
def
get
(
cur
,
appkey
):
query
=
"""
SELECT * FROM
applications
WHERE
app_key = %s
"""
cur
.
execute
(
query
,
(
appkey
,))
app
=
cur
.
fetchone
()
if
app
is
None
:
return
(
False
,
'Application with key {} does not exist'
.
format
(
appkey
))
else
:
return
(
True
,
app
)
if
app
is
None
:
return
(
False
,
'Application with key {} does not exist'
.
format
(
appkey
))
else
:
return
(
True
,
app
)
@
staticmethod
@
with_psql
def
get_list
(
cur
,
username
):
query
=
"""
SELECT * FROM
applications
WHERE
username = %s
"""
cur
.
execute
(
query
,
(
username
,))
@
with_psql
def
get_list
(
cur
,
username
):
query
=
"""
SELECT * FROM
applications
WHERE
username = %s
"""
cur
.
execute
(
query
,
(
username
,))
return
(
True
,
cur
.
fetchall
())
return
(
True
,
cur
.
fetchall
())
@
staticmethod
@
with_psql
def
update
(
cur
,
appkey
,
name
,
desc
):
query
=
"""
UPDATE
applications
SET
name = %s,
description = %s,
WHERE
app_key = %s
"""
cur
.
execute
(
query
,
(
name
,
desc
,
appkey
))
@
with_psql
def
update
(
cur
,
appkey
,
name
,
desc
):
query
=
"""
UPDATE
applications
SET
name = %s,
description = %s,
WHERE
app_key = %s
"""
cur
.
execute
(
query
,
(
name
,
desc
,
appkey
))
return
(
True
,)
return
(
True
,)
dao/application/application.pyc
View file @
6b335653
No preview for this file type
misc.py
View file @
6b335653
...
...
@@ -39,3 +39,25 @@ def prep_id_range(devlist):
s
+=
str
(
r
[
-
1
])
+
']'
return
s
import
psycopg2
# 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
misc.pyc
View file @
6b335653
No preview for this file type
server.py
View file @
6b335653
...
...
@@ -19,8 +19,7 @@ server = Flask(__name__, template_folder='templates/')
@
server
.
route
(
'/'
)
def
index
():
if
'name'
in
session
and
len
(
session
[
'name'
])
>
0
:
ah
=
ad
.
ApplicationDao
()
apps
=
ah
.
get_list
(
session
[
'name'
].
encode
(
'utf-8'
))
apps
=
ad
.
get_list
(
session
[
'name'
].
encode
(
'utf-8'
))
session
.
pop
(
'appkey'
,
None
)
# print('apps: ', apps)
...
...
@@ -98,13 +97,12 @@ def new_application():
@
server
.
route
(
'/app'
,
methods
=
[
'GET'
,
'POST'
])
def
app
():
if
'name'
in
session
:
ah
=
ad
.
ApplicationDao
()
if
request
.
method
==
'GET'
:
dh
=
dd
.
DeviceDao
()
session
[
'appkey'
]
=
request
.
args
.
get
(
'appkey'
)
app
=
a
h
.
get
(
session
[
'appkey'
])
app
=
a
d
.
get
(
session
[
'appkey'
])
devs
=
dh
.
get_list
(
app
[
1
][
1
])
try
:
...
...
@@ -122,7 +120,7 @@ def app():
return
render_template
(
'new-app.html'
,
feedback
=
error
)
else
:
appkey
=
misc
.
rand_str
(
APP_KEY_LEN
)
res
=
a
h
.
create
(
request
.
form
[
'appname'
],
appkey
,
session
[
'name'
],
request
.
form
[
'appdesc'
])
res
=
a
d
.
create
(
request
.
form
[
'appname'
],
appkey
,
session
[
'name'
],
request
.
form
[
'appdesc'
])
if
not
res
[
0
]:
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
...
...
@@ -131,7 +129,7 @@ def app():
res
=
dh
.
create_table
(
appkey
)
if
not
res
[
0
]:
a
h
.
delete
(
appkey
)
a
d
.
delete
(
appkey
)
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
return
redirect
(
url_for
(
'index'
))
...
...
@@ -149,8 +147,7 @@ def delete_app():
dh
.
delete_table
(
session
[
'appkey'
])
ah
=
ad
.
ApplicationDao
()
res
=
ah
.
delete
(
session
[
'appkey'
])
res
=
ad
.
delete
(
session
[
'appkey'
])
if
not
res
[
0
]:
return
redirect
(
url_for
(
'app'
))
...
...
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