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
from
misc
import
with_psql
import
bcrypt
@
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
:
@
with_psql
def
delete
(
cur
,
appkey
):
def
__init__
(
self
):
query
=
"""
pass
DELETE FROM
applications
# decorator implementation
WHERE
def
with_psql
(
f
):
app_key = %s
def
_with_psql
(
*
args
,
**
kwargs
):
"""
conn
=
psycopg2
.
connect
(
'dbname=gateway'
)
cur
.
execute
(
query
,
(
appkey
,))
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
,))
return
(
True
,)
return
(
True
,)
@
staticmethod
@
with_psql
@
with_psql
def
get
(
cur
,
appkey
):
def
get
(
cur
,
appkey
):
query
=
"""
query
=
"""
SELECT * FROM
SELECT * FROM
applications
applications
WHERE
WHERE
app_key = %s
app_key = %s
"""
"""
cur
.
execute
(
query
,
(
appkey
,))
cur
.
execute
(
query
,
(
appkey
,))
app
=
cur
.
fetchone
()
app
=
cur
.
fetchone
()
if
app
is
None
:
if
app
is
None
:
return
(
False
,
'Application with key {} does not exist'
.
format
(
appkey
))
return
(
False
,
'Application with key {} does not exist'
.
format
(
appkey
))
else
:
else
:
return
(
True
,
app
)
return
(
True
,
app
)
@
staticmethod
@
with_psql
@
with_psql
def
get_list
(
cur
,
username
):
def
get_list
(
cur
,
username
):
query
=
"""
query
=
"""
SELECT * FROM
SELECT * FROM
applications
applications
WHERE
WHERE
username = %s
username = %s
"""
"""
cur
.
execute
(
query
,
(
username
,))
cur
.
execute
(
query
,
(
username
,))
return
(
True
,
cur
.
fetchall
())
return
(
True
,
cur
.
fetchall
())
@
staticmethod
@
with_psql
@
with_psql
def
update
(
cur
,
appkey
,
name
,
desc
):
def
update
(
cur
,
appkey
,
name
,
desc
):
query
=
"""
query
=
"""
UPDATE
UPDATE
applications
applications
SET
SET
name = %s,
name = %s,
description = %s,
description = %s,
WHERE
WHERE
app_key = %s
app_key = %s
"""
"""
cur
.
execute
(
query
,
(
name
,
desc
,
appkey
))
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):
...
@@ -39,3 +39,25 @@ def prep_id_range(devlist):
s
+=
str
(
r
[
-
1
])
+
']'
s
+=
str
(
r
[
-
1
])
+
']'
return
s
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/')
...
@@ -19,8 +19,7 @@ server = Flask(__name__, template_folder='templates/')
@
server
.
route
(
'/'
)
@
server
.
route
(
'/'
)
def
index
():
def
index
():
if
'name'
in
session
and
len
(
session
[
'name'
])
>
0
:
if
'name'
in
session
and
len
(
session
[
'name'
])
>
0
:
ah
=
ad
.
ApplicationDao
()
apps
=
ad
.
get_list
(
session
[
'name'
].
encode
(
'utf-8'
))
apps
=
ah
.
get_list
(
session
[
'name'
].
encode
(
'utf-8'
))
session
.
pop
(
'appkey'
,
None
)
session
.
pop
(
'appkey'
,
None
)
# print('apps: ', apps)
# print('apps: ', apps)
...
@@ -98,13 +97,12 @@ def new_application():
...
@@ -98,13 +97,12 @@ def new_application():
@
server
.
route
(
'/app'
,
methods
=
[
'GET'
,
'POST'
])
@
server
.
route
(
'/app'
,
methods
=
[
'GET'
,
'POST'
])
def
app
():
def
app
():
if
'name'
in
session
:
if
'name'
in
session
:
ah
=
ad
.
ApplicationDao
()
if
request
.
method
==
'GET'
:
if
request
.
method
==
'GET'
:
dh
=
dd
.
DeviceDao
()
dh
=
dd
.
DeviceDao
()
session
[
'appkey'
]
=
request
.
args
.
get
(
'appkey'
)
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
])
devs
=
dh
.
get_list
(
app
[
1
][
1
])
try
:
try
:
...
@@ -122,7 +120,7 @@ def app():
...
@@ -122,7 +120,7 @@ def app():
return
render_template
(
'new-app.html'
,
feedback
=
error
)
return
render_template
(
'new-app.html'
,
feedback
=
error
)
else
:
else
:
appkey
=
misc
.
rand_str
(
APP_KEY_LEN
)
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
]:
if
not
res
[
0
]:
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
...
@@ -131,7 +129,7 @@ def app():
...
@@ -131,7 +129,7 @@ def app():
res
=
dh
.
create_table
(
appkey
)
res
=
dh
.
create_table
(
appkey
)
if
not
res
[
0
]:
if
not
res
[
0
]:
a
h
.
delete
(
appkey
)
a
d
.
delete
(
appkey
)
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
))
...
@@ -149,8 +147,7 @@ def delete_app():
...
@@ -149,8 +147,7 @@ def delete_app():
dh
.
delete_table
(
session
[
'appkey'
])
dh
.
delete_table
(
session
[
'appkey'
])
ah
=
ad
.
ApplicationDao
()
res
=
ad
.
delete
(
session
[
'appkey'
])
res
=
ah
.
delete
(
session
[
'appkey'
])
if
not
res
[
0
]:
if
not
res
[
0
]:
return
redirect
(
url_for
(
'app'
))
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