Beispiel #1
0
def is_checkbox(field):
    """is checkbox"""
    field = getattr(
        field, 'field',
        field)  # get the field attribute of the field or the field itself
    if hasattr(field, 'widget'):
        return field.widget.__class__.__name__ == floppyforms.CheckboxInput(
        ).__class__.__name__
    return False
Beispiel #2
0
class PhotoGrid(forms.ModelForm):
    fastUpload = forms.BooleanField(
        widget=forms.CheckboxInput(attrs={'class': 'fastUploadClass'}),
        required=False,
        initial=False)

    #rodalref = forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'boolRodRef'}), required=False)
    class Meta:
        model = Report
        fields = ('hide', )
Beispiel #3
0
    def __init__(self, user, bt_ids, submit, *args, **kwargs):
        super(BankTransactionListForm, self).__init__(*args, **kwargs)

        self.fields['tags'].queryset = (
            BankTransactionTag.objects.get_user_tags_queryset(user))
        self.fields['reconciled'].widget.choices[0] = ('1', _('Reconciled?'))

        for pk in bt_ids:
            self.fields['banktransaction_' + str(pk)] = forms.BooleanField(
                required=False,
                widget=forms.CheckboxInput(attrs={'data-id': pk}))

        choices = ()
        if user.has_perm('banktransactions.change_banktransaction'):
            choices += (
                ('reconcile', _('Reconcile')),
                ('unreconcile', _('Unreconcile')),
            )
        if user.has_perm('banktransactions.delete_banktransaction'):
            choices += (('delete', _('Delete')), )
        if choices:
            self.fields['operation'].choices = choices

        self._submit = submit
Beispiel #4
0
class RequestForm(forms.Form):
    public_body = forms.CharField(
        widget=PublicBodySelect,
        label=_("Search for a topic or a public body:"),
        required=False)
    subject = forms.CharField(
        label=_("Subject"),
        max_length=230,
        widget=forms.TextInput(attrs={
            'placeholder': _("Subject"),
            "class": "form-control"
        }))
    body = forms.CharField(
        label=_("Body"),
        widget=forms.Textarea(
            attrs={
                'placeholder': _("Specify your request here..."),
                "class": "form-control"
            }))
    full_text = forms.BooleanField(
        required=False,
        initial=False,
        label=_("Don't wrap in template"),
        widget=forms.CheckboxInput(attrs={'tabindex': '-1'}))
    public = forms.BooleanField(
        required=False,
        initial=True,
        label=_("This request is public."),
        help_text=_(
            "If you don't want your request to be public right now,"
            " uncheck this. You can always decide to make it public later."))
    reference = forms.CharField(widget=forms.HiddenInput, required=False)
    redirect_url = forms.CharField(widget=forms.HiddenInput, required=False)
    hide_public = forms.BooleanField(widget=forms.HiddenInput,
                                     initial=False,
                                     required=False)
    hide_similar = forms.BooleanField(widget=forms.HiddenInput,
                                      initial=False,
                                      required=False)

    def __init__(self,
                 user=None,
                 list_of_laws=(),
                 default_law=None,
                 hide_law_widgets=True,
                 **kwargs):
        super(RequestForm, self).__init__(**kwargs)
        self.user = user
        self.list_of_laws = list_of_laws
        self.indexed_laws = dict([(l.pk, l) for l in self.list_of_laws])
        self.default_law = default_law

        self.fields["public_body"].widget.set_initial_jurisdiction(
            kwargs.get('initial', {}).pop('jurisdiction', None))
        self.fields["public_body"].widget.set_initial_search(
            kwargs.get('initial', {}).pop('public_body_search', None))
        self.fields["law"] = forms.ChoiceField(
            label=_("Information Law"),
            required=False,
            widget=forms.Select if not hide_law_widgets else forms.HiddenInput,
            initial=default_law.pk,
            choices=((l.pk, l.name) for l in list_of_laws))

    def laws_to_json(self):
        return json.dumps(
            dict([(l.id, l.as_dict()) for l in self.list_of_laws]))

    def clean_public_body(self):
        pb = self.cleaned_data['public_body']
        if pb == "new":
            if not new_publicbody_allowed:
                raise forms.ValidationError(
                    _("You are not allowed to create a new public body"))
        elif pb == "":
            if not publicbody_empty:
                raise forms.ValidationError(
                    _("You must specify a public body"))
            pb = None
        else:
            try:
                pb_pk = int(pb)
            except ValueError:
                raise forms.ValidationError(_("Invalid value"))
            try:
                public_body = PublicBody.objects.get(pk=pb_pk)
            except PublicBody.DoesNotExist:
                raise forms.ValidationError(_("Invalid value"))
            self.public_body_object = public_body
            self.foi_law_object = public_body.default_law
        return pb

    public_body_object = None

    def clean_reference(self):
        ref = self.cleaned_data['reference']
        if not ref:
            return ''
        try:
            kind, value = ref.split(':', 1)
        except ValueError:
            return ''
        try:
            return '%s:%s' % (kind, value)
        except ValueError:
            return ''

    def clean_law_for_public_body(self, public_body):
        law = self.clean_law_without_public_body()
        if law is None:
            return None
        if law.jurisdiction.id != public_body.jurisdiction.id:
            self._errors["law"] = self.error_class(
                [_("Invalid Information Law")])
            return None
        return law

    def clean_law_without_public_body(self):
        try:
            law = self.cleaned_data['law']
            law = self.indexed_laws[int(law)]
        except (ValueError, KeyError):
            self._errors["law"] = self.error_class(
                [_("Invalid Information Law")])
            return None
        return law

    def clean(self):
        cleaned = self.cleaned_data
        public_body = cleaned.get("public_body")
        if public_body is not None and (public_body != "new"
                                        and public_body != ""):
            self.foi_law = self.clean_law_for_public_body(
                self.public_body_object)
        else:
            self.foi_law = self.clean_law_without_public_body()

        throttle_message = check_throttle(self.user, FoiRequest)
        if throttle_message:
            raise forms.ValidationError(throttle_message)

        return cleaned