- Timestamp:
- Dec 10, 2008, 2:11:32 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/exodus/views.py
r6371 r6430 1 1 # (c) Roland van Laar 2006 2 2 3 from django.db import models 4 from django.http import HttpResponse, HttpResponseRedirect 5 from django.template import Context, loader 3 from django.http import HttpResponseRedirect 6 4 from django.shortcuts import render_to_response 7 5 from django.core.urlresolvers import reverse, NoReverseMatch 8 from django.forms.formsets import formset_factory9 6 from django import forms 10 7 from socket import gethostname … … 12 9 from exodus.models import * 13 10 from exodus.wllogic import free_masterip, newSSIDName, addInterlinkIP, \ 14 11 freeInterlinkIP, freePublicAPIP 15 12 16 13 from exodus.utils import pdebug 17 14 18 15 class GenericHandler(object): 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 16 """Conventions used: type has to to the same name as the dependency 17 object, delet template is named delete<type>.html 18 """ 19 def __init__(self, request, mode): 20 pdebug(100, "Function: GenericHandler.__init__") 21 # Strip 'Handler' of name 22 type = self.__class__.__name__[:-7:] 23 formClass = eval(type + 'Form') 24 25 self.object = eval('self.' + type[0].lower() + type[1::]) 26 self.title = self.object._meta.verbose_name 27 self.request = request 28 self.mode = mode 29 self.is_changed = {} 30 31 if request.POST.has_key('cancel'): 32 pdebug(100, "Action cancel") 33 self.form_action = 'cancel' 34 # Allow overriding cancel call, in particular with regards to the 35 # response call 36 self._cancel() 37 elif request.POST.has_key('proceed'): 38 pdebug(100, "Action proceed") 39 self.form_action = 'proceed' 40 41 # Delete does not require a valid form 42 if mode == 'delete': 43 self._delete() 44 else: 45 # First checking whether form is valid, then add/edit actions 46 self.form = formClass(request.POST, instance=self.object) 47 try: 48 if self.form.is_valid(): 49 pdebug(100, "Form valid") 50 # Set response on forehand, to allow override \ 51 # in procedure 52 try: 53 self.response = HttpResponseRedirect(reverse( \ 54 'exodus.views.viewNode', args=[self.node.name])) 55 except (AttributeError, NoReverseMatch): 56 self.response = HttpResponseRedirect(reverse( \ 57 'exodus.views.viewNodelist')) 58 59 #Checking whether data did change 60 _oldInstance = dict(self.object.as_list()) 61 _instance = dict(self.form.save(commit=False).as_list()) 62 for key,value in _instance.items(): 63 if value != _oldInstance[key]: 64 pdebug(100, "Key %s changed value '%s' -> '%s'"\ 65 % (key, _oldInstance[key], value)) 66 self.is_changed[key] = value 67 68 # Call override procedure 69 if mode == 'add': 70 self._add() 71 elif mode == 'edit': 72 self._edit() 73 else: 74 raise ValueError, 'Mode "%s" not defined' % mode 75 else: 76 raise ValueError, 'Form error, please edit and resubmit' 77 except ValueError, message: 78 self.response = render_to_response('genericForm.html', { 79 'form': self.form, 'message' : message, 80 'title' : self.title, 'mode' : mode, 81 'type' : type, 'object': self.object, 82 'delInclude' : "delete" + type.capitalize() + ".html", 83 'addInclude' : "add" + type.capitalize() + ".html", 84 'editInclude' : "edit" + type.capitalize() + ".html" }) 85 else: 86 message = 'Please edit and submit' 87 88 # Dirty? hack to allow initial form to be filled with date \ 89 # for GET request, no errors raised 90 if request.GET and mode == 'add': 91 self.form = formClass(request.GET, instance=self.object) 92 self.form._errors = {} 93 else: 94 self.form = formClass(instance=self.object) 95 96 self.response = render_to_response('genericForm.html', { 97 'form': self.form, 'message' : message, 'title' : self.title, 98 'mode' : mode, 'type' : type, 'object': self.object, 99 'delInclude' : "delete" + type.capitalize() + ".html", 100 'addInclude' : "add" + type.capitalize() + ".html", 101 'editInclude' : "edit" + type.capitalize() + ".html" }) 102 103 def _add(self): 104 pdebug(100, "Function: GenericHandler._add") 105 self.form.save() 106 107 def _edit(self): 108 pdebug(100, "Function: GenericHandler._edit") 109 self.form.save() 110 111 def _delete(self): 112 pdebug(100, "Function: GenericHandler._delete") 113 self.object.delete() 114 self.response = HttpResponseRedirect(reverse('exodus.views.viewNode',\ 115 args=[self.node.name])) 116 117 def _cancel(self): 118 pdebug(100, "Function: GenericHandler._cancel") 119 self.response = HttpResponseRedirect(reverse('exodus.views.viewNode',\ 120 args=[self.node.name])) 121 #self.response = HttpResponseRedirect(reverse('exodus.views.viewNodelist')) 122 123 def render_to_response(self): 124 pdebug(100, "Function: GenericHandler.render_to_response") 125 return self.response 129 126 130 127 … … 132 129 # PublicAP 133 130 class PublicAPForm(forms.ModelForm): 134 135 136 131 class Meta: 132 model = PublicAP 133 exclude = ('shortdesc', 'desc', 'ip', 'dhcpstart', 'dhcpstop') 137 134 138 135 class PublicAPHandler(GenericHandler): 139 136 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 137 def __init__(self, request, node, interface, publicAP, mode): 138 pdebug(100, "Function: PublicAPHandler.__init__") 139 self.node = Node.objects.get(name=node) 140 self.interface = Interface.objects.get(node=self.node, iface=interface) 141 if mode == 'add': 142 self.publicAP = PublicAP(iface=self.interface) 143 else: 144 self.publicAP = PublicAP.objects.get(iface=self.interface, \ 145 pk=publicAP) 146 super(PublicAPHandler, self).__init__(request, mode) 147 148 def _add(self): 149 pdebug(100, "Function: PublicAPHandler._add") 150 _instance = self.form.save(commit=False) 151 # Find IP range inside interface range with disired size/subnet 152 _instance.ip = freePublicAPIP(_instance.iface, _instance.netmask) 153 _instance.dhcpstart = 1 154 _instance.dhcpstop = 2 155 # If wireless generate ssid name 156 _instance.ssid = newSSIDName(_instance.iface.node, _instance.iface, \ 157 'omni') 158 _instance.save() 162 159 163 160 def genericPublicAP(request, node, interface, publicAP, mode): 164 165 166 161 pdebug(100, "Function: genericPublicAP") 162 handler = PublicAPHandler(request, node, interface, publicAP, mode) 163 return handler.render_to_response() 167 164 168 165 # 169 166 # Interface 170 167 class InterfaceForm(forms.ModelForm): 171 172 173 174 168 class Meta: 169 model = Interface 170 exclude = ( 'ip', 'ssid', 'mode', 'channel', 'shortdesc', \ 171 'netmask' ) 175 172 176 173 class InterfaceHandler(GenericHandler): 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 174 def __init__(self, request, node, interface, mode): 175 pdebug(100, "Function: InterfaceHandler.__init__") 176 self.node = Node.objects.get(name=node) 177 if mode == 'add': 178 self.interface = Interface(node=self.node) 179 else: 180 self.interface = Interface.objects.get(node=self.node, \ 181 iface=interface) 182 super(InterfaceHandler, self).__init__(request, mode) 183 184 def _add(self): 185 pdebug(100, "Function: InterfaceHandler._add") 186 self._saveInterface() 187 188 def _edit(self): 189 pdebug(100, "Function: InterfaceHandler._edit") 190 self._saveInterface() 191 192 def _saveInterface(self): 193 pdebug(100, "Function: InterfaceHandler._saveInterface") 194 _instance = self.form.save(commit=False) 195 if _instance.link and (_instance.type != _instance.link.type): 196 raise ValueError,'Type of local and remote interface needs to match' 197 if str(_instance.type) != "eth": 198 _instance.ssid = newSSIDName(_instance.node, _instance.iface, \ 199 'unused') 200 _instance.channel = '1' 201 _instance.mode = 1 # set to master 202 203 # Only change IP if changes in interface link/mask or new of course :-) 204 if self.mode == 'add' or self.is_changed.has_key('link') or \ 205 self.is_changed.has_key('netmask'): 206 if not _instance.link: 207 _instance.ip = freeInterlinkIP(_instance) 208 else: 209 _instance.ip = addInterlinkIP(_instance.link) 210 211 # XXX: Change in netmask requires full range of netmask changes \ 212 # on slaves 213 _instance.save() 214 #Dirty to hack to get reference to self working 215 if not _instance.link: 216 _instance.link = _instance 217 _instance.save() 221 218 222 219 def genericInterface(request, node, interface, mode): 223 224 225 220 pdebug(100, "Function: genericInterface") 221 handler = InterfaceHandler(request, node, interface, mode) 222 return handler.render_to_response() 226 223 227 224 # 228 225 # Node 229 226 class NodeForm(forms.ModelForm): 230 231 232 227 class Meta: 228 model = Node 229 exclude = ( 'masterip' ) 233 230 234 231 class NodeHandler(GenericHandler): 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 232 def __init__(self, request, node, mode): 233 pdebug(100, "Function: NodeHandler.__init__") 234 if mode == 'add': 235 self.node = Node() 236 else: 237 self.node = Node.objects.get(name=node) 238 super(NodeHandler, self).__init__(request, mode) 239 240 def _add(self): 241 pdebug(100, "Function: NodeHandler._add") 242 # input a valid master ip into new_data 243 _instance = self.form.save(commit=False) 244 _instance.masterip = free_masterip(_instance.network) 245 _instance.save() 246 self.response = HttpResponseRedirect(reverse('exodus.views.viewNode', \ 247 args=[_instance.name])) 248 249 def _delete(self): 250 pdebug(100, "Function: NodeHandler._delete") 251 for _master in Interface.objects.filter(node=self.object): 252 if _master.link == _master: 253 for _makeMaster in Interface.objects.filter(link=_master): 254 _makeMaster.link = _makeMaster 255 _makeMaster.save() 256 self.object.delete() 257 # As node is deleted, goto overview page 258 self.response = HttpResponseRedirect(reverse( \ 259 'exodus.views.viewNodelist')) 260 261 def _cancel(self): 262 pdebug(100, "Function: NodeHandler._cancel") 263 if self.mode == 'new': 264 self.response = HttpResponseRedirect(reverse( \ 265 'exodus.views.viewNodelist')) 266 else: 267 self.response = HttpResponseRedirect(reverse( \ 268 'exodus.views.viewNode', args=[self.node.name])) 272 269 273 270 def genericNode(request, node, mode): 274 275 276 271 pdebug(100, "Function: genericNode") 272 handler = NodeHandler(request, node, mode) 273 return handler.render_to_response() 277 274 278 275 # 279 276 # location 280 277 class LocationForm(forms.ModelForm): 281 282 278 class Meta: 279 model = Location 283 280 284 281 class LocationHandler(GenericHandler): 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 282 def __init__(self, request, location, mode): 283 pdebug(100, "Function: LocationHandler.__init__") 284 if mode == 'add': 285 self.location = Location() 286 else: 287 self.location = Location.objects.get(description=location) 288 289 super(LocationHandler,self).__init__(request, mode) 290 291 def _add(self): 292 pdebug(100, "Function: LocationHandler._add") 293 _instance = self.form.save() 294 # After adding a location, allow adding a Node with this location 295 self.response = HttpResponseRedirect( \ 296 reverse('exodus.views.genericNode', args=["add", "new"]) + \ 297 "?location=%i" % _instance.pk) 298 299 def _delete(self): 300 pdebug(100, "Function: LocationHandler._delete") 301 self.object.delete() 302 self.response = HttpResponseRedirect(reverse( \ 303 'exodus.views.viewNodelist')) 304 305 def _cancel(self): 306 pdebug(100, "Function: LocationHandler._cancel") 307 self.response = HttpResponseRedirect(reverse( \ 308 'exodus.views.viewNodelist')) 312 309 313 310 # 314 311 # Views 315 312 def viewNode(request, node): 316 317 318 313 pdebug(100, "Function: viewNode") 314 node = Node.objects.get(name=node) 315 return render_to_response('viewNode.html', {'node': node}) 319 316 320 317 def viewNodelist(request): 321 322 323 324 325 326 327 328 329 330 318 pdebug(100, "Function: viewNodelist") 319 nodes = Node.objects.all() 320 configFiles = ( 321 'rc.local', 322 'rc.node.local', 323 'dhcpd.conf', 324 'named.conf', 325 'resolv.conf' ) 326 return render_to_response('viewNodelist.html', {'nodes' : nodes, \ 327 'configFiles' : configFiles}) 331 328 332 329 def configFile(request, version, node, file): 333 334 335 336 337 338 339 340 341 342 330 pdebug(100, "Function: configFile") 331 node = Node.objects.get(name=node) 332 333 # Extra statictics information for use of generation 334 server = {} 335 server['host'] = gethostname() 336 337 templateFile = version + '/' + file 338 return render_to_response(templateFile, {'node' : node, 'server' : server},\ 339 mimetype='text/plain') 343 340 344 341 # 345 342 # DnsServer 346 343 class DnsServerForm(forms.ModelForm): 347 348 344 class Meta: 345 model = DnsServer 349 346 350 347 class DnsServerHandler(GenericHandler): 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 348 def __init__(self, request, dnsServer, mode): 349 pdebug(100, "Function: DnsServerHandler.__init__") 350 if mode == 'add': 351 self.dnsServer = DnsServer() 352 else: 353 self.dnsServer= DnsServer.objects.get(ipaddress=dnsServer) 354 super(DnsServerHandler, self).__init__(request, mode) 355 356 def _add(self): 357 pdebug(100, "Function: DnsServerHandler._add") 358 _instance = self.form.save() 359 self.response = HttpResponseRedirect(reverse('exodus.views.viewList', \ 360 args=['dnsServer'])) 361 362 def _delete(self): 363 pdebug(100, "Function: DnsServerHandler._delete") 364 self.object.delete() 365 self.response = HttpResponseRedirect(reverse('exodus.views.viewList', \ 366 args=['dnsServer'])) 367 368 def _cancel(self): 369 pdebug(100, "Function: DnsServerHandler._cancel") 370 self.response = HttpResponseRedirect(reverse('exodus.views.viewList', \ 371 args=['dnsServer'])) 375 372 376 373 def genericModel(request, model, mode, object): 377 378 379 380 381 382 374 pdebug(100, "Function: genericModel %s, %s, %s" % (model, mode, object)) 375 """Wrapper, to get to the function needed""" 376 model = model[0].upper() + model[1::] 377 model = eval(model + 'Handler') 378 handler = model(request, object, mode) 379 return handler.render_to_response() 383 380 384 381 def viewList(request, model): 385 386 387 388 389 390 391 392 393 394 395 382 pdebug(100, "Function: viewList") 383 """Standard interface for simple overview pages, with view/edit/delete 384 buttons on it 385 """ 386 modelURL = model 387 model = model[0].upper() + model[1::] 388 modelName = model 389 model = eval(model) 390 objects = model.objects.all() 391 return render_to_response('viewList.html', {'objects': objects, \ 392 'modelURL' : modelURL, 'modelName' : modelName})
Note:
See TracChangeset
for help on using the changeset viewer.