ImageField for Google App Engine

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from django import forms
from django.utils.translation import ugettext_lazy as _
from google.appengine.api import images

# Django's built-in ImageField doesn't work on AppEngine because
# it relies on unavailable PIL APIs. Here's my own version that works.

def image_bytes_are_valid(image_bytes):
    try:
        test_image = images.Image(image_bytes)
        # Unfortunately the only way to validate image bytes on AppEngine is to
        # perform a transform. Lame.
        ignored_output = test_image.execute_transforms(images.PNG)
    except images.Error:
        return False
    return True

class AppEngineImageField(forms.FileField):
    default_error_messages = {
        'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or was a corrupted image."),
    }
    
    def clean(self, data, initial=None):
        raw_file = super(AppEngineImageField, self).clean(data, initial)
        if raw_file is None:
            return None
        elif not data and initial:
            return initial
            
        if hasattr(data, 'read'):
            bytes = data.read()
        else:
            try:
                bytes = data['content']
            except:
                bytes = None
        
        if bytes is None:
            raise forms.ValidationError(self.error_messages['invalid_image'])
        
        if (len(bytes) > 0) and (not image_bytes_are_valid(bytes)):
            raise forms.ValidationError(self.error_messages['invalid_image'])
        
        if hasattr(raw_file, 'seek') and callable(raw_file.seek):
            raw_file.seek(0)
            
        return raw_file
        
                
                

More like this

  1. Google AppEngine Login Decorator by cmcavoy 6 years ago
  2. Accepting and processing PayPal IPN messages (including using App Engine) by simon 5 years, 8 months ago
  3. HTML/JS template filter to show localized dates/times by mback2k 5 years ago
  4. A form field for valdating PDF and Microsoft Word document by jimmylam 5 years, 5 months ago
  5. rss news by montao 4 years, 4 months ago

Comments

johnecon (on July 8, 2012):

hello dave! I would like to use this snippet which seems exactly what i need. So under Event/models.py i have something like this

class Event(models.Model): title = models.CharField(max_length=200) address = models.CharField(max_length=200) image = AppEngineImageField()

and under Event/admin.py I have the following class

class EventAdmin(admin.ModelAdmin): list_display = ('address', 'image', 'category', 'description', 'title', 'activationDate', 'expiryDate', 'isExpired') search_fields = ['title']

admin.site.register(Event, EventAdmin)

But when i go to admin area to add a new event unfortunately i am not able to upload an image to my event.. Any suggestions??

#

(Forgotten your password?)