Ejemplo n.º 1
0
class ConfigurableDataSourceFromAppForm(forms.Form):

    app_id = forms.ChoiceField()
    case_type = forms.ChoiceField()

    def __init__(self, domain, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.add_input(Submit('submit', _('Save Changes')))
        super(ConfigurableDataSourceFromAppForm, self).__init__(*args, **kwargs)
        apps = get_apps_in_domain(domain, full=True, include_remote=False)
        self.fields['app_id'] = forms.ChoiceField(
            label=_('Application'),
            choices=[(app._id, app.name) for app in apps]
        )
        self.fields['case_type'] = forms.ChoiceField(
            choices=[(ct, ct) for ct in set([c for app in apps for c in app.get_case_types() if c])]
        )

    def clean(self):
        cleaned_data = super(ConfigurableDataSourceFromAppForm, self).clean()
        app = Application.get(cleaned_data['app_id'])
        if cleaned_data['case_type'] not in app.get_case_types():
            raise ValidationError(_('Case type {} not found in application {}!'.format(
                cleaned_data['case_type'],
                app.name,
            )))
        # set the app property on the form so we don't have to go back to the DB for it
        # there may be a better way to do this.
        self.app = app
        return cleaned_data
Ejemplo n.º 2
0
class ConfigurableFormDataSourceFromAppForm(forms.Form):

    app_id = forms.ChoiceField()
    form_id = forms.ChoiceField()

    def __init__(self, domain, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.add_input(Submit('submit', _('Save Changes')))
        super(ConfigurableFormDataSourceFromAppForm, self).__init__(*args, **kwargs)
        apps = get_apps_in_domain(domain, full=True, include_remote=False)
        self.fields['app_id'] = forms.ChoiceField(
            label=_('Application'),
            choices=[(app._id, app.name) for app in apps]
        )
        self.fields['form_id'] = forms.ChoiceField(
            label=_('Module - Form'),
            choices=[(form.get_unique_id(), form.get_module().default_name() + ' - ' + form.default_name())
                     for form in set([form for app in apps for form in app.get_forms()])]
        )

    def clean(self):
        cleaned_data = super(ConfigurableFormDataSourceFromAppForm, self).clean()
        app = Application.get(cleaned_data['app_id'])
        form = Form.get_form(cleaned_data['form_id'])
        if form.get_app()._id != app._id:
            raise ValidationError(_('Form name {} not found in application {}').format(
                form.default_name(),
                app.name
            ))
        self.app = app
        self.form = form
        return cleaned_data
Ejemplo n.º 3
0
class DocumentFormBase(forms.Form):
    """
    HQ specific document base form. Loosely modeled off of Django's ModelForm
    """

    def __init__(self, instance=None, *args, **kwargs):
        self.instance = instance
        object_data = instance._doc if instance is not None else {}
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.add_input(Submit('submit', _('Save Changes')))
        super(DocumentFormBase, self).__init__(initial=object_data, *args, **kwargs)

    def save(self, commit=False):
        self.populate_instance(self.instance, self.cleaned_data)
        if commit:
            self.instance.save()
        return self.instance

    def populate_instance(self, instance, cleaned_data):
        for field in self.fields:
            setattr(instance, field, cleaned_data[field])
        return instance