18
18
import datetime
19
19
from django .db import transaction
20
20
from django .contrib .postgres .aggregates import ArrayAgg
21
+ from django .http import HttpResponseRedirect
22
+ from django .urls import reverse
21
23
22
24
from nav .models .manage import Netbox
23
25
from nav .bulkparse import NetboxBulkParser
24
26
from nav .bulkimport import NetboxImporter
25
27
28
+ from nav .web .message import new_message , Messages
26
29
from nav .web .seeddb import SeeddbInfo , reverse_lazy
27
30
from nav .web .seeddb .constants import SEEDDB_EDITABLE_MODELS
28
31
from nav .web .seeddb .page import view_switcher
31
34
from nav .web .seeddb .utils .move import move
32
35
from nav .web .seeddb .utils .bulk import render_bulkimport
33
36
from nav .web .seeddb .page .netbox .forms import NetboxFilterForm , NetboxMoveForm
37
+ from nav .web .utils import (
38
+ generate_qr_codes_as_byte_strings ,
39
+ generate_qr_codes_as_zip_file ,
40
+ )
34
41
35
42
36
43
class NetboxInfo (SeeddbInfo ):
@@ -54,7 +61,11 @@ class NetboxInfo(SeeddbInfo):
54
61
def netbox (request ):
55
62
"""Controller for landing page for netboxes"""
56
63
return view_switcher (
57
- request , list_view = netbox_list , move_view = netbox_move , delete_view = netbox_delete
64
+ request ,
65
+ list_view = netbox_list ,
66
+ move_view = netbox_move ,
67
+ delete_view = netbox_delete ,
68
+ generate_qr_codes_view = netbox_generate_qr_codes ,
58
69
)
59
70
60
71
@@ -112,6 +123,58 @@ def netbox_pre_deletion_mark(queryset):
112
123
queryset .update (deleted_at = datetime .datetime .now (), up_to_date = False )
113
124
114
125
126
+ def netbox_generate_qr_codes (request ):
127
+ """Controller for generating qr codes for netboxes"""
128
+ if not request .POST .getlist ('object' ):
129
+ new_message (
130
+ request ,
131
+ "You need to select at least one object to generate qr codes for" ,
132
+ Messages .ERROR ,
133
+ )
134
+ return HttpResponseRedirect (reverse ('seeddb-room' ))
135
+
136
+ url_dict = dict ()
137
+ netboxes = Netbox .objects .filter (id__in = request .POST .getlist ('object' ))
138
+
139
+ for netbox in netboxes :
140
+ url = request .build_absolute_uri (
141
+ reverse ('ipdevinfo-details-by-id' , kwargs = {'netbox_id' : netbox .id })
142
+ )
143
+ url_dict [str (netbox )] = url
144
+
145
+ qr_codes_zip_file = generate_qr_codes_as_zip_file (url_dict = url_dict )
146
+
147
+ info = NetboxInfo ()
148
+ query = (
149
+ Netbox .objects .select_related ("room" , "category" , "type" , "organization" )
150
+ .prefetch_related ("profiles" )
151
+ .annotate (profile = ArrayAgg ("profiles__name" ))
152
+ )
153
+ filter_form = NetboxFilterForm (request .GET )
154
+ value_list = (
155
+ 'sysname' ,
156
+ 'room' ,
157
+ 'ip' ,
158
+ 'category' ,
159
+ 'organization' ,
160
+ 'profile' ,
161
+ 'type__name' ,
162
+ )
163
+ return render_list (
164
+ request ,
165
+ query ,
166
+ value_list ,
167
+ 'seeddb-netbox-edit' ,
168
+ edit_url_attr = 'pk' ,
169
+ filter_form = filter_form ,
170
+ template = 'seeddb/list_netbox.html' ,
171
+ extra_context = {
172
+ ** info .template_context ,
173
+ ** {"qr_codes_zip_file" : qr_codes_zip_file },
174
+ },
175
+ )
176
+
177
+
115
178
def netbox_move (request ):
116
179
"""Controller for handling a move request"""
117
180
info = NetboxInfo ()
0 commit comments