new user logo width=80px
[unfold.git] / forge / views.py
1 # -*- coding:utf-8 -*-
2 # Create your views here.
3
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
10
11 # Import of the forms and models
12 from forge.models import *
13 from forge.forms import *
14
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
18
19 # Import the settings
20 from django.conf import settings
21
22 import os
23
24 # Import class used to create the environment
25 from forge.script.request import TransformRawXml
26
27 from myslice.theme import ThemeView
28 from unfold.loginrequired import FreeAccessView
29
30 class CreateCourseViev (FreeAccessView, ThemeView):
31         template_name = 'create-course.html'
32
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)
40                                 if form.is_valid():
41                                         result = taskCreateCourse.delay(form)
42                         else:
43                                 labs = Lab.objects.all()
44                                 form = courseForm()
45                 return render_to_response(self.template, { 'theme' : self.theme,'slicename':slicename, 'ple_slicename':ple_slicename, 'username':username, 'form': form }, context_instance=RequestContext(request))
46
47 def mainView(request):
48         return render(request, 'mainView.html', locals())
49
50
51 def createLab(request):
52         if request.method == 'POST':
53                 form = LabForm(request.POST, request.FILES)
54                 if form.is_valid():
55                         newLab = Lab(
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'],
61                         )
62                         newLab.save()
63         else:
64                 form = LabForm()
65         labs = Lab.objects.all()
66         return render_to_response('createLab.html', {'labs': labs, 'form':form}, context_instance=RequestContext(request))
67
68 def labDetails(request, id):
69         try:
70                 lab = Lab.objects.get(id=id)
71         except Lab.DoesNotExist:
72                 raise Http404
73         return render(request, 'labDetails.html', locals())
74
75 def deleteLab(request, id):
76         try:
77                 lab = Lab.objects.get(id=id)
78         except Lab.DoesNotExist:
79                 raise Http404
80         lab.delete()
81         return redirect('forge.views.createLab')
82
83 def createCourse(request):
84         if request.method == 'POST':
85                 form = courseForm(request.POST)
86                 if form.is_valid():
87                         result = taskCreateCourse.delay(form)
88         else:
89                 labs = Lab.objects.all()
90                 form = courseForm()
91         return render(request, 'createCourse.html', locals())
92
93 def installEnvironment(request, id):
94         task = taskInstallEnvironment.delay(Environment.objects.get(id=id))
95         return redirect('forge.views.listCourse')
96
97 def renewSlice(request, id):
98         task = taskRenewSlice.delay(Course.objects.get(id=id))
99         return redirect('forge.views.listCourse')
100
101 def deleteCourse(request, id):
102         task = taskDeleteCourse.delay(Course.objects.get(id=id))
103         return redirect('forge.views.listCourse')
104
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()
111         renderCourses = []
112         for course in courses:
113                 renderEnvironment = []
114                 if course.ready:
115                         for environment in course.environment_set.all():
116                                 renderSlice = []
117                                 for pleSlice in environment.slice_set.all():
118                                         renderHost = []
119                                         for host in pleSlice.host_set.all():
120                                                 renderService = []
121                                                 for service in host.service_set.all():
122                                                         renderService.append(service)
123                                                 renderInterface = []
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])
130
131         
132         return render(request, 'listCourse.html', {'courses': renderCourses})