forms.py 3.07 KB
Newer Older
Kálmán Viktor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.
17 18
from django.forms import (
    ModelForm, ModelChoiceField, ChoiceField, Form, CharField, RadioSelect,
19
    Textarea,
20
)
Kálmán Viktor committed
21
from django.utils.translation import ugettext_lazy as _
22 23
from django.template import RequestContext
from django.template.loader import render_to_string
Kálmán Viktor committed
24 25 26 27 28

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

from request.models import (
29
    LeaseType, TemplateAccessType, TemplateAccessAction,
Kálmán Viktor committed
30
)
31
from dashboard.forms import VmResourcesForm
Kálmán Viktor committed
32 33 34 35 36 37 38 39 40 41 42 43


class LeaseTypeForm(ModelForm):
    @property
    def helper(self):
        helper = FormHelper()
        helper.add_input(Submit("submit", _("Save"),
                                css_class="btn btn-success", ))
        return helper

    class Meta:
        model = LeaseType
Kálmán Viktor committed
44
        fields = ["name", "lease", ]
Kálmán Viktor committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59


class TemplateAccessTypeForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(TemplateAccessTypeForm, self).__init__(*args, **kwargs)

    @property
    def helper(self):
        helper = FormHelper()
        helper.add_input(Submit("submit", _("Save"),
                                css_class="btn btn-success", ))
        return helper

    class Meta:
        model = TemplateAccessType
Kálmán Viktor committed
60
        fields = ["name", "templates", ]
Kálmán Viktor committed
61 62


63 64 65 66 67
class InitialFromFileMixin(object):
    def __init__(self, *args, **kwargs):
        request = kwargs.pop("request", None)
        super(InitialFromFileMixin, self).__init__(*args, **kwargs)

68
        self.initial['message'] = render_to_string(
69 70 71 72 73 74
            self.initial_template,
            RequestContext(request, {}),
        )


class TemplateRequestForm(InitialFromFileMixin, Form):
Kálmán Viktor committed
75
    template = ModelChoiceField(TemplateAccessType.objects.all(),
76
                                label=_("Template share"))
Kálmán Viktor committed
77 78
    level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect,
                        initial=TemplateAccessAction.LEVELS.user)
79
    message = CharField(widget=Textarea, label=_("Message"))
80 81

    initial_template = "request/initials/template.html"
82 83


84 85
class LeaseRequestForm(InitialFromFileMixin, Form):
    lease = ModelChoiceField(LeaseType.objects.all(), label=_("Lease"))
86
    message = CharField(widget=Textarea)
87

88 89
    initial_template = "request/initials/lease.html"

Kálmán Viktor committed
90

91
class ResourceRequestForm(InitialFromFileMixin, VmResourcesForm):
92
    message = CharField(widget=Textarea)
93 94

    initial_template = "request/initials/resources.html"