models.py 4.57 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 45 46 47 48 49 50 51 52

class Request(TimeStampedModel):
    STATUSES = Choices(
        ('UNSEEN', _("unseen")),
        ('PENDING', _('pending')),
        ('ACCEPTED', _('accepted')),
        ('DECLINED', _('declined')),
    )
    status = CharField(choices=STATUSES, default=STATUSES.UNSEEN,
                       max_length=10)
    user = ForeignKey(User, verbose_name=_('user'))
    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
53 54 55 56 57

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

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

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

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

67
    def get_request_icon(self):
Kálmán Viktor committed
68 69 70 71 72 73 74 75 76 77 78 79 80
        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
81

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

    def accept(self):
        self.status = "ACCEPTED"
        self.save()

    def decline(self):
        self.status = "DECLINED"
        self.save()

Kálmán Viktor committed
98 99 100 101

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

102 103 104 105 106 107
    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
108 109 110 111 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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)])

    def accept(self):
        pass
        # self.instance.change_resources(xy=xy)


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

    def accept(self):
        pass


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
157 158 159
    def get_readable_level(self):
        return self.LEVELS[self.level]

Kálmán Viktor committed
160 161
    def accept(self):
        pass