diff --git a/progit/hooks/__init__.py b/progit/hooks/__init__.py index 9f35565..69be6b0 100644 --- a/progit/hooks/__init__.py +++ b/progit/hooks/__init__.py @@ -10,10 +10,29 @@ import os import shutil +import wtforms from progit import APP, get_repo_path +class RequiredIf(wtforms.validators.Required): + """ Wtforms validator setting a field as required if another field + has a value. + """ + + def __init__(self, other_field_name, *args, **kwargs): + self.other_field_name = other_field_name + super(RequiredIf, self).__init__(*args, **kwargs) + + def __call__(self, form, field): + other_field = form._fields.get(self.other_field_name) + if other_field is None: + raise Exception( + 'no field named "%s" in form' % self.other_field_name) + if bool(other_field.data): + super(RequiredIf, self).__call__(form, field) + + class BaseHook(object): ''' Base class for progit's hooks. '''