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
1067f8e8
Commit
1067f8e8
authored
Apr 17, 2020
by
Vladislav Rykov
Browse files
device dao changed
parent
6b335653
Changes
3
Show whitespace changes
Inline
Side-by-side
dao/device/device.py
View file @
1067f8e8
import
psycopg2
from
psycopg2
import
sql
import
bcrypt
from
misc
import
with_psql
class
DeviceDao
:
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_datatable
(
cur
,
appkey
,
dev_id
):
@
with_psql
def
create_datatable
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
sql
.
SQL
(
...
...
@@ -43,9 +15,8 @@ class DeviceDao:
return
(
True
,)
@
staticmethod
@
with_psql
def
delete_datatable
(
cur
,
appkey
,
dev_id
):
@
with_psql
def
delete_datatable
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
...
...
@@ -53,9 +24,8 @@ class DeviceDao:
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
create_table
(
cur
,
appkey
):
@
with_psql
def
create_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
sql
.
SQL
(
...
...
@@ -68,9 +38,8 @@ class DeviceDao:
return
(
True
,)
@
staticmethod
@
with_psql
def
delete_table
(
cur
,
appkey
):
@
with_psql
def
delete_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
...
...
@@ -78,12 +47,8 @@ class DeviceDao:
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
create
(
cur
,
name
,
dev_id
,
appkey
,
desc
):
@
with_psql
def
create
(
cur
,
name
,
dev_id
,
appkey
,
desc
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
INSERT INTO
...
...
@@ -96,9 +61,8 @@ class DeviceDao:
return
(
True
,)
@
staticmethod
@
with_psql
def
delete
(
cur
,
appkey
,
dev_id
):
@
with_psql
def
delete
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
DELETE FROM
...
...
@@ -111,9 +75,8 @@ class DeviceDao:
return
(
True
,)
@
staticmethod
@
with_psql
def
get
(
cur
,
appkey
,
dev_id
):
@
with_psql
def
get
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
...
...
@@ -131,9 +94,8 @@ class DeviceDao:
return
(
True
,
dev
)
@
staticmethod
@
with_psql
def
get_list
(
cur
,
appkey
):
@
with_psql
def
get_list
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
...
...
dao/device/device.pyc
View file @
1067f8e8
No preview for this file type
server.py
View file @
1067f8e8
...
...
@@ -98,12 +98,11 @@ def new_application():
def
app
():
if
'name'
in
session
:
if
request
.
method
==
'GET'
:
dh
=
dd
.
DeviceDao
()
session
[
'appkey'
]
=
request
.
args
.
get
(
'appkey'
)
app
=
ad
.
get
(
session
[
'appkey'
])
devs
=
d
h
.
get_list
(
app
[
1
][
1
])
devs
=
d
d
.
get_list
(
app
[
1
][
1
])
try
:
filelist
=
[
f
for
f
in
os
.
listdir
(
DATA_DOWNLOAD_DIR
)
if
f
.
startswith
(
session
[
'appkey'
])]
...
...
@@ -125,8 +124,7 @@ def app():
if
not
res
[
0
]:
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
dh
=
dd
.
DeviceDao
()
res
=
dh
.
create_table
(
appkey
)
res
=
dd
.
create_table
(
appkey
)
if
not
res
[
0
]:
ad
.
delete
(
appkey
)
...
...
@@ -139,13 +137,12 @@ def app():
@
server
.
route
(
'/delete-app'
)
def
delete_app
():
if
'name'
in
session
:
dh
=
dd
.
DeviceDao
()
devs
=
dh
.
get_list
(
session
[
'appkey'
])
devs
=
dd
.
get_list
(
session
[
'appkey'
])
for
dev
in
devs
[
1
]:
data
.
delete_table
(
session
[
'appkey'
],
dev
[
1
])
d
h
.
delete_table
(
session
[
'appkey'
])
d
d
.
delete_table
(
session
[
'appkey'
])
res
=
ad
.
delete
(
session
[
'appkey'
])
...
...
@@ -160,8 +157,7 @@ def delete_app():
@
server
.
route
(
'/add-dev'
)
def
new_dev
():
if
'name'
in
session
:
dh
=
dd
.
DeviceDao
()
dev_list
=
dh
.
get_list
(
session
[
'appkey'
])
dev_list
=
dd
.
get_list
(
session
[
'appkey'
])
#print('dev list : ', dev_list)
...
...
@@ -177,9 +173,8 @@ def new_dev():
@
server
.
route
(
'/dev'
,
methods
=
[
'GET'
,
'POST'
])
def
dev
():
if
'name'
in
session
:
dh
=
dd
.
DeviceDao
()
if
request
.
method
==
'GET'
:
dev
=
d
h
.
get
(
session
[
'appkey'
],
request
.
args
.
get
(
'id'
))
dev
=
d
d
.
get
(
session
[
'appkey'
],
request
.
args
.
get
(
'id'
))
session
[
'devid'
]
=
dev
[
1
][
1
]
session
[
'devname'
]
=
dev
[
1
][
0
]
...
...
@@ -193,7 +188,7 @@ def dev():
return
render_template
(
'dev.html'
,
dev
=
dev
[
1
],
appkey
=
session
[
'appkey'
],
ltup
=
ltup
)
else
:
res
=
d
h
.
create
(
request
.
form
[
'devname'
],
request
.
form
[
'devid'
],
session
[
'appkey'
],
request
.
form
[
'devdesc'
])
res
=
d
d
.
create
(
request
.
form
[
'devname'
],
request
.
form
[
'devid'
],
session
[
'appkey'
],
request
.
form
[
'devdesc'
])
if
not
res
[
0
]:
return
render_template
(
'add-dev.html'
,
feedback
=
res
[
1
])
...
...
@@ -201,7 +196,7 @@ def dev():
res
=
data
.
create_table
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
if
not
res
[
0
]:
d
h
.
delete
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
d
d
.
delete
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
return
render_template
(
'add-dev.html'
,
feedback
=
res
[
1
])
else
:
return
redirect
(
url_for
(
'app'
,
appkey
=
session
[
'appkey'
]))
...
...
@@ -244,9 +239,8 @@ def dev_conf():
@
server
.
route
(
'/delete-dev'
)
def
delete_dev
():
if
'name'
in
session
and
'devid'
in
session
:
dh
=
dd
.
DeviceDao
()
data
.
delete_table
(
session
[
'appkey'
],
session
[
'devid'
])
res
=
d
h
.
delete
(
session
[
'appkey'
],
session
[
'devid'
])
res
=
d
d
.
delete
(
session
[
'appkey'
],
session
[
'devid'
])
return
redirect
(
url_for
(
'app'
,
appkey
=
session
[
'appkey'
]))
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