~panosl/+junk/django-attachments

« back to all changes in this revision

Viewing changes to attachments/forms.py

  • Committer: Panos Laganakos
  • Date: 2011-10-06 07:31:43 UTC
  • Revision ID: panos@phaethon-designs.gr-20111006073143-wmml8s14b39q489y
ability to limit uploads per model object.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
    def clean(self):
12
12
        data = self.cleaned_data.get('attachment_file')
13
13
        try:
14
 
                        #allowed_types = tuple(settings.ALLOWED_TYPES.split(' '))
15
14
            allowed_types = tuple(settings.ALLOWED_TYPES.split(' '))
16
 
            print allowed_types
17
 
            print data.name.lower().endswith(allowed_types)
18
15
            if not data.name.lower().endswith(allowed_types):
19
16
                raise forms.ValidationError(_('Filetype not allowed.'))
20
17
        except AttributeError:
21
18
            pass
22
 
        super(AttachmentForm, self).clean()
 
19
        if settings.MAX_QUOTA and data._size > settings.MAX_QUOTA:
 
20
            raise forms.ValidationError(_('You are exceeding your allowed space.'))
23
21
        return self.cleaned_data
24
22
            
25
 
        
26
 
 
27
23
    class Meta:
28
24
        model = Attachment
29
25
        fields = ('attachment_file',)
30
26
 
31
27
    def save(self, request, obj, *args, **kwargs):
 
28
        MEGABYTE = 1048576.0
32
29
        self.instance.creator = request.user
33
30
        self.instance.content_type = ContentType.objects.get_for_model(obj)
34
31
        self.instance.object_id = obj.id