diff --git a/circle/dashboard/forms.py b/circle/dashboard/forms.py index 85f67d7..56f21a3 100644 --- a/circle/dashboard/forms.py +++ b/circle/dashboard/forms.py @@ -697,16 +697,18 @@ class LeaseForm(forms.ModelForm): class VmRenewForm(forms.Form): + force = forms.BooleanField(required=False, label=_( + "Set expiration times even if they are shorter than " + "the current value.")) + def __init__(self, *args, **kwargs): choices = kwargs.pop('choices') default = kwargs.pop('default') super(VmRenewForm, self).__init__(*args, **kwargs) - self.fields['lease'] = forms.ModelChoiceField(queryset=choices, - initial=default, - required=False, - empty_label=None, - label=_('Length')) + self.fields.insert(0, 'lease', forms.ModelChoiceField( + queryset=choices, initial=default, required=False, + empty_label=None, label=_('Length'))) if len(choices) < 2: self.fields['lease'].widget = HiddenInput() diff --git a/circle/vm/operations.py b/circle/vm/operations.py index 07bd770..d9fd9ef 100644 --- a/circle/vm/operations.py +++ b/circle/vm/operations.py @@ -28,7 +28,9 @@ from sizefield.utils import filesizeformat from celery.exceptions import TimeLimitExceeded -from common.models import create_readable, humanize_exception +from common.models import ( + create_readable, humanize_exception, HumanReadableException +) from common.operations import Operation, register_operation from .tasks.local_tasks import ( abortable_async_instance_operation, abortable_async_node_operation, @@ -716,10 +718,24 @@ class RenewOperation(InstanceOperation): required_perms = () concurrency_check = False - def _operation(self, lease=None): - (self.instance.time_of_suspend, - self.instance.time_of_delete) = self.instance.get_renew_times(lease) + def _operation(self, activity, lease=None, force=False): + suspend, delete = self.instance.get_renew_times(lease) + if (not force and suspend and self.instance.time_of_suspend and + suspend < self.instance.time_of_suspend): + raise HumanReadableException.create(ugettext_noop( + "Renewing the machine with the selected lease would result " + "in its suspension time get earlier than before.")) + if (not force and delete and self.instance.time_of_delete and + delete < self.instance.time_of_delete): + raise HumanReadableException.create(ugettext_noop( + "Renewing the machine with the selected lease would result " + "in its delete time get earlier than before.")) + self.instance.time_of_suspend = suspend + self.instance.time_of_delete = delete self.instance.save() + activity.result = create_readable(ugettext_noop( + "Renewed to suspend at %(suspend)s and destroy at %(delete)s."), + suspend=suspend, delete=delete) register_operation(RenewOperation)