forms.py 3.03 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 19
from django.forms import (
    ModelForm, ModelChoiceField, ChoiceField, Form, CharField, RadioSelect,
)
Kálmán Viktor committed
20 21
from django import forms
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


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


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


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

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


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

    initial_template = "request/initials/template.html"
80 81


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

86 87
    initial_template = "request/initials/lease.html"

Kálmán Viktor committed
88

89
class ResourceRequestForm(InitialFromFileMixin, VmResourcesForm):
90
    message = CharField(widget=forms.Textarea)
91 92

    initial_template = "request/initials/resources.html"