Skip to content

Commit

Permalink
URL expression to use the Django URL dispatcher in views
Browse files Browse the repository at this point in the history
  • Loading branch information
ainoniwa committed Feb 13, 2015
1 parent b6a9c66 commit f4272fb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 29 deletions.
3 changes: 2 additions & 1 deletion console/views.py
Expand Up @@ -3,6 +3,7 @@
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from instance.models import Instance
from vrtManager.instance import wvmInstance
Expand All @@ -16,7 +17,7 @@ def console(request):
Console instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

if request.method == 'GET':
token = request.GET.get('token', '')
Expand Down
7 changes: 4 additions & 3 deletions create/views.py
Expand Up @@ -2,6 +2,7 @@
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from servers.models import Compute
from create.models import Flavor
Expand All @@ -19,7 +20,7 @@ def create(request, host_id):
Create new instance.
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

conn = None
errors = []
Expand Down Expand Up @@ -79,7 +80,7 @@ def create(request, host_id):
else:
try:
conn._defineXML(xml)
return HttpResponseRedirect('/instance/%s/%s' % (host_id, name))
return HttpResponseRedirect(reverse('instance', args=[host_id, name]))
except libvirtError as err:
errors.append(err.message)
if 'create' in request.POST:
Expand Down Expand Up @@ -127,7 +128,7 @@ def create(request, host_id):
uuid, volumes, data['networks'], data['virtio'], data['mac'])
create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid)
create_instance.save()
return HttpResponseRedirect('/instance/%s/%s/' % (host_id, data['name']))
return HttpResponseRedirect(reverse('instance', args=[host_id, data['name']]))
except libvirtError as err:
if data['hdd_size']:
conn.delete_volume(volumes.keys()[0])
Expand Down
5 changes: 3 additions & 2 deletions hostdetail/views.py
Expand Up @@ -3,6 +3,7 @@
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
import json
import time

Expand All @@ -16,7 +17,7 @@ def hostusage(request, host_id):
Return Memory and CPU Usage
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

points = 5
datasets = {}
Expand Down Expand Up @@ -105,7 +106,7 @@ def overview(request, host_id):
Overview page.
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
time_refresh = TIME_JS_REFRESH
Expand Down
15 changes: 8 additions & 7 deletions instance/views.py
Expand Up @@ -6,6 +6,7 @@
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
import json
import time

Expand All @@ -23,7 +24,7 @@ def instusage(request, host_id, vname):
Return instance usage
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

cookies = {}
datasets = {}
Expand Down Expand Up @@ -263,7 +264,7 @@ def insts_status(request, host_id):
Instances block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
instances = []
Expand Down Expand Up @@ -300,7 +301,7 @@ def instances(request, host_id):
Instances block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
instances = []
Expand Down Expand Up @@ -371,7 +372,7 @@ def instance(request, host_id, vname):
Instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

def show_clone_disk(disks):
clone_disk = []
Expand Down Expand Up @@ -478,7 +479,7 @@ def show_clone_disk(disks):
conn.delete_disk()
finally:
conn.delete()
return HttpResponseRedirect('/instances/%s/' % host_id)
return HttpResponseRedirect(reverse('instances', args=[host_id]))
if 'snapshot' in request.POST:
name = request.POST.get('name', '')
conn.create_snapshot(name)
Expand Down Expand Up @@ -563,7 +564,7 @@ def show_clone_disk(disks):
conn_migrate.moveto(conn, vname, live, unsafe, xml_del)
conn_migrate.define_move(vname)
conn_migrate.close()
return HttpResponseRedirect('/instance/%s/%s' % (compute_id, vname))
return HttpResponseRedirect(reverse('instance', args=[compute_id, vname]))
if 'delete_snapshot' in request.POST:
snap_name = request.POST.get('name', '')
conn.snapshot_delete(snap_name)
Expand All @@ -583,7 +584,7 @@ def show_clone_disk(disks):
clone_data[post] = request.POST.get(post, '')

conn.clone_instance(clone_data)
return HttpResponseRedirect('/instance/%s/%s' % (host_id, clone_data['name']))
return HttpResponseRedirect(reverse('instance', args=[host_id, clone_data['name']]))

conn.close()

Expand Down
7 changes: 4 additions & 3 deletions interfaces/views.py
@@ -1,6 +1,7 @@
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse

from servers.models import Compute
from interfaces.forms import AddInterface
Expand All @@ -16,7 +17,7 @@ def interfaces(request, host_id):
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/')
return HttpResponseRedirect(reverse('index'))

