Slice req is always validated because it's under user's project on which he is a PI
[unfold.git] / forge / models.py
1 # -*- coding:utf-8 -*-
2
3 from django.db import models
4
5 # Used for automatically delete file
6 from django.dispatch import receiver
7 import os
8
9 # Class to describe a service set on a host
10 class Service(models.Model):
11         serviceName = models.CharField(max_length=50)
12         servicePort = models.CharField(max_length=10)
13         host = models.ForeignKey('Host')
14
15         def __unicode__(self):
16                 if self.servicePort != u'':
17                         return u"%s on %s" % (self.serviceName, self.servicePort)
18                 else:
19                         return u"%s" % self.serviceName
20
21 # Class to describe an interface available on a host
22 class Interface(models.Model):
23         ip = models.CharField(max_length=16)
24         name = models.CharField(max_length=40)
25         host = models.ForeignKey('Host')
26
27         def __unicode__(self):
28                 return u"%s -> %s" % (self.name, self.ip)
29
30 # Class to describe a host
31 class Host(models.Model):
32         TYPE_CHOICES = (
33                 ('PUB', 'Public'),
34                 ('PRIV', 'Private'),
35                 ('COM', 'Common'),
36         )
37         hostname = models.CharField(max_length=100)
38         hostType = models.CharField(max_length=20, choices=TYPE_CHOICES, default='PRIV')
39         pleSlice = models.ForeignKey('Slice')
40         latitude = models.FloatField()
41         longitude = models.FloatField()
42
43         def __unicode__(self):
44                 return u"%s %s" % (self.hostname, self.hostType)
45
46 # Class to describe a slice (sliceName and reference to environment)
47 class Slice(models.Model):
48         sliceName = models.CharField(max_length=50)
49         environment = models.ForeignKey('Environment')
50
51         def __unicode__(self):
52                 return u"%s" % self.sliceName
53
54 # Class to describe a student environment (sshKey and reference to a course)
55 class Environment(models.Model):
56         sshKey = models.FileField(upload_to='ict_education/keys')
57         course = models.ForeignKey('Course')
58         confFile = models.FileField(upload_to='ict_education/xmlFiles')
59         linkFile = models.FileField(upload_to='ict_education/xmlFiles')
60         ready = models.BooleanField(default=False)
61
62 # function used to automatically delete the stored file when deleting the model from the database
63 @receiver(models.signals.post_delete, sender = Environment)
64 def environment_delete_ssh_key(sender, instance, **kwargs):
65         if instance.sshKey:
66                 if os.path.isfile(instance.sshKey.path):
67                         os.remove(instance.sshKey.path)
68         if instance.confFile:
69                 if os.path.isfile(instance.confFile.path):
70                         os.remove(instance.confFile.path)
71         if instance.linkFile:
72                 if os.path.isfile(instance.linkFile.path):
73                         os.remove(instance.linkFile.path)
74
75 # Class to describe a course (ie a set of environment) (reference to lab, mainKey used by the teacher to access all node)
76 class Course(models.Model):
77         lab = models.ForeignKey('Lab')
78         mainKey = models.FileField(upload_to='ict_education/keys', null = True)
79         ready = models.BooleanField(default=False)
80         sliceName = models.CharField(max_length=50)
81         
82         def __unicode__(self):
83                 return u"%s %s" % (self.lab, self.sliceName)
84
85 # function used to automatically delete the stored file when deleting the model from the database
86 @receiver(models.signals.post_delete, sender = Course)
87 def course_delete_main_ssh_key(sender, instance, **kwargs):
88         if instance.mainKey:
89                 if os.path.isfile(instance.mainKey.path):
90                         os.remove(instance.mainKey.path)
91
92 # Class to describe a lab
93 class Lab(models.Model):
94         title = models.CharField(max_length=100)
95         author = models.CharField(max_length=40)
96         subject = models.FileField(upload_to='ict_education/Labs/subjects')
97         configurationFile = models.FileField(upload_to='ict_education/Labs/xmlFiles')
98         linkFile = models.FileField(upload_to='ict_education/Labs/xmlFiles')
99
100         def __unicode__(self):
101                 return u"%s %s" % (self.title, self.author)
102
103 # function used to automatically delete the stored file when deleting the model from the database
104 @receiver(models.signals.post_delete, sender = Lab)
105 def lab_delete_files(sender, instance, **kwargs):
106         # Remove the subject
107         if instance.subject:
108                 if os.path.isfile(instance.subject.path):
109                         os.remove(instance.subject.path)
110         # Remove the configuration file
111         if instance.configurationFile:
112                 if os.path.isfile(instance.configurationFile.path):
113                         os.remove(instance.configurationFile.path)
114         # Remove the link file
115         if instance.linkFile:
116                 if os.path.isfile(instance.linkFile.path):
117                         os.remove(instance.linkFile.path)
118