-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathviews.py
48 lines (43 loc) · 1.84 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from django.shortcuts import render
from django.views.generic import TemplateView
from django.http import JsonResponse, QueryDict
from django.utils.translation import ugettext as _
from .models import Config
from .nab2mqttd import Nab2MQTTd
from django.utils import translation
import datetime
class SettingsView(TemplateView):
template_name = "nab2mqttd/settings.html"
def get_context_data(self, **kwargs):
# on charge les donnees depuis la base de données
context = super().get_context_data(**kwargs)
context["config"] = Config.load()
return context
def post(self, request, *args, **kwargs):
# quand on reçoit une nouvelle config (via interface)
config = Config.load()
if "server" in request.POST:
config.server = request.POST["server"]
if "clientid" in request.POST:
config.clientid = request.POST["clientid"]
if "username" in request.POST:
config.username = request.POST["username"]
if "password" in request.POST:
config.password = request.POST["password"]
if "port" in request.POST:
config.port = request.POST["port"]
if "tls" in request.POST:
config.tls = request.POST["tls"]
if "tlsinsecure" in request.POST:
config.tlsinsecure = request.POST["tlsinsecure"]
if "topic" in request.POST:
config.topic = request.POST["topic"]
config.save()
Nab2MQTTd.signal_daemon()
context = self.get_context_data(**kwargs)
return render(request, SettingsView.template_name, context=context)
def put(self, request, *args, **kwargs):
# quand on clique sur le bouton de l'intervaface pour jouer tout de suite
# restart the MQTT daemon
Nab2MQTTd.signal_daemon()
return JsonResponse({"status": "ok"})