|
11 | 11 |
|
12 | 12 | @admin.action(description="Disable selected cards")
|
13 | 13 | def disable_selected_cards(model_admin, request, queryset: QuerySetType[InterlockCard]):
|
14 |
| - for interlock_card in queryset: |
15 |
| - interlock_card.enabled = False |
16 |
| - interlock_card.save(update_fields=["enabled"]) |
| 14 | + for interlock_card in queryset: |
| 15 | + interlock_card.enabled = False |
| 16 | + interlock_card.save(update_fields=["enabled"]) |
17 | 17 |
|
18 | 18 |
|
19 | 19 | @admin.action(description="Enable selected cards")
|
20 | 20 | def enable_selected_cards(model_admin, request, queryset: QuerySetType[InterlockCard]):
|
21 |
| - for interlock_card in queryset: |
22 |
| - interlock_card.enabled = True |
23 |
| - interlock_card.save(update_fields=["enabled"]) |
| 21 | + for interlock_card in queryset: |
| 22 | + interlock_card.enabled = True |
| 23 | + interlock_card.save(update_fields=["enabled"]) |
24 | 24 |
|
25 | 25 |
|
26 | 26 | @admin.action(description="Lock selected interlocks")
|
27 | 27 | def lock_selected_interlocks(model_admin, request, queryset):
|
28 |
| - for interlock in queryset: |
29 |
| - try: |
30 |
| - command_succeeded = interlock.lock() |
31 |
| - if command_succeeded: |
32 |
| - messages.success(request, f"{interlock} has been successfully locked") |
33 |
| - else: |
34 |
| - messages.error(request, f"{interlock} could not be locked. {interlock.most_recent_reply}") |
35 |
| - except Exception as error: |
36 |
| - messages.error(request, f"{interlock} could not be locked due to the following error: {str(error)}") |
| 28 | + for interlock in queryset: |
| 29 | + try: |
| 30 | + command_succeeded = interlock.lock() |
| 31 | + if command_succeeded: |
| 32 | + messages.success(request, f"{interlock} has been successfully locked") |
| 33 | + else: |
| 34 | + messages.error(request, f"{interlock} could not be locked. {interlock.most_recent_reply}") |
| 35 | + except Exception as error: |
| 36 | + messages.error(request, f"{interlock} could not be locked due to the following error: {str(error)}") |
37 | 37 |
|
38 | 38 |
|
39 | 39 | @admin.action(description="Unlock selected interlocks")
|
40 | 40 | def unlock_selected_interlocks(model_admin, request, queryset):
|
41 |
| - for interlock in queryset: |
42 |
| - try: |
43 |
| - command_succeeded = interlock.unlock() |
44 |
| - if command_succeeded: |
45 |
| - messages.success(request, f"{interlock} has been successfully unlocked") |
46 |
| - else: |
47 |
| - messages.error(request, f"{interlock} could not be unlocked. {interlock.most_recent_reply}") |
48 |
| - except Exception as error: |
49 |
| - messages.error(request, f"{interlock} could not be unlocked due to the following error: {str(error)}") |
| 41 | + for interlock in queryset: |
| 42 | + try: |
| 43 | + command_succeeded = interlock.unlock() |
| 44 | + if command_succeeded: |
| 45 | + messages.success(request, f"{interlock} has been successfully unlocked") |
| 46 | + else: |
| 47 | + messages.error(request, f"{interlock} could not be unlocked. {interlock.most_recent_reply}") |
| 48 | + except Exception as error: |
| 49 | + messages.error(request, f"{interlock} could not be unlocked due to the following error: {str(error)}") |
50 | 50 |
|
51 | 51 |
|
52 | 52 | @admin.action(description="Synchronize selected interlocks with tool usage")
|
53 | 53 | def synchronize_with_tool_usage(model_admin, request, queryset):
|
54 |
| - for interlock in queryset: |
55 |
| - # Ignore interlocks with no tool assigned, and ignore interlocks connected to doors |
56 |
| - if not hasattr(interlock, "tool") or hasattr(interlock, "door"): |
57 |
| - continue |
58 |
| - if interlock.tool.in_use(): |
59 |
| - interlock.unlock() |
60 |
| - else: |
61 |
| - interlock.lock() |
| 54 | + for interlock in queryset: |
| 55 | + # Ignore interlocks with no tool assigned, and ignore interlocks connected to doors |
| 56 | + if not hasattr(interlock, "tool") or hasattr(interlock, "door"): |
| 57 | + continue |
| 58 | + if interlock.tool.in_use(): |
| 59 | + interlock.unlock() |
| 60 | + else: |
| 61 | + interlock.lock() |
62 | 62 |
|
63 | 63 |
|
64 | 64 | @admin.action(description="Create next interlock")
|
65 | 65 | def create_next_interlock(model_admin, request, queryset):
|
66 |
| - for interlock in queryset: |
67 |
| - new_interlock = Interlock() |
68 |
| - new_interlock.card = interlock.card |
69 |
| - new_interlock.unit_id = interlock.unit_id |
70 |
| - max_channel = Interlock.objects.filter(card=interlock.card).aggregate(Max("channel"))["channel__max"] |
71 |
| - new_interlock.channel = max_channel + 1 if max_channel is not None else None |
72 |
| - new_interlock.save() |
| 66 | + for interlock in queryset: |
| 67 | + new_interlock = Interlock() |
| 68 | + new_interlock.card = interlock.card |
| 69 | + new_interlock.unit_id = interlock.unit_id |
| 70 | + max_channel = Interlock.objects.filter(card=interlock.card).aggregate(Max("channel"))["channel__max"] |
| 71 | + new_interlock.channel = max_channel + 1 if max_channel is not None else None |
| 72 | + new_interlock.save() |
73 | 73 |
|
74 | 74 |
|
75 | 75 | @admin.action(description="Duplicate selected tool configuration")
|
76 | 76 | def duplicate_tool_configuration(model_admin, request, queryset):
|
77 |
| - for tool in queryset: |
78 |
| - original_name = tool.name |
79 |
| - new_name = "Copy of " + tool.name |
80 |
| - try: |
81 |
| - if Tool.objects.filter(name=new_name).exists(): |
82 |
| - messages.error( |
83 |
| - request, |
84 |
| - mark_safe( |
85 |
| - f'There is already a copy of {original_name} as <a href="{reverse("admin:NEMO_tool_change", args=[tool.id])}">{new_name}</a>. Change the copy\'s name and try again' |
86 |
| - ), |
87 |
| - ) |
88 |
| - continue |
89 |
| - elif tool.is_child_tool(): |
90 |
| - messages.warning(request, f"{original_name} is a child tool and cannot be duplicated") |
91 |
| - continue |
92 |
| - else: |
93 |
| - old_required_resources = tool.required_resource_set.all() |
94 |
| - old_nonrequired_resources = tool.nonrequired_resource_set.all() |
95 |
| - old_backup_users = tool.backup_owners.all() |
96 |
| - old_superusers = tool.superusers.all() |
97 |
| - old_reviewers = tool.adjustment_request_reviewers.all() |
98 |
| - old_qualified_users = User.objects.filter(qualifications__id=tool.pk).distinct() |
99 |
| - tool.pk = None |
100 |
| - tool.interlock = None |
101 |
| - tool.visible = False |
102 |
| - tool.operational = False |
103 |
| - tool.name = new_name |
104 |
| - tool.image = None |
105 |
| - tool.description = None |
106 |
| - tool.serial = None |
107 |
| - tool.save() |
108 |
| - tool.required_resource_set.set(old_required_resources) |
109 |
| - tool.nonrequired_resource_set.set(old_nonrequired_resources) |
110 |
| - tool.backup_owners.set(old_backup_users) |
111 |
| - tool.superusers.set(old_superusers) |
112 |
| - tool.adjustment_request_reviewers.set(old_reviewers) |
113 |
| - for user in old_qualified_users: |
114 |
| - user.qualifications.add(tool) |
115 |
| - messages.success( |
116 |
| - request, |
117 |
| - mark_safe( |
118 |
| - f'A duplicate of {original_name} has been made as <a href="{reverse("admin:NEMO_tool_change", args=[tool.id])}">{tool.name}</a>' |
119 |
| - ), |
120 |
| - ) |
121 |
| - except Exception as error: |
122 |
| - messages.error( |
123 |
| - request, f"{original_name} could not be duplicated because of the following error: {str(error)}" |
124 |
| - ) |
| 77 | + for tool in queryset: |
| 78 | + original_name = tool.name |
| 79 | + new_name = "Copy of " + tool.name |
| 80 | + try: |
| 81 | + if Tool.objects.filter(name=new_name).exists(): |
| 82 | + messages.error( |
| 83 | + request, |
| 84 | + mark_safe( |
| 85 | + f'There is already a copy of {original_name} as <a href="{reverse("admin:NEMO_tool_change", args=[tool.id])}">{new_name}</a>. Change the copy\'s name and try again' |
| 86 | + ), |
| 87 | + ) |
| 88 | + continue |
| 89 | + elif tool.is_child_tool(): |
| 90 | + messages.warning(request, f"{original_name} is a child tool and cannot be duplicated") |
| 91 | + continue |
| 92 | + else: |
| 93 | + old_required_resources = tool.required_resource_set.all() |
| 94 | + old_nonrequired_resources = tool.nonrequired_resource_set.all() |
| 95 | + old_backup_users = tool.backup_owners.all() |
| 96 | + old_superusers = tool.superusers.all() |
| 97 | + old_reviewers = tool.adjustment_request_reviewers.all() |
| 98 | + old_qualified_users = User.objects.filter(qualifications__id=tool.pk).distinct() |
| 99 | + tool.pk = None |
| 100 | + tool.interlock = None |
| 101 | + tool.visible = False |
| 102 | + tool.operational = False |
| 103 | + tool.name = new_name |
| 104 | + tool.image = None |
| 105 | + tool.description = None |
| 106 | + tool.serial = None |
| 107 | + tool.save() |
| 108 | + tool.required_resource_set.set(old_required_resources) |
| 109 | + tool.nonrequired_resource_set.set(old_nonrequired_resources) |
| 110 | + tool.backup_owners.set(old_backup_users) |
| 111 | + tool.superusers.set(old_superusers) |
| 112 | + tool.adjustment_request_reviewers.set(old_reviewers) |
| 113 | + for user in old_qualified_users: |
| 114 | + user.qualifications.add(tool) |
| 115 | + messages.success( |
| 116 | + request, |
| 117 | + mark_safe( |
| 118 | + f'A duplicate of {original_name} has been made as <a href="{reverse("admin:NEMO_tool_change", args=[tool.id])}">{tool.name}</a>' |
| 119 | + ), |
| 120 | + ) |
| 121 | + except Exception as error: |
| 122 | + messages.error( |
| 123 | + request, f"{original_name} could not be duplicated because of the following error: {str(error)}" |
| 124 | + ) |
125 | 125 |
|
126 | 126 |
|
127 | 127 | @admin.action(description="Rebuild area tree")
|
128 | 128 | def rebuild_area_tree(model_admin, request, queryset):
|
129 |
| - Area.objects.rebuild() |
| 129 | + Area.objects.rebuild() |
130 | 130 |
|
131 | 131 |
|
132 | 132 | @admin.action(description="Export selected adjustment requests in CSV")
|
133 | 133 | def adjustment_requests_export_csv(modeladmin, request, queryset):
|
134 |
| - return adjustments_csv_export(queryset.all()) |
| 134 | + return adjustments_csv_export(queryset.all()) |
135 | 135 |
|
136 | 136 |
|
137 | 137 | @admin.action(description="Export selected access requests in CSV")
|
138 | 138 | def access_requests_export_csv(modeladmin, request, queryset):
|
139 |
| - return access_csv_export(queryset.all()) |
| 139 | + return access_csv_export(queryset.all()) |
0 commit comments