Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
co2ampel2
Web
Commits
398db1f0
Commit
398db1f0
authored
1 month ago
by
Patrick Ade
Browse files
Options
Download
Email Patches
Plain Diff
added comment
parent
4c761710
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/app/views.py
+12
-19
backend/app/views.py
with
12 additions
and
19 deletions
+12
-19
backend/app/views.py
+
12
-
19
View file @
398db1f0
...
@@ -13,6 +13,8 @@ from django.views.decorators.csrf import csrf_exempt
...
@@ -13,6 +13,8 @@ from django.views.decorators.csrf import csrf_exempt
from
django.contrib.auth
import
authenticate
,
login
,
logout
from
django.contrib.auth
import
authenticate
,
login
,
logout
from
.forms
import
CreateUserForm
from
.forms
import
CreateUserForm
"""old functions not used by django ninja"""
@
ensure_csrf_cookie
@
ensure_csrf_cookie
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
...
@@ -30,18 +32,14 @@ def login_view(request):
...
@@ -30,18 +32,14 @@ def login_view(request):
email
=
data
[
"email"
]
email
=
data
[
"email"
]
password
=
data
[
"password"
]
password
=
data
[
"password"
]
except
json
.
JSONDecodeError
:
except
json
.
JSONDecodeError
:
return
JsonResponse
(
return
JsonResponse
({
"success"
:
False
,
"message"
:
"Invalid JSON"
},
status
=
400
)
{
"success"
:
False
,
"message"
:
"Invalid JSON"
},
status
=
400
)
user
=
authenticate
(
request
,
username
=
email
,
password
=
password
)
user
=
authenticate
(
request
,
username
=
email
,
password
=
password
)
if
user
:
if
user
:
login
(
request
,
user
)
# also creates a session in the browser
login
(
request
,
user
)
# also creates a session in the browser
return
JsonResponse
({
"success"
:
True
})
return
JsonResponse
({
"success"
:
True
})
return
JsonResponse
(
return
JsonResponse
({
"success"
:
False
,
"message"
:
"Invalid credentials"
},
status
=
401
)
{
"success"
:
False
,
"message"
:
"Invalid credentials"
},
status
=
401
)
def
logout_view
(
request
):
def
logout_view
(
request
):
...
@@ -52,9 +50,7 @@ def logout_view(request):
...
@@ -52,9 +50,7 @@ def logout_view(request):
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
def
user
(
request
):
def
user
(
request
):
if
request
.
user
.
is_authenticated
:
if
request
.
user
.
is_authenticated
:
return
JsonResponse
(
return
JsonResponse
({
"username"
:
request
.
user
.
username
,
"email"
:
request
.
user
.
email
})
{
"username"
:
request
.
user
.
username
,
"email"
:
request
.
user
.
email
}
)
return
JsonResponse
({
"message"
:
"Not logged in"
},
status
=
401
)
return
JsonResponse
({
"message"
:
"Not logged in"
},
status
=
401
)
...
@@ -64,9 +60,7 @@ def register(request):
...
@@ -64,9 +60,7 @@ def register(request):
form
=
CreateUserForm
(
data
)
form
=
CreateUserForm
(
data
)
if
form
.
is_valid
():
if
form
.
is_valid
():
form
.
save
()
form
.
save
()
return
JsonResponse
(
return
JsonResponse
({
"success"
:
"User registered successfully"
},
status
=
201
)
{
"success"
:
"User registered successfully"
},
status
=
201
)
else
:
else
:
errors
=
form
.
errors
.
as_json
()
errors
=
form
.
errors
.
as_json
()
return
JsonResponse
({
"error"
:
errors
},
status
=
400
)
return
JsonResponse
({
"error"
:
errors
},
status
=
400
)
...
@@ -80,9 +74,7 @@ def room_data_range(request):
...
@@ -80,9 +74,7 @@ def room_data_range(request):
stop
=
request
.
GET
.
get
(
"stop"
,
"now()"
)
stop
=
request
.
GET
.
get
(
"stop"
,
"now()"
)
if
not
room
:
if
not
room
:
return
JsonResponse
(
return
JsonResponse
({
"error"
:
"Missing 'room' parameter"
},
status
=
400
)
{
"error"
:
"Missing 'room' parameter"
},
status
=
400
)
load_dotenv
()
load_dotenv
()
client
=
InfluxDBHelper
(
client
=
InfluxDBHelper
(
...
@@ -108,9 +100,7 @@ def room_data_range(request):
...
@@ -108,9 +100,7 @@ def room_data_range(request):
return
JsonResponse
({
"room"
:
room
,
"data"
:
data_by_time
},
status
=
200
)
return
JsonResponse
({
"room"
:
room
,
"data"
:
data_by_time
},
status
=
200
)
except
json
.
JSONDecodeError
:
except
json
.
JSONDecodeError
:
return
JsonResponse
(
return
JsonResponse
({
"success"
:
False
,
"message"
:
"Invalid JSON"
},
status
=
400
)
{
"success"
:
False
,
"message"
:
"Invalid JSON"
},
status
=
400
)
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
...
@@ -130,6 +120,7 @@ def get_rooms(request):
...
@@ -130,6 +120,7 @@ def get_rooms(request):
return
JsonResponse
({
"rooms"
:
sorted
(
rooms
)})
return
JsonResponse
({
"rooms"
:
sorted
(
rooms
)})
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
def
get_rooms_from_building
(
request
):
def
get_rooms_from_building
(
request
):
building
=
request
.
GET
.
get
(
"building"
)
building
=
request
.
GET
.
get
(
"building"
)
...
@@ -155,6 +146,7 @@ def get_rooms_from_building(request):
...
@@ -155,6 +146,7 @@ def get_rooms_from_building(request):
return
JsonResponse
({
"building"
:
building
,
"rooms"
:
sorted
(
rooms
)})
return
JsonResponse
({
"building"
:
building
,
"rooms"
:
sorted
(
rooms
)})
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
def
get_bau
(
request
):
def
get_bau
(
request
):
client
=
InfluxDBHelper
(
client
=
InfluxDBHelper
(
...
@@ -176,6 +168,7 @@ def get_bau(request):
...
@@ -176,6 +168,7 @@ def get_bau(request):
return
JsonResponse
({
"baus"
:
sorted
(
baus
)})
return
JsonResponse
({
"baus"
:
sorted
(
baus
)})
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
def
room_data_csv_view
(
request
):
def
room_data_csv_view
(
request
):
room
=
request
.
GET
.
get
(
"room"
)
room
=
request
.
GET
.
get
(
"room"
)
...
@@ -234,6 +227,7 @@ def room_data_csv_download(request):
...
@@ -234,6 +227,7 @@ def room_data_csv_download(request):
response
[
"Content-Disposition"
]
=
f
'attachment; filename="
{
room
}
_data.csv"'
response
[
"Content-Disposition"
]
=
f
'attachment; filename="
{
room
}
_data.csv"'
return
response
return
response
@
require_http_methods
([
"GET"
])
@
require_http_methods
([
"GET"
])
def
room_availability
(
request
):
def
room_availability
(
request
):
room
=
request
.
GET
.
get
(
"room"
)
room
=
request
.
GET
.
get
(
"room"
)
...
@@ -308,4 +302,3 @@ def all_sensors_availability(request):
...
@@ -308,4 +302,3 @@ def all_sensors_availability(request):
sensors_available
=
client
.
sensors_with_available_data
(
start
,
stop
)
sensors_available
=
client
.
sensors_with_available_data
(
start
,
stop
)
return
JsonResponse
({
"available_sensors"
:
sensors_available
},
status
=
200
)
return
JsonResponse
({
"available_sensors"
:
sensors_available
},
status
=
200
)
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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
Menu
Explore
Projects
Groups
Snippets