2014-07-08 15:19:15 +00:00
|
|
|
from django import forms
|
2014-07-14 14:56:48 +00:00
|
|
|
from django.utils.html import escape
|
2014-07-08 15:19:15 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-07-14 14:56:48 +00:00
|
|
|
from djcelery.humanize import naturaldate
|
2014-07-08 15:19:15 +00:00
|
|
|
|
|
|
|
from orchestra.forms.widgets import ShowTextWidget, ReadOnlyWidget
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceForm(forms.ModelForm):
|
|
|
|
verbose_name = forms.CharField(label=_("Name"), widget=ShowTextWidget(bold=True),
|
|
|
|
required=False)
|
2014-07-09 16:17:43 +00:00
|
|
|
allocated = forms.IntegerField(label=_("Allocated"))
|
2014-07-16 15:20:16 +00:00
|
|
|
unit = forms.CharField(label=_("Unit"), widget=ShowTextWidget(), 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')
|
2014-07-08 15:19:15 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.resource = kwargs.pop('resource', None)
|
|
|
|
super(ResourceForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.resource:
|
|
|
|
self.fields['verbose_name'].initial = self.resource.verbose_name
|
2014-07-16 15:20:16 +00:00
|
|
|
self.fields['unit'].initial = self.resource.unit
|
2014-07-08 15:19:15 +00:00
|
|
|
if self.resource.ondemand:
|
2014-07-09 16:17:43 +00:00
|
|
|
self.fields['allocated'].required = False
|
|
|
|
self.fields['allocated'].widget = ReadOnlyWidget(None, '')
|
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
|
|
|
|
|
|
|
|
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)
|