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
06100a6f
Commit
06100a6f
authored
Apr 09, 2020
by
Vladislav Rykov
Browse files
changed table management, tested
parent
5c990a0c
Changes
7
Show whitespace changes
Inline
Side-by-side
dao/application/application.py
View file @
06100a6f
import
psycopg2
import
bcrypt
from
misc
import
rand_str
APP_KEY_LEN
=
8
class
ApplicationDao
:
...
...
@@ -31,14 +29,14 @@ class ApplicationDao:
@
staticmethod
@
with_psql
def
create
(
cur
,
name
,
username
,
desc
):
def
create
(
cur
,
name
,
appkey
,
username
,
desc
):
query
=
"""
INSERT INTO
applications
VALUES
(%s, %s, %s, %s)
"""
cur
.
execute
(
query
,
(
name
,
rand_str
(
APP_KEY_LEN
)
,
username
,
desc
))
cur
.
execute
(
query
,
(
name
,
appkey
,
username
,
desc
))
return
(
True
,)
...
...
dao/application/application.pyc
View file @
06100a6f
No preview for this file type
dao/device/device.py
View file @
06100a6f
import
psycopg2
from
psycopg2
import
sql
import
bcrypt
...
...
@@ -27,46 +28,100 @@ class DeviceDao:
return
res
return
_with_psql
@
staticmethod
@
with_psql
def
create_datatable
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
sql
.
SQL
(
"""CREATE TABLE {} (
utc NUMERIC(10) NOT NULL,
data json NOT NULL
)"""
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
delete_datatable
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
"DROP TABLE {}"
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
create_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
sql
.
SQL
(
"""CREATE TABLE {} (
name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY,
description VARCHAR(200)
)"""
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
delete_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
"DROP TABLE {}"
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
create
(
cur
,
name
,
dev_id
,
appkey
,
desc
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
INSERT INTO
devices
{}
VALUES
(%s, %s,
%s,
%s)
(%s, %s, %s)
"""
cur
.
execute
(
query
,
(
name
,
dev_id
,
appkey
,
desc
))
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
name
,
dev_id
,
desc
])
return
(
True
,)
@
staticmethod
@
with_psql
def
delete
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
DELETE FROM
devices
{}
WHERE
app_key = %s
AND
dev_id = %s
"""
cur
.
execute
(
query
,
(
appkey
,
dev_id
))
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
dev_id
])
return
(
True
,)
@
staticmethod
@
with_psql
def
get
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
devices
{}
WHERE
app_key = %s
AND
dev_id = %s
"""
cur
.
execute
(
query
,
(
appkey
,
dev_id
))
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
dev_id
])
dev
=
cur
.
fetchone
()
if
(
dev
is
None
):
...
...
@@ -78,12 +133,12 @@ class DeviceDao:
@
staticmethod
@
with_psql
def
get_list
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
devices
WHERE
app_key = %s
{}
"""
cur
.
execute
(
query
,
(
appkey
,))
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,
cur
.
fetchall
())
dao/device/device.pyc
View file @
06100a6f
No preview for this file type
server.py
View file @
06100a6f
...
...
@@ -6,6 +6,7 @@ import dao.user.user as ud
import
dao.application.application
as
ad
import
dao.device.device
as
dd
APP_KEY_LEN
=
8
server
=
Flask
(
__name__
,
template_folder
=
'templates/'
)
...
...
@@ -109,21 +110,38 @@ def app():
error
=
'Application name cannot be empty.'
return
render_template
(
'new-app.html'
,
feedback
=
error
)
else
:
res
=
ah
.
create
(
request
.
form
[
'appname'
],
session
[
'name'
],
request
.
form
[
'appdesc'
])
appkey
=
misc
.
rand_str
(
APP_KEY_LEN
)
res
=
ah
.
create
(
request
.
form
[
'appname'
],
appkey
,
session
[
'name'
],
request
.
form
[
'appdesc'
])
if
not
res
[
0
]:
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
#res = new_app_devs(request.form['appname'])
#if not res[0]:
# rm_app(request.form['appname'])
# return render_template('new-app.html', feedback=res[1])
dh
=
dd
.
DeviceDao
()
res
=
dh
.
create_table
(
appkey
)
if
not
res
[
0
]:
ah
.
delete
(
appkey
)
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
#if not res[0] or not rer[0]:
# return render_template('new-app.html', feedback=str(res[1])+'|'+str(rer[1]))
#else:
return
redirect
(
url_for
(
'index'
))
@
server
.
route
(
'/delete-app'
)
def
delete_app
():
dh
=
dd
.
DeviceDao
()
devs
=
dh
.
get_list
(
session
[
'appkey'
])
for
dev
in
devs
[
1
]:
dh
.
delete_datatable
(
session
[
'appkey'
],
dev
[
1
])
dh
.
delete_table
(
session
[
'appkey'
])
ah
=
ad
.
ApplicationDao
()
res
=
ah
.
delete
(
session
[
'appkey'
])
if
not
res
[
0
]:
return
redirect
(
url_for
(
'app'
))
else
:
return
redirect
(
url_for
(
'index'
))
@
server
.
route
(
'/add-dev'
)
def
new_dev
():
...
...
@@ -145,15 +163,21 @@ def new_dev():
def
dev
():
dh
=
dd
.
DeviceDao
()
if
request
.
method
==
'GET'
:
dev
=
dh
.
get
(
session
[
'appkey'
],
request
.
args
.
get
(
'
dev_
id'
))
dev
=
dh
.
get
(
session
[
'appkey'
],
request
.
args
.
get
(
'id'
))
ltup
=
'recently'
return
render_template
(
'dev.html'
,
dev
=
dev
[
1
],
ltup
=
ltup
)
return
render_template
(
'dev.html'
,
dev
=
dev
[
1
],
appkey
=
session
[
'appkey'
],
ltup
=
ltup
)
else
:
res
=
dh
.
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
])
else
:
res
=
dh
.
create_datatable
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
if
not
res
[
0
]:
dh
.
delete
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
return
render_template
(
'add-dev.html'
,
feedback
=
res
[
1
])
else
:
return
redirect
(
url_for
(
'app'
))
...
...
templates/app.html
View file @
06100a6f
...
...
@@ -14,7 +14,7 @@
{% if devs %}
<ol>
{% for dev in devs %}
<li><a
href=
"/dev?
dev_
id={{ dev[1] }}"
>
{{ dev[0] }}
</a></li>
<li><a
href=
"/dev?id={{ dev[1] }}"
>
{{ dev[0] }}
</a></li>
{% endfor %}
</ol>
{% else %}
...
...
templates/dev.html
View file @
06100a6f
...
...
@@ -8,8 +8,8 @@
<div
class=
"col-md-6 col-md-offset-3"
>
<p>
Name: {{ dev[0] }}
</p>
<p>
dev_id: {{ dev[1] }}
</p>
<p>
app-key: {{
dev[2]
}}
</p>
<p>
Description : {{ dev[
3
] }}
</p>
<p>
app-key: {{
appkey
}}
</p>
<p>
Description : {{ dev[
2
] }}
</p>
<p>
Last time up : {{ ltup }}
</p>
<a
href=
"/data"
><button
type=
"submit"
class=
"btn btn-primary"
>
Data
</button></a>
...
...
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