2014-07-08 15:19:15 +00:00
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2015-04-27 12:24:17 +00:00
|
|
|
from orchestra.forms import ReadOnlyFormMixin
|
|
|
|
from orchestra.forms.widgets import SpanWidget
|
2014-07-08 15:19:15 +00:00
|
|
|
|
|
|
|
|
2015-04-27 12:24:17 +00:00
|
|
|
class ResourceForm(ReadOnlyFormMixin, forms.ModelForm):
|
2014-11-13 15:34:00 +00:00
|
|
|
verbose_name = forms.CharField(label=_("Name"), required=False,
|
2015-04-27 12:24:17 +00:00
|
|
|
widget=SpanWidget(tag='<b>'))
|
2015-02-27 16:57:39 +00:00
|
|
|
allocated = forms.DecimalField(label=_("Allocated"))
|
2015-04-27 12:24:17 +00:00
|
|
|
unit = forms.CharField(label=_("Unit"), required=False)
|
2014-07-08 15:19:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
2014-07-16 15:20:16 +00:00
|
|
|
fields = ('verbose_name', 'used', 'last_update', 'allocated', 'unit')
|
2015-04-27 12:24:17 +00:00
|
|
|
readonly_fields = ('verbose_name', 'unit')
|
2014-07-08 15:19:15 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.resource = kwargs.pop('resource', None)
|
2015-04-27 12:24:17 +00:00
|
|
|
if self.resource:
|
|
|
|
initial = kwargs.get('initial', {})
|
|
|
|
initial.update({
|
|
|
|
'verbose_name': self.resource.get_verbose_name(),
|
|
|
|
'unit': self.resource.unit,
|
|
|
|
})
|
|
|
|
kwargs['initial'] = initial
|
2014-07-08 15:19:15 +00:00
|
|
|
super(ResourceForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.resource:
|
2014-09-26 15:05:20 +00:00
|
|
|
if self.resource.on_demand:
|
2014-07-09 16:17:43 +00:00
|
|
|
self.fields['allocated'].required = False
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields['allocated'].widget = SpanWidget(original=None, display='')
|
2014-07-08 15:19:15 +00:00
|
|
|
else:
|
2014-07-09 16:17:43 +00:00
|
|
|
self.fields['allocated'].required = True
|
|
|
|
self.fields['allocated'].initial = self.resource.default_allocation
|
2015-04-27 12:24:17 +00:00
|
|
|
|
2014-10-27 13:29:02 +00:00
|
|
|
# def has_changed(self):
|
|
|
|
# """ Make sure resourcedata objects are created for all resources """
|
|
|
|
# if not self.instance.pk:
|
|
|
|
# return True
|
|
|
|
# return super(ResourceForm, self).has_changed()
|
2014-07-08 15:19:15 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.instance.resource_id = self.resource.pk
|
|
|
|
return super(ResourceForm, self).save(*args, **kwargs)
|