@@ -67,51 +67,68 @@ def get_context_data(self, **kwargs):
67
67
round (status .battery_voltage , 2 )
68
68
for status in device_status_qs
69
69
if status .battery_soc is not None and status .battery_voltage is not None
70
- ]
71
-
72
- # query changes
73
- organization_changes = device .history .filter (changes__icontains = '"current_organization"' ).all ().order_by ('-timestamp' )
74
- room_changes = device .history .filter (changes__icontains = '"current_room"' ).all ().order_by ('-timestamp' )
75
- user_changes = device .history .filter (changes__icontains = '"current_user"' ).all ().order_by ('-timestamp' )
76
-
77
- organization_change_log = []
78
- room_change_log = []
79
- user_change_log = []
80
-
81
- # prepare changes
82
- for h in organization_changes :
83
- prev = h .changes ['current_organization' ][0 ]
84
- next = h .changes ['current_organization' ][1 ]
85
- organization_change_log .append ({
86
- 'timestamp' : h .timestamp ,
87
- 'prev' : None if prev == 'None' else Organization .objects .filter (id = prev ).first (),
88
- 'next' : None if next == 'None' else Organization .objects .filter (id = next ).first (),
89
- })
90
- for h in room_changes :
91
- prev = h .changes ['current_room' ][0 ]
92
- next = h .changes ['current_room' ][1 ]
93
- room_change_log .append ({
94
- 'timestamp' : h .timestamp ,
95
- 'prev' : None if prev == 'None' else Room .objects .filter (id = prev ).first (),
96
- 'next' : None if next == 'None' else Room .objects .filter (id = next ).first (),
97
- })
98
- for h in user_changes :
99
- prev = h .changes ['current_user' ][0 ]
100
- next = h .changes ['current_user' ][1 ]
101
- user_change_log .append ({
102
- 'timestamp' : h .timestamp ,
103
- 'prev' : None if prev == 'None' else CustomUser .objects .filter (id = prev ).first (),
104
- 'next' : None if next == 'None' else CustomUser .objects .filter (id = next ).first (),
105
- })
106
-
107
- context ['organization_change_log' ] = organization_change_log
108
- context ['room_change_log' ] = room_change_log
109
- context ['user_change_log' ] = user_change_log
110
-
70
+ ]
111
71
# Serialize data to JSON format
112
72
context ['battery_times' ] = json .dumps (battery_times , cls = DjangoJSONEncoder )
113
73
context ['battery_charges' ] = json .dumps (battery_charges , cls = DjangoJSONEncoder )
114
74
context ['battery_voltages' ] = json .dumps (battery_voltages , cls = DjangoJSONEncoder )
75
+
76
+ # query changes
77
+ organization_changes = device .history .filter (changes__icontains = '"current_organization"' ).all ().order_by ('-timestamp' )
78
+ room_changes = device .history .filter (changes__icontains = '"current_room"' ).all ().order_by ('-timestamp' )
79
+ user_changes = device .history .filter (changes__icontains = '"current_user"' ).all ().order_by ('-timestamp' )
80
+
81
+ organization_change_log = []
82
+ room_change_log = []
83
+ user_change_log = []
84
+ workshop_change_log = []
85
+
86
+ # prepare changes
87
+ for h in organization_changes :
88
+ prev = h .changes ['current_organization' ][0 ]
89
+ next = h .changes ['current_organization' ][1 ]
90
+ organization_change_log .append ({
91
+ 'timestamp' : h .timestamp ,
92
+ 'prev' : None if prev == 'None' else Organization .objects .filter (id = prev ).first (),
93
+ 'next' : None if next == 'None' else Organization .objects .filter (id = next ).first (),
94
+ })
95
+ for h in room_changes :
96
+ prev = h .changes ['current_room' ][0 ]
97
+ next = h .changes ['current_room' ][1 ]
98
+ room_change_log .append ({
99
+ 'timestamp' : h .timestamp ,
100
+ 'prev' : None if prev == 'None' else Room .objects .filter (id = prev ).first (),
101
+ 'next' : None if next == 'None' else Room .objects .filter (id = next ).first (),
102
+ })
103
+ for h in user_changes :
104
+ prev = h .changes ['current_user' ][0 ]
105
+ next = h .changes ['current_user' ][1 ]
106
+ user_change_log .append ({
107
+ 'timestamp' : h .timestamp ,
108
+ 'prev' : None if prev == 'None' else CustomUser .objects .filter (id = prev ).first (),
109
+ 'next' : None if next == 'None' else CustomUser .objects .filter (id = next ).first (),
110
+ })
111
+
112
+ # time, workshop
113
+ workshop_changes = []
114
+ for record in reversed (sorted ((record .time , record .workshop .name ) for record in device .air_quality_records .all ())):
115
+ if not workshop_changes or workshop_changes [- 1 ][1 ] != record [1 ]:
116
+ workshop_changes .append (record )
117
+
118
+ for i in range (0 , len (workshop_changes )):
119
+ workshop_change_log .append ({
120
+ 'timestamp' : workshop_changes [i ][0 ],
121
+ 'prev' : None if i == len (workshop_changes ) - 1 else workshop_changes [i + 1 ][1 ],
122
+ 'next' : workshop_changes [i ][1 ]
123
+ })
124
+
125
+ current_workshop = workshop_change_log [0 ]['next' ] if workshop_change_log else None
126
+
127
+ context ['organization_change_log' ] = organization_change_log
128
+ context ['room_change_log' ] = room_change_log
129
+ context ['user_change_log' ] = user_change_log
130
+ context ['workshop_change_log' ] = workshop_change_log
131
+ context ['current_workshop' ] = current_workshop
115
132
116
133
# Fetch all DeviceLogs entries related to this Device, ordered by timestamp descendingly
117
134
device_logs_qs = DeviceLogs .objects .filter (device = device ).order_by ('-timestamp' )
@@ -180,7 +197,7 @@ class DeviceEditView(UpdateView):
180
197
model = Device
181
198
form_class = DeviceForm
182
199
template_name = 'devices/form.html'
183
- success_url = reverse_lazy ('device -list' ) # URL to redirect to after a successful update
200
+ success_url = reverse_lazy ('devices -list' ) # URL to redirect to after a successful update
184
201
185
202
def get_queryset (self ):
186
203
"""
@@ -221,7 +238,7 @@ def get_queryset(self):
221
238
class DeviceDeleteView (LoginRequiredMixin , DeleteView ):
222
239
model = Device
223
240
template_name = 'devices/confirm_delete.html' # Confirmation page template
224
- success_url = reverse_lazy ('device -list' ) # Redirect here after deletion
241
+ success_url = reverse_lazy ('devices -list' ) # Redirect here after deletion
225
242
226
243
def get_object (self , queryset = None ):
227
244
device = super ().get_object (queryset )
0 commit comments