2 # Create your views here.
4 # Several import from the django facilities
5 from django.template import RequestContext
6 from django.shortcuts import render, get_object_or_404, render_to_response, redirect
7 from django.template import RequestContext
8 from django.core.urlresolvers import reverse
9 from django.core.files import File
11 # Import of the forms and models
12 from forge.models import *
13 from forge.forms import *
15 # Import of the task and TaskState (used to link task in the database)
16 from forge.tasks import *
17 from djcelery.models import TaskState
20 from django.conf import settings
24 # Import class used to create the environment
25 from forge.script.request import TransformRawXml
27 from myslice.theme import ThemeView
28 from unfold.loginrequired import FreeAccessView
30 class CreateCourseViev (FreeAccessView, ThemeView):
31 template_name = 'create-course.html'
33 def get (self, request, slicename, state=None):
34 username = self.request.user
35 split_slicename = slicename.split('.')
36 ple_slicename = split_slicename[0] + '8' + split_slicename[1] + '_' + split_slicename[2]
37 if not Course.objects.get(sliceName = ple_slicename).exists():
38 if request.method == 'POST':
39 form = courseForm(request.POST)
41 result = taskCreateCourse.delay(form)
43 labs = Lab.objects.all()
45 return render_to_response(self.template, { 'theme' : self.theme,'slicename':slicename, 'ple_slicename':ple_slicename, 'username':username, 'form': form }, context_instance=RequestContext(request))
47 def mainView(request):
48 return render(request, 'mainView.html', locals())
51 def createLab(request):
52 if request.method == 'POST':
53 form = LabForm(request.POST, request.FILES)
56 title = form['title'].value(),
57 author = form['author'].value(),
58 subject = request.FILES['subject'],
59 configurationFile = request.FILES['configurationFile'],
60 linkFile = request.FILES['linkFile'],
65 labs = Lab.objects.all()
66 return render_to_response('createLab.html', {'labs': labs, 'form':form}, context_instance=RequestContext(request))
68 def labDetails(request, id):
70 lab = Lab.objects.get(id=id)
71 except Lab.DoesNotExist:
73 return render(request, 'labDetails.html', locals())
75 def deleteLab(request, id):
77 lab = Lab.objects.get(id=id)
78 except Lab.DoesNotExist:
81 return redirect('forge.views.createLab')
83 def createCourse(request):
84 if request.method == 'POST':
85 form = courseForm(request.POST)
87 result = taskCreateCourse.delay(form)
89 labs = Lab.objects.all()
91 return render(request, 'createCourse.html', locals())
93 def installEnvironment(request, id):
94 task = taskInstallEnvironment.delay(Environment.objects.get(id=id))
95 return redirect('forge.views.listCourse')
97 def renewSlice(request, id):
98 task = taskRenewSlice.delay(Course.objects.get(id=id))
99 return redirect('forge.views.listCourse')
101 def deleteCourse(request, id):
102 task = taskDeleteCourse.delay(Course.objects.get(id=id))
103 return redirect('forge.views.listCourse')
105 def listCourse(request):
106 courses = Course.objects.all()
107 environments = Environment.objects.all()
108 slices = Slice.objects.all()
109 hosts = Host.objects.all()
110 services = Service.objects.all()
112 for course in courses:
113 renderEnvironment = []
115 for environment in course.environment_set.all():
117 for pleSlice in environment.slice_set.all():
119 for host in pleSlice.host_set.all():
121 for service in host.service_set.all():
122 renderService.append(service)
124 for interface in host.interface_set.all():
125 renderInterface.append(interface)
126 renderHost.append([host, renderService, renderInterface])
127 renderSlice.append([pleSlice, renderHost])
128 renderEnvironment.append([environment, renderSlice])
129 renderCourses.append([course, renderEnvironment])
132 return render(request, 'listCourse.html', {'courses': renderCourses})