errors = []
ifaces_all = []
Expand Down Expand Up @@ -59,7 +60,7 @@ def interface(request, host_id, iface):
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/')
return HttpResponseRedirect(reverse('index'))

errors = []
ifaces_all = []
Expand Down Expand Up @@ -90,7 +91,7 @@ def interface(request, host_id, iface):
return HttpResponseRedirect(request.get_full_path())
if 'delete' in request.POST:
conn.delete_iface()
return HttpResponseRedirect('/interfaces/%s' % host_id)
return HttpResponseRedirect(reverse('interfaces', args=[host_id]))
conn.close()
except libvirtError as err:
errors.append(err)
Expand Down
9 changes: 5 additions & 4 deletions networks/views.py
Expand Up @@ -2,6 +2,7 @@
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from servers.models import Compute
from networks.forms import AddNetPool
Expand All @@ -17,7 +18,7 @@ def networks(request, host_id):
Networks block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
compute = Compute.objects.get(id=host_id)
Expand Down Expand Up @@ -47,7 +48,7 @@ def networks(request, host_id):
if not errors:
conn.create_network(data['name'], data['forward'], gateway, netmask,
dhcp, data['bridge_name'], data['openvswitch'], data['fixed'])
return HttpResponseRedirect('/network/%s/%s/' % (host_id, data['name']))
return HttpResponseRedirect(reverse('network', args=[host_id, data['name']]))
conn.close()
except libvirtError as err:
errors.append(err)
Expand All @@ -60,7 +61,7 @@ def network(request, host_id, pool):
Networks block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
compute = Compute.objects.get(id=host_id)
Expand Down Expand Up @@ -99,7 +100,7 @@ def network(request, host_id, pool):
if 'delete' in request.POST:
try:
conn.delete()
return HttpResponseRedirect('/networks/%s/' % host_id)
return HttpResponseRedirect(reverse('networks', args=[host_id]))
except libvirtError as error_msg:
errors.append(error_msg.message)
if 'set_autostart' in request.POST:
Expand Down
3 changes: 2 additions & 1 deletion secrets/views.py
@@ -1,6 +1,7 @@
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse

from servers.models import Compute
from secrets.forms import AddSecret
Expand All @@ -15,7 +16,7 @@ def secrets(request, host_id):
Networks block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
secrets_all = []
Expand Down
9 changes: 5 additions & 4 deletions servers/views.py
@@ -1,6 +1,7 @@
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse

from servers.models import Compute
from instance.models import Instance
Expand All @@ -17,17 +18,17 @@ def index(request):
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))
else:
return HttpResponseRedirect('/servers')
return HttpResponseRedirect(reverse('servers_list'))


def servers_list(request):
"""
Servers page.
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

def get_hosts_status(hosts):
"""
Expand Down Expand Up @@ -124,7 +125,7 @@ def infrastructure(request):
Infrastructure page.
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

compute = Compute.objects.filter()
hosts_vms = {}
Expand Down
9 changes: 5 additions & 4 deletions storages/views.py
Expand Up @@ -2,6 +2,7 @@
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from servers.models import Compute
from storages.forms import AddStgPool, AddImage, CloneImage
Expand All @@ -16,7 +17,7 @@ def storages(request, host_id):
Storage pool block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

errors = []
compute = Compute.objects.get(id=host_id)
Expand Down Expand Up @@ -55,7 +56,7 @@ def storages(request, host_id):
data['source_format'], data['target'])
else:
conn.create_storage(data['stg_type'], data['name'], data['source'], data['target'])
return HttpResponseRedirect('/storage/%s/%s/' % (host_id, data['name']))
return HttpResponseRedirect(reverse('storage', args=[host_id, data['name']]))
conn.close()
except libvirtError as err:
errors.append(err)
Expand All @@ -68,7 +69,7 @@ def storage(request, host_id, pool):
Storage pool block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
return HttpResponseRedirect(reverse('login'))

def handle_uploaded_file(path, f_name):
target = path + '/' + str(f_name)
Expand Down Expand Up @@ -125,7 +126,7 @@ def handle_uploaded_file(path, f_name):
if 'delete' in request.POST:
try:
conn.delete()
return HttpResponseRedirect('/storages/%s/' % host_id)
return HttpResponseRedirect(reverse('storages', args=[host_id]))
except libvirtError as error_msg:
errors.append(error_msg.message)
if 'set_autostart' in request.POST:
Expand Down

0 comments on commit f4272fb

Please sign in to comment.