models.py 4.96 KB
Newer Older
Kálmán Viktor committed
1 2 3
from django.db.models import (
    Model, CharField, IntegerField, TextField, ForeignKey, ManyToManyField,
)
Kálmán Viktor committed
4 5
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
Kálmán Viktor committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from model_utils.models import TimeStampedModel
from model_utils import Choices

from vm.models import Instance, InstanceTemplate, Lease


class RequestAction(Model):

    def accept(self):
        raise NotImplementedError

Kálmán Viktor committed
22 23 24
    class Meta:
        abstract = True

Kálmán Viktor committed
25 26 27 28 29 30 31

class RequestType(Model):
    name = CharField(max_length=25)

    def __unicode__(self):
        return self.name

Kálmán Viktor committed
32 33 34
    class Meta:
        abstract = True

Kálmán Viktor committed
35 36 37 38 39 40 41 42 43 44

class Request(TimeStampedModel):
    STATUSES = Choices(
        ('UNSEEN', _("unseen")),
        ('PENDING', _('pending')),
        ('ACCEPTED', _('accepted')),
        ('DECLINED', _('declined')),
    )
    status = CharField(choices=STATUSES, default=STATUSES.UNSEEN,
                       max_length=10)
45 46
    user = ForeignKey(User, related_name="user")
    closed_by = ForeignKey(User, related_name="closed_by", null=True)
Kálmán Viktor committed
47 48 49 50 51 52 53
    TYPES = Choices(
        ('resource', _('resource request')),
        ('lease', _("lease request")),
        ('template', _("template access")),
    )
    type = CharField(choices=TYPES, max_length=10)
    reason = TextField(help_text="szia")
Kálmán Viktor committed
54 55 56 57 58

    content_type = ForeignKey(ContentType)
    object_id = IntegerField()
    action = GenericForeignKey("content_type", "object_id")

59 60 61
    def get_absolute_url(self):
        return reverse("request.views.request-detail", kwargs={'pk': self.pk})

Kálmán Viktor committed
62 63 64
    def get_readable_status(self):
        return self.STATUSES[self.status]

65 66 67
    def get_readable_type(self):
        return self.TYPES[self.type]

68
    def get_request_icon(self):
Kálmán Viktor committed
69 70 71 72 73 74 75 76 77 78 79 80 81
        return {
            'resource': "tasks",
            'lease': "clock-o",
            'template': "puzzle-piece"
        }.get(self.type)

    def get_effect(self):
        return {
            "UNSEEN": "primary",
            "PENDING": "warning",
            "ACCEPTED": "success",
            "DECLINED": "danger",
        }.get(self.status)
Kálmán Viktor committed
82

83 84 85 86 87 88 89 90
    def get_status_icon(self):
        return {
            "UNSEEN": "eye-slash",
            "PENDING": "exclamation-triangle",
            "ACCEPTED": "check",
            "DECLINED": "times",
        }.get(self.status)

91 92
    def accept(self, user):
        self.action.accept(user)
93
        self.status = "ACCEPTED"
94
        self.closed_by = user
95 96
        self.save()

97
    def decline(self, user):
98
        self.status = "DECLINED"
99
        self.closed_by = user
100 101
        self.save()

Kálmán Viktor committed
102 103 104 105

class LeaseType(RequestType):
    lease = ForeignKey(Lease)

106 107 108 109 110 111
    def __unicode__(self):
        return _("%(name)s (suspend: %(s)s, remove: %(r)s)") % {
            'name': self.name,
            's': self.lease.get_readable_suspend_time(),
            'r': self.lease.get_readable_delete_time()}

Kálmán Viktor committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    def get_absolute_url(self):
        return reverse("request.views.lease-type-detail",
                       kwargs={'pk': self.pk})


class TemplateAccessType(RequestType):
    templates = ManyToManyField(InstanceTemplate)

    def get_absolute_url(self):
        return reverse("request.views.template-type-detail",
                       kwargs={'pk': self.pk})


class ResourceChangeAction(RequestAction):
    instance = ForeignKey(Instance)
    num_cores = IntegerField(verbose_name=_('number of cores'),
                             help_text=_('Number of virtual CPU cores '
                                         'available to the virtual machine.'),
                             validators=[MinValueValidator(0)])
    ram_size = IntegerField(verbose_name=_('RAM size'),
                            help_text=_('Mebibytes of memory.'),
                            validators=[MinValueValidator(0)])
    priority = IntegerField(verbose_name=_('priority'),
                            help_text=_('CPU priority.'),
                            validators=[MinValueValidator(0)])

138 139
    def accept(self, user):
        self.instance.resources_request.async(user=user, resource_request=self)
Kálmán Viktor committed
140 141 142 143 144 145


class ExtendLeaseAction(RequestAction):
    instance = ForeignKey(Instance)
    lease_type = ForeignKey(LeaseType)

146 147 148
    def accept(self, user):
        self.instance.renew(lease=self.lease_type.lease, save=True, force=True,
                            user=user)
Kálmán Viktor committed
149 150 151 152 153 154 155 156 157 158 159 160


class TemplateAccessAction(RequestAction):
    template_type = ForeignKey(TemplateAccessType)
    LEVELS = Choices(
        ('user', _('user')),
        ('operator', _('operator')),
    )
    level = CharField(choices=LEVELS, default=LEVELS.user,
                      max_length=10)
    user = ForeignKey(User)

Kálmán Viktor committed
161 162 163
    def get_readable_level(self):
        return self.LEVELS[self.level]

164 165 166
    def accept(self, user):
        for t in self.template_type.templates.all():
            t.set_user_level(self.user, self.level)