view apps/bundle/forms.py @ 183:cdcbfaa65cfe

Catch integrity error when creating bundles If the user has already created a bundle with that name, then the form should raise a ValidationError instead of showing a 500.
author dellsystem <ilostwaldo@gmail.com>
date Sat, 27 Oct 2012 15:41:58 -0400
parents 86129d185ddb
children b711f0087709
line wrap: on
line source

from django import forms

from apps.bundle.models import Bundle


class BundleForm(forms.ModelForm):
    class Meta:
        model = Bundle
        fields = ('uploader', 'name', 'description', 'free_license',
            'octave_format')

    file = forms.FileField(help_text=("Upload a plain text file or an \
        archive file."))

    def clean(self):
        data = self.cleaned_data
        name_used = Bundle.objects.filter(uploader=data.get('uploader'),
            name=data.get('name')).exists()

        # If a bundle with this user/name combo exists, raise an error
        if name_used:
            raise forms.ValidationError("You have already created a bundle"
                " with this name.")

        return data


class BundleEditForm(forms.ModelForm):
    """
    Like BundleForm, but for editing bundles. A new form is needed because
    the name field should not be editable after creation, and because the
    file field shouldn't be required in this case
    """
    class Meta:
        model = Bundle
        fields = ('description', 'free_license')

    file = forms.FileField(help_text=("Upload a plain text file or an \
        archive file to update the version."), required=False)