/
views.py
1155 lines (948 loc) · 55.9 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pathlib import Path
from django.shortcuts import render
from django.contrib import messages
from django.shortcuts import redirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse, HttpResponse, FileResponse
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from constance import config # For the explicitly user-configurable stuff
from .decorators import site_is_configured, login_if_required_for_dashboard
from . import device_forms, profile_forms, beer_forms, setup_forms
from . import setup_views, mdnsLocator, almost_json, git_integration, connection_debug, udev_integration
import datetime, os, random, subprocess
import fermentrack_django.settings as settings
from app.models import BrewPiDevice, OldControlConstants, NewControlConstants, PinDevice, SensorDevice, BeerLogPoint, Beer, ExtendedSettings
from external_push.views import external_push_list
from django.contrib.auth.models import User
import environ
env = environ.Env()
def error_notifications(request):
if config.GIT_UPDATE_TYPE != "none" and settings.USE_DOCKER:
# Check the git status at least every 6 hours
now_time = timezone.now()
if config.LAST_GIT_CHECK < now_time - datetime.timedelta(hours=6):
try:
if git_integration.app_is_current():
config.LAST_GIT_CHECK = now_time
else:
messages.info(request, "This app is not at the latest version! " +
'<a href="/upgrade"">Upgrade from GitHub</a> to receive the latest version.')
except:
# If we can't check for the latest version info, skip and move on
pass
# config.LAST_GIT_CHECK = now_time - datetime.timedelta(hours=18)
# config.FIRMWARE_LIST_LAST_REFRESHED = now_time - datetime.timedelta(hours=72)
# if not config.ALLOW_GIT_BRANCH_SWITCHING:
# # Ths user is using one of the two "default" branches (dev or master). Make sure that the branch he/she is
# # actually using is the same as the one that he/she wanted.
#
# # Don't check if the user has custom branch switching though, as they should be allowed to pick whatever
# # branch he/she wants.
# # TODO - Fix the below once we eliminate docker-dev
# if settings.GIT_BRANCH != config.GIT_UPDATE_TYPE and (settings.GIT_BRANCH == 'docker-dev' and config.GIT_UPDATE_TYPE != 'dev'):
# if config.GIT_UPDATE_TYPE not in [x for x,_ in settings.CONSTANCE_ADDITIONAL_FIELDS['git_update_type_select'][1]['choices']]:
# # TODO - Fix this to pick up the default
# config.GIT_UPDATE_TYPE = "dev"
# else:
# messages.warning(request, "You selected to update from the {} code ".format(config.GIT_UPDATE_TYPE) +
# "branch, but you are currently using the {} branch. ".format(settings.GIT_BRANCH) +
# 'Click <a href="/upgrade">here</a> to update to the correct branch.')
django_env_version = env.int("ENV_DJANGO_VERSION", default=-1)
if django_env_version < 2:
msg = 'You are running an installation of Fermentrack that was installed using an old version of Fermentrack Tools. To fix this, please <a href="https://github.com/thorrak/fermentrack/issues/727">follow the instructions here</a>. Tilt functionality will not work until this is fixed, and other features may break in future updates if this is not resolved.'
messages.warning(request, msg)
# Siteroot is a lazy way of determining where to direct the user when they go to http://devicename.local/
def siteroot(request):
# In addition to requiring the site to be configured, we require that there be a user account. Due to the
# setup workflow, the user will generally be created before constance configuration takes place, but if
# the user account gets deleted (for example, in the admin) we want the user to go through that portion
# of account setup.
num_users = User.objects.all().count()
if not config.USER_HAS_COMPLETED_CONFIGURATION or num_users <= 0:
# If things aren't configured, redirect to the guided setup workflow
return redirect('setup_splash')
else:
# Notify the user of things like git being out of date, issues with SSH, etc.
error_notifications(request)
# The default screen is the "lcd list" screen
return render(request, template_name="siteroot.html")
# return device_lcd_list(request=request)
@login_required
@site_is_configured # Checks if the user completed constance configuration
def add_device(request):
# TODO - Add user permissioning
# if not request.user.has_perm('app.add_device'):
# messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
# return redirect("/")
if request.POST:
form = device_forms.BrewPiDeviceCreateForm(request.POST)
if form.is_valid():
# TODO - Add support for editing to this
new_device = BrewPiDevice(
device_name=form.cleaned_data['device_name'],
temp_format=form.cleaned_data['temp_format'],
data_point_log_interval=form.cleaned_data['data_point_log_interval'],
useInetSocket=form.cleaned_data['useInetSocket'],
socketPort=form.cleaned_data['socketPort'],
socketHost=form.cleaned_data['socketHost'],
serial_port=form.cleaned_data['serial_port'],
serial_alt_port=form.cleaned_data['serial_alt_port'],
board_type=form.cleaned_data['board_type'],
socket_name=form.cleaned_data['socket_name'],
connection_type=form.cleaned_data['connection_type'],
wifi_host=form.cleaned_data['wifi_host'],
wifi_port=form.cleaned_data['wifi_port'],
prefer_connecting_via_udev=form.cleaned_data['prefer_connecting_via_udev'],
)
new_device.save()
# Once the device is added, go ahead and autodetect the udev serial number (if we're connecting via serial)
if new_device.connection_type == BrewPiDevice.CONNECTION_SERIAL:
new_device.set_udev_from_port()
messages.success(request, u'Device {} Added.<br>Please wait a few seconds for controller to start'.format(new_device))
return redirect("/")
else:
return render(request, template_name='setup/device_add.html', context={'form': form})
else:
# We don't want two devices to have the same port, and the port number doesn't really matter. Just
# randomize it.
random_port = random.randint(2000,3000)
initial_values = {'socketPort': random_port, 'temp_format': config.TEMPERATURE_FORMAT,
'modify_not_create': False}
form = device_forms.BrewPiDeviceCreateForm(initial=initial_values)
return render(request, template_name='setup/device_add.html', context={'form': form})
@site_is_configured
@login_if_required_for_dashboard
def device_lcd_list(request):
# This handles generating the list of LCD screens for each device.
# Loading the actual data for the LCD screens is handled by Vue.js which loads the data via calls to api/lcd.py
return render(request, template_name="device_lcd_list.html")
@login_required
@site_is_configured
def device_control_constants_legacy(request, device_id, control_constants):
# TODO - Add user permissioning
# if not request.user.has_perm('app.add_device'):
# messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
# return redirect("/")
active_device = BrewPiDevice.objects.get(id=device_id)
if request.POST:
form = device_forms.OldCCModelForm(request.POST)
if form.is_valid():
# Generate the new_control_constants object from the form data
new_control_constants = form.save(commit=False)
# At this point, we have both the OLD control constants (control_constants) and the NEW control constants
# TODO - Modify the below to only send constants that have changed to the controller
if not new_control_constants.save_all_to_controller(active_device):
return render(request, template_name='device_control_constants_old.html',
context={'form': form, 'active_device': active_device})
# TODO - Make it so if we added a preset name we save the new preset
# new_device.save()
messages.success(request, u'Control constants updated for device {}'.format(active_device))
return redirect("/")
else:
return render(request, template_name='device_control_constants_old.html',
context={'form': form, 'active_device': active_device})
else:
form = device_forms.OldCCModelForm(instance=control_constants)
return render(request, template_name='device_control_constants_old.html',
context={'form': form, 'active_device': active_device})
@login_required
@site_is_configured
def device_control_constants_modern(request, device_id, control_constants):
# TODO - Add user permissioning
# if not request.user.has_perm('app.add_device'):
# messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
# return redirect("/")
active_device = BrewPiDevice.objects.get(id=device_id)
if request.POST:
form = device_forms.NewCCModelForm(request.POST)
if form.is_valid():
# Generate the new_control_constants object from the form data
new_control_constants = form.save(commit=False)
# At this point, we have both the OLD control constants (control_constants) and the NEW control constants
# TODO - Modify the below to only send constants that have changed to the controller
if not new_control_constants.save_all_to_controller(active_device):
return render(request, template_name='device_control_constants_new.html',
context={'form': form, 'active_device': active_device})
# TODO - Make it so if we added a preset name we save the new preset
# new_device.save()
messages.success(request, u'Control constants updated for device {}'.format(active_device))
return redirect("/")
else:
return render(request, template_name='device_control_constants_new.html',
context={'form': form, 'active_device': active_device})
else:
form = device_forms.OldCCModelForm(instance=control_constants)
return render(request, template_name='device_control_constants_new.html',
context={'form': form, 'active_device': active_device})
@login_required
@site_is_configured
def device_control_constants(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
control_constants, is_legacy = active_device.retrieve_control_constants()
if control_constants is None:
# We weren't able to retrieve the version from the controller.
messages.error(request, u"Unable to reach brewpi-script for device {}".format(active_device))
return redirect('device_dashboard', device_id=device_id)
elif is_legacy:
return device_control_constants_legacy(request, device_id, control_constants)
else:
return device_control_constants_modern(request, device_id, control_constants)
@login_required
@site_is_configured
def sensor_list(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
devices_loaded = active_device.load_sensors_from_device()
if devices_loaded:
for this_device in active_device.available_devices:
data = {'device_function': this_device.device_function, 'invert': int(this_device.invert),
'child_no': this_device.child_no, 'address': this_device.address, 'pin': this_device.pin}
this_device.device_form = device_forms.SensorFormRevised(data)
for this_device in active_device.installed_devices:
data = {'device_function': this_device.device_function, 'invert': int(this_device.invert),
'child_no': this_device.child_no, 'address': this_device.address, 'pin': this_device.pin,
'installed': True, 'perform_uninstall': True}
this_device.device_form = device_forms.SensorFormRevised(data)
else:
# If we weren't able to load devices, we should have set an error message instead. Display it.
# (we can't display it directly from load_sensors_from_device() because we aren't passing request)
messages.error(request, active_device.error_message)
return render(request, template_name="pin_list.html",
context={'available_devices': active_device.available_devices, 'active_device': active_device,
'installed_devices': active_device.installed_devices, 'devices_loaded': devices_loaded})
@login_required
@site_is_configured
def sensor_config(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
active_device.load_sensors_from_device()
if request.POST:
form = device_forms.SensorFormRevised(request.POST)
if form.is_valid():
# OK. Here is where things get a bit tricky - We can't just rely on the form to generate the sensor object
# as all the form really does is specify what about the sensor to change. Let's locate the sensor we need
# to update, then adjust it based on the sensor (device) type.
try:
if form.data['installed']:
sensor_to_adjust = SensorDevice.find_device_from_address_or_pin(active_device.installed_devices,
address=form.cleaned_data['address'], pin=form.cleaned_data['pin'], child_no=form.cleaned_data['child_no'])
else:
sensor_to_adjust = SensorDevice.find_device_from_address_or_pin(active_device.available_devices,
address=form.cleaned_data['address'], pin=form.cleaned_data['pin'], child_no=form.cleaned_data['child_no'])
except ValueError:
messages.error(request, "Unable to confirm the pin/address on your controller. Check to ensure that " +
"your controller is properly connected, and reattempt assignment.")
return redirect('sensor_list', device_id=device_id)
sensor_to_adjust.device_function = form.cleaned_data['device_function']
sensor_to_adjust.invert = form.cleaned_data['invert']
sensor_to_adjust.calibrate_adjust = form.cleaned_data['calibration']
if form.cleaned_data['perform_uninstall']:
write_succeeded = sensor_to_adjust.uninstall()
else:
write_succeeded = sensor_to_adjust.write_config_to_controller()
if write_succeeded:
if form.cleaned_data['perform_uninstall']:
messages.success(request, 'Device {} was uninstalled'.format(device_id))
else:
messages.success(request, 'Device definition saved for device {}'.format(device_id))
return redirect('sensor_list', device_id=device_id)
else:
# We failed to write the configuration to the controller. Show an error.
# TODO - Expand this error message to include instructions on resetting the EEPROM.
messages.error(request, "Failed to write the configuration to the controller. If this continues, try "
"resetting the EEPROM on the controller.")
return redirect('sensor_list', device_id=device_id)
else:
messages.error(request, "There was an error processing the form. Please review and resubmit.")
return redirect('sensor_list', device_id=device_id)
return redirect('sensor_list', device_id=device_id)
@login_required
@site_is_configured
def sensor_refresh(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
active_device.request_device_refresh()
return redirect('sensor_list', device_id=device_id)
@site_is_configured
@login_if_required_for_dashboard
def device_dashboard(request, device_id, beer_id=None):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
beer_create_form = beer_forms.BeerCreateForm()
if beer_id is None:
beer_obj = active_device.active_beer or None
available_beer_logs = Beer.objects.filter(device_id=active_device.id)
else:
try:
beer_obj = Beer.objects.get(id=beer_id, device_id=active_device.id)
except ObjectDoesNotExist:
# If we are given an invalid beer log ID, let's return an error & drop back to the (valid) dashboard
messages.error(request, 'Unable to load beer log with ID {}'.format(beer_id))
return redirect('device_dashboard', device_id=device_id)
available_beer_logs = Beer.objects.filter(device_id=active_device.id).exclude(id=beer_id)
if beer_obj is None:
# TODO - Determine if we want to load some fake "example" data (similar to what brewpi-www does)
beer_file_url = "/data/fake.csv"
else:
beer_file_url = beer_obj.data_file_url('base_csv')
# if beer_obj is None:
# column_headers = {}
# else:
# column_headers = beer_obj.column_headers_to_graph_string('base_csv')
return render(request, template_name="device_dashboard.html",
context={'active_device': active_device, 'beer_create_form': beer_create_form,
'beer': beer_obj, 'temp_display_format': config.DATE_TIME_FORMAT_DISPLAY,
'gravity_display_format': config.GRAVITY_DISPLAY_FORMAT,
# 'column_headers': column_headers,
'beer_file_url': beer_file_url, 'available_beer_logs': available_beer_logs,
'selected_beer_id': beer_id})
@login_required
@site_is_configured
def device_temp_control(request, device_id):
# TODO - Add user permissioning
# if not request.user.has_perm('app.add_device'):
# messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
# return redirect("/")
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
if request.POST:
form = device_forms.TempControlForm(request.POST)
if form.is_valid():
if form.cleaned_data['temp_control'] == 'off':
try:
success = active_device.set_temp_control(method=form.cleaned_data['temp_control'])
except ValueError as e:
messages.error(request, str(e))
return redirect('siteroot')
elif form.cleaned_data['temp_control'] == 'beer_constant' or form.cleaned_data['temp_control'] == 'fridge_constant':
try:
success = active_device.set_temp_control(method=form.cleaned_data['temp_control'],
set_temp=float(form.cleaned_data['temperature_setting']))
except ValueError as e:
messages.error(request, str(e))
return redirect('siteroot')
elif form.cleaned_data['temp_control'] == 'beer_profile':
if 'start_at' in form.cleaned_data:
start_at = form.cleaned_data['start_at']
else:
start_at = None
try:
success = active_device.set_temp_control(method=form.cleaned_data['temp_control'],
profile=form.cleaned_data['profile'], profile_startat=start_at)
except ValueError as e:
messages.error(request, str(e))
return redirect('siteroot')
else:
messages.error(request, "Invalid temperature control function specified.")
return redirect('siteroot')
if success:
messages.success(request, u'Temperature control settings updated for {}. Please wait a few seconds for settings to take effect.'.format(active_device))
if active_device.active_beer is None:
# We started temperature control, but aren't logging anything. Prompt the user.
messages.info(request, 'Temperature control enabled, but logging is off. Start a new beer from the device dashboard.')
elif active_device.logging_status != active_device.DATA_LOGGING_ACTIVE:
# We altered temperature control, but logging is paused. Prompt the user.
messages.info(request, 'Temperature control enabled, but logging is off. Start a new beer from the device dashboard.')
else:
messages.error(request, u'Unable to update temperature control settings for {}'.format(active_device))
return redirect('siteroot')
else:
messages.error(request, 'Unable to parse temperature control settings provided')
return redirect('siteroot')
else:
messages.error(request, 'No temperature control settings provided')
return redirect('siteroot')
@login_required
@site_is_configured
def github_trigger_upgrade(request, variant=""):
# TODO - Add permission check here
commit_info = git_integration.get_local_remote_commit_info()
allow_git_branch_switching = config.ALLOW_GIT_BRANCH_SWITCHING
app_is_current = git_integration.app_is_current()
git_update_type = config.GIT_UPDATE_TYPE
tags = git_integration.get_tag_info()
local_versions = git_integration.get_local_version_numbers()
# lockfile = Path(settings.ROOT_DIR) / "utils" / "upgrade_lock"
if allow_git_branch_switching:
branch_info = git_integration.get_remote_branch_info()
else:
branch_info = {}
# if request.POST:
# if app_is_current and 'new_branch' not in request.POST and 'tag' not in request.POST:
# messages.error(request, "Nothing to upgrade - Local copy and GitHub are at same commit")
# elif lockfile.exists():
# messages.error(request, "Cannot upgrade - upgrade appears to be in progress. To upgrade anyways, "
# "delete the upgrade lock using the function below.")
# else:
# cmds = {}
#
# if not allow_git_branch_switching:
# # I'm not doing "if git_update_type == config.GIT_UPDATE_TYPE" so users who have update set to 'none'
# # can still update from the "master" branch.
# if git_update_type == "dev":
# # TODO - Change back to dev once we eliminate docker-dev
# branch_to_use = "docker-dev"
# else:
# # Assume if they have anything other than "dev" they want master
# branch_to_use = "master"
# elif 'new_branch' not in request.POST:
# # Branch switching is enabled, but we weren't provided with a branch. Use the current branch.
# branch_to_use = commit_info['local_branch']
# else:
# # Branch switching is enabled & the user provided a branch. Use it.
# branch_to_use = request.POST.get('new_branch', "master")
#
# variant_flags = ""
# if variant == "force": # Does git reset --hard
# variant_flags += "-f "
# if settings.USE_DOCKER:
# variant_flags += "-d "
#
# cmd = f"nohup utils/upgrade3.sh {variant_flags} -b \"{branch_to_use}\" &"
#
# subprocess.call(cmd, shell=True)
# messages.success(request, "Triggered an upgrade from GitHub")
#
# else:
# # We'll display this error message if the page is being accessed and no form has been posted
# if app_is_current:
# messages.warning(request, "Nothing to upgrade - Local copy and GitHub are at same commit")
return render(request, template_name="github_trigger_upgrade.html",
context={'commit_info': commit_info, 'app_is_current': app_is_current, 'branch_info': branch_info,
'tags': tags, 'git_update_type': git_update_type,
'allow_git_branch_switching': allow_git_branch_switching, 'local_versions': local_versions,})
# @login_required
# @site_is_configured
# def delete_upgrade_lock_file(request, variant=""):
# lockfile = Path(settings.ROOT_DIR) / "utils" / "upgrade_lock"
#
# if not lockfile.exists():
# messages.info(request, "Unable to delete lock file - file does not exist")
# else:
# os.remove(lockfile)
# messages.success(request, "Successfully cleared lockfile. Ready to upgrade.")
#
# return redirect('github_trigger_upgrade')
# @login_required
# @site_is_configured
# def github_trigger_force_upgrade(request):
# # TODO - Get rid of this in favor of a better urlpattern
# return github_trigger_upgrade(request, variant="force")
def login(request, next=None):
if not next:
if 'next' in request.GET:
next=request.GET['next']
elif 'next' in request.POST:
next=request.POST['next']
else:
next="/"
if 'username' in request.POST:
target_user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
if target_user: # If the user authenticated, process login & redirect
auth.login(request, target_user)
messages.success(request, "Logged in")
if 'next' in request.POST:
if len(request.POST['next']) > 1:
return redirect(request.POST['next'])
return redirect('siteroot')
else:
messages.error(request, "Login failed")
return render(request, template_name="site_login.html", context={'pagetitle': 'Log In', 'next': next})
# If we hit this, we just need to display the form (no valid form input received)
return render(request, template_name="site_login.html", context={'pagetitle': 'Log In', 'next': next})
def logout(request):
if request.user.is_authenticated:
auth.logout(request)
return redirect('siteroot')
else:
return redirect('login')
@login_required
@site_is_configured
def site_settings(request):
# TODO - Add user permissioning. The wizard creates the user and login so we can check for superuser here
if not config.USER_HAS_COMPLETED_CONFIGURATION:
return redirect('siteroot')
if request.POST:
form = setup_forms.GuidedSetupConfigForm(request.POST)
if form.is_valid():
f = form.cleaned_data
config.BREWERY_NAME = f['brewery_name']
config.CUSTOM_THEME = f['custom_theme']
config.DATE_TIME_FORMAT_DISPLAY = f['date_time_format_display']
config.REQUIRE_LOGIN_FOR_DASHBOARD = f['require_login_for_dashboard']
config.TEMPERATURE_FORMAT = f['temperature_format']
config.GRAVITY_DISPLAY_FORMAT = f['gravity_display_format']
config.PREFERRED_TIMEZONE = f['preferred_timezone']
config.USER_HAS_COMPLETED_CONFIGURATION = True # Toggle once they've completed the configuration workflow
config.GRAVITY_SUPPORT_ENABLED = f['enable_gravity_support']
# config.GIT_UPDATE_TYPE = f['update_preference']
if f['enable_sentry_support'] != settings.ENABLE_SENTRY:
# The user changed the "Enable Sentry" value - but this doesn't actually take effect until Fermentrack
# restarts.
# TODO - Queue a request to Huey to restart fermentrack
messages.warning(request, "Sentry status has changed - please restart Fermentrack for this to take "
"effect.")
# This sits outside the if check above in case the user updates the setting before Fermentrack was restarted
if f['enable_sentry_support']:
setup_views.set_sentry_status(enabled=True)
else:
setup_views.set_sentry_status(enabled=False)
messages.success(request, 'App configuration has been saved')
return redirect('siteroot')
else:
# If we received an invalid form, the form object is maintained and is passed to the "render" request
# broken out at the end of this function
pass
else:
form = setup_forms.GuidedSetupConfigForm()
context = {'form': form, 'completed_config': config.USER_HAS_COMPLETED_CONFIGURATION}
context.update(external_push_list(request, context_only=True)) # Add in the "external push" object list
return render(request, template_name='site_config.html', context=context)
@login_required
@site_is_configured
def device_control_constants_legacy(request, device_id, control_constants):
# TODO - Add user permissioning
# if not request.user.has_perm('app.add_device'):
# messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
# return redirect("/")
active_device = BrewPiDevice.objects.get(id=device_id)
if request.POST:
form = device_forms.OldCCModelForm(request.POST)
if form.is_valid():
# Generate the new_control_constants object from the form data
new_control_constants = form.save(commit=False)
# At this point, we have both the OLD control constants (control_constants) and the NEW control constants
# TODO - Modify the below to only send constants that have changed to the controller
if not new_control_constants.save_all_to_controller(active_device):
return render(request, template_name='device_control_constants_old.html',
context={'form': form, 'active_device': active_device})
# TODO - Make it so if we added a preset name we save the new preset
# new_device.save()
messages.success(request, u'Control constants updated for device {}'.format(active_device))
return redirect("/")
else:
return render(request, template_name='device_control_constants_old.html',
context={'form': form, 'active_device': active_device})
else:
form = device_forms.OldCCModelForm(instance=control_constants)
return render(request, template_name='device_control_constants_old.html',
context={'form': form, 'active_device': active_device})
@login_required
@site_is_configured
def device_eeprom_reset(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
# This may be unncecessary for the EEPROM reset process, but using it as a proxy to check if we can connect
control_constants, is_legacy = active_device.retrieve_control_constants()
if control_constants is None:
# We weren't able to retrieve the version from the controller.
messages.error(request, u"Unable to reach brewpi-script for device {}".format(active_device))
return redirect('device_dashboard', device_id=device_id)
else:
active_device.reset_eeprom()
messages.success(request, "Device EEPROM reset")
return redirect("device_manage", device_id=device_id)
@login_required
@site_is_configured
def device_wifi_reset(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
# Using this as a proxy to check if we can connect
control_constants, is_legacy = active_device.retrieve_control_constants()
if control_constants is None:
# We weren't able to retrieve the version from the controller.
messages.error(request, u"Unable to reach brewpi-script for device {}".format(active_device))
return redirect('device_dashboard', device_id=device_id)
else:
active_device.reset_wifi()
messages.success(request, "Device WiFi settings reset. Reset the device to access the configuration AP.")
return redirect("device_manage", device_id=device_id)
@login_required
@site_is_configured
def device_restart(request, device_id):
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
# Using this as a proxy to check if we can connect
control_constants, is_legacy = active_device.retrieve_control_constants()
if control_constants is None:
# We weren't able to retrieve the version from the controller.
messages.error(request, u"Unable to reach brewpi-script for device {}".format(active_device))
return redirect('device_dashboard', device_id=device_id)
else:
active_device.restart()
messages.success(request, "Device restarted. Please wait a minute for the device to reconnect.")
return redirect("device_manage", device_id=device_id)
def site_help(request):
return render(request, template_name='site_help.html', context={})
@login_required
@site_is_configured
def device_manage(request, device_id):
# TODO - Add user permissioning
# if not request.user.has_perm('app.edit_device'):
# messages.error(request, 'Your account is not permissioned to edit devices. Please contact an admin')
# return redirect("/")
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
# Forms posted back to device_manage are explicitly settings update forms
if request.POST:
form = device_forms.BrewPiDeviceModifyForm(request.POST)
extended_settings_form = device_forms.BrewPiDeviceExtendedSettingsForm()
if form.is_valid():
# Update the device settings based on what we were passed via the form
# Device name is a special case since it's the key for devices
if active_device.device_name != form.cleaned_data['device_name']:
try:
existing_device = BrewPiDevice.objects.get(device_name=form.cleaned_data['device_name'])
if existing_device.id != active_device.id:
messages.error(request, u'A device already exists with the name {}'.format(
form.cleaned_data['device_name']))
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
except ObjectDoesNotExist:
# There was no existing device - we're good. Set the new name.
pass
active_device.device_name = form.cleaned_data['device_name']
active_device.temp_format = form.cleaned_data['temp_format']
active_device.data_point_log_interval = form.cleaned_data['data_point_log_interval']
active_device.useInetSocket = form.cleaned_data['useInetSocket']
active_device.socketPort = form.cleaned_data['socketPort']
active_device.socketHost = form.cleaned_data['socketHost']
active_device.serial_port = form.cleaned_data['serial_port']
active_device.serial_alt_port = form.cleaned_data['serial_alt_port']
# Not going to allow editing the board type. Can revisit if there seems to be a need later
# active_device.board_type= form.cleaned_data['board_type']
active_device.socket_name = form.cleaned_data['socket_name']
active_device.connection_type = form.cleaned_data['connection_type']
active_device.wifi_host = form.cleaned_data['wifi_host']
active_device.wifi_port = form.cleaned_data['wifi_port']
active_device.save()
messages.success(request, f'Device {active_device} Updated.<br>Please wait up to a minute for the '
f'connection to restart')
# TODO - Figure out how to accomplish this with the new process manager
# transaction.on_commit(active_device.restart_process)
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
else:
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
else:
# This would probably be easier if I was to use ModelForm instead of Form, but at this point I don't feel like
# refactoring it. Project for later if need be.
initial_values = {
'device_name': active_device.device_name,
'temp_format': active_device.temp_format,
'data_point_log_interval': active_device.data_point_log_interval,
'connection_type': active_device.connection_type,
'useInetSocket': active_device.useInetSocket,
'socketPort': active_device.socketPort,
'socketHost': active_device.socketHost,
'serial_port': active_device.serial_port,
'serial_alt_port': active_device.serial_alt_port,
'board_type': active_device.board_type,
'socket_name': active_device.socket_name,
'wifi_host': active_device.wifi_host,
'wifi_port': active_device.wifi_port,
'modify_not_create': True,
}
form = device_forms.BrewPiDeviceModifyForm(initial=initial_values)
extended_settings_form = device_forms.BrewPiDeviceExtendedSettingsForm()
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
@login_required
@site_is_configured
def device_extended_settings(request, device_id):
# TODO - Add user permissioning
# if not request.user.has_perm('app.edit_device'):
# messages.error(request, 'Your account is not permissioned to edit devices. Please contact an admin')
# return redirect("/")
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
initial_values = {
'device_name': active_device.device_name,
'temp_format': active_device.temp_format,
'data_point_log_interval': active_device.data_point_log_interval,
'connection_type': active_device.connection_type,
'useInetSocket': active_device.useInetSocket,
'socketPort': active_device.socketPort,
'socketHost': active_device.socketHost,
'serial_port': active_device.serial_port,
'serial_alt_port': active_device.serial_alt_port,
'board_type': active_device.board_type,
'socket_name': active_device.socket_name,
'wifi_host': active_device.wifi_host,
'wifi_port': active_device.wifi_port,
'modify_not_create': True,
}
form = device_forms.BrewPiDeviceModifyForm(initial=initial_values)
# Forms posted back to device_extended_settings are explicitly extended settings update forms
if request.POST:
extended_settings_form = device_forms.BrewPiDeviceExtendedSettingsForm(request.POST)
if extended_settings_form.is_valid():
es = ExtendedSettings(
invertTFT=extended_settings_form.cleaned_data['invertTFT'],
# glycol=extended_settings_form.cleaned_data['glycol'],
# lowDelay=extended_settings_form.cleaned_data['lowDelay'],
)
es.save_all_to_controller(active_device)
if es.lowDelay:
messages.warning(request, "Low Delay mode enabled. This may damage and should not be used with "
"compressor-based builds!")
if es.glycol:
messages.warning(request, "Glycol mode enabled. This may damage and should not be used with "
"compressor-based builds!")
messages.success(request, "Set updated extended settings to device")
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
else:
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
else:
# This would probably be easier if I was to use ModelForm instead of Form, but at this point I don't feel like
# refactoring it. Project for later if need be.
extended_settings_form = device_forms.BrewPiDeviceExtendedSettingsForm()
return render(request, template_name='device_manage.html',
context={'form': form, 'extended_settings_form': extended_settings_form,
'active_device': active_device})
@login_required
@site_is_configured
def device_uninstall(request, device_id):
# TODO - Add user permissioning
# if not request.user.has_perm('app.delete_device'):
# messages.error(request, 'Your account is not permissioned to uninstall devices. Please contact an admin')
# return redirect("/")
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
if request.POST:
if 'remove_1' in request.POST and 'remove_2' in request.POST and 'remove_3' in request.POST:
if request.POST['remove_1'] == "on" and request.POST['remove_2'] == "on" and request.POST['remove_3'] == "on":
try: # Yes, there is probably a better way to do this than try/except, but this works.
if active_device.gravity_sensor is not None:
# If there's an associated gravity sensor, let's disassociate the sensor & stop it from logging
grav_sensor = active_device.gravity_sensor
if grav_sensor.active_log is not None:
# The gravity sensor is currently actively logging something. This is not ideal. Lets stop it.
grav_sensor.active_log = None
messages.warning(request,
u"Gravity sensor {} was actively logging, and has now been stopped.".format(
grav_sensor))
grav_sensor.assigned_brewpi_device = None
grav_sensor.save()
except:
pass
active_device.delete()
messages.success(request, u"The device '{}' was successfully uninstalled.".format(active_device))
return redirect("siteroot")
# If we get here, one of the switches wasn't toggled
messages.error(request, "All three switches must be set to 'yes' to uninstall a device.")
return redirect("device_manage", device_id=device_id)
else:
messages.error(request, "To uninstall a device, use the form on the 'Manage Device' page.")
return redirect("device_manage", device_id=device_id)
# So here's the deal -- If we want to write json files sequentially, we have to skip closing the array. If we want to
# then interpret them using JavaScript, however, we MUST have fully formed, valid json. To acheive that, we're going to
# wrap the json file and append the closing bracket after dumping its contents to the browser.
def almost_json_view(request, device_id, beer_id):
json_close = "\r\n]"
empty_array = []
try:
beer_obj = Beer.objects.get(id=beer_id)
except ObjectDoesNotExist:
# The beer doesn't exist. Return nothing.
return JsonResponse(empty_array, safe=False, json_dumps_params={'indent': 4})
filename = settings.ROOT_DIR / settings.DATA_ROOT / beer_obj.full_filename("annotation_json")
if os.path.isfile(filename): # If there are no annotations, return an empty JsonResponse
f = open(filename, 'r')
wrapper = almost_json.AlmostJsonWrapper(f, closing_string=json_close)
response = FileResponse(wrapper, content_type="application/json")
# response['Content-Length'] = os.path.getsize(filename) + len(json_close)
return response
else:
return JsonResponse(empty_array, safe=False, json_dumps_params={'indent': 4})
@login_required
@site_is_configured
def debug_connection(request, device_id):
# TODO - Add user permissioning
# if not request.user.has_perm('app.delete_device'):
# messages.error(request, 'Your account is not permissioned to uninstall devices. Please contact an admin')
# return redirect("/")
try:
active_device = BrewPiDevice.objects.get(id=device_id)
except ObjectDoesNotExist:
messages.error(request, "Unable to load device with ID {}".format(device_id))
return redirect('siteroot')
tests=[]
FAILED = 'Failed'
PASSED = 'Passed'