summaryrefslogtreecommitdiffstats
path: root/tools/pharos-dashboard/dashboard/models.py
blob: 132ba7c6de0655126c8e1d5a328a7f033d014748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User


class Booking(models.Model):
    booking_id = models.AutoField(primary_key=True)
    # Bookings should be deleted before resources
    resource = models.ForeignKey('Resource', models.PROTECT)
    # delete Booking when user is deleted
    user = models.ForeignKey(User, models.CASCADE)
    start_date_time = models.DateTimeField()
    end_date_time = models.DateTimeField()
    creation = models.DateTimeField(auto_now=True)
    purpose = models.CharField(max_length=300)

    class Meta:
        db_table = 'booking'

    # check for conflicting bookings before saving
    def save(self, *args, **kwargs):
        # conflicts end after booking starts, and start before booking ends
        conflicting_bookings = Booking.objects.filter(resource_id=self.resource_id)
        conflicting_bookings = conflicting_bookings.filter(end_date_time__gt=self.start_date_time)
        conflicting_bookings = conflicting_bookings.filter(start_date_time__lt=self.end_date_time)
        # we may change a booking, so it is not a conflict
        conflicting_bookings = conflicting_bookings.exclude(booking_id=self.booking_id)
        if conflicting_bookings.count() > 0:
            raise ValueError('This booking overlaps with another booking')
        super(Booking, self).save(*args, **kwargs)


def __str__(self):
    return 'Booking: ' + str(self.resource)


class Pod(models.Model):
    pod_id = models.AutoField(primary_key=True)
    # Delete Pod with resource
    resource = models.ForeignKey('Resource', models.CASCADE)
    chassis = models.CharField(max_length=500, blank=True, null=True)

    class Meta:
        db_table = 'pod'

    def __str__(self):
        if self.chassis is None:
            return str(self.pod_id) + ' ' + str(self.resource)
        return str(self.pod_id) + ' ' + self.chassis


class Resource(models.Model):
    resource_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, unique=True)
    slavename = models.CharField(max_length=50, blank=True, null=True)
    description = models.CharField(max_length=300, blank=True, null=True)
    url = models.CharField(max_length=100, blank=True, null=True)
    bookable = models.BooleanField(default=False)
    active = models.BooleanField(default=True)

    class Meta:
        db_table = 'resource'

    def __str__(self):
        return self.name


class Server(models.Model):
    server_id = models.AutoField(primary_key=True)
    resource = models.ForeignKey(Resource, models.DO_NOTHING)
    model = models.CharField(max_length=200, blank=True, null=True)
    cpu = models.CharField(max_length=200, blank=True, null=True)
    ram = models.CharField(max_length=200, blank=True, null=True)
    storage = models.CharField(max_length=200, blank=True, null=True)
    count = models.IntegerField(default=1)

    class Meta:
        db_table = 'server'


class UserResource(models.Model):
    user_resource_id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, models.CASCADE)
    # Delete if Resource is deleted
    resource = models.ForeignKey(Resource, models.CASCADE)

    class Meta:
        db_table = 'user_resource'