comparison gpp/bio/forms.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children b6263ac72052
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 """
2 This file contains the forms used by the bio application.
3 """
4 from PIL import ImageFile
5 from PIL import Image
6
7 try:
8 from cStringIO import StringIO
9 except:
10 from StringIO import StringIO
11
12 from django import forms
13 from django.conf import settings
14 from django.core.files.base import ContentFile
15 from django.contrib.auth.models import User
16
17 from bio.models import UserProfile
18
19
20 class EditUserForm(forms.ModelForm):
21 """Form for editing the fields of the User model."""
22 email = forms.EmailField(label='Email', required=True)
23 class Meta:
24 model = User
25 fields = ('first_name', 'last_name', 'email')
26
27
28 class EditUserProfileForm(forms.ModelForm):
29 """Form for editing the fields of the UserProfile model."""
30 location = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 }))
31 occupation = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 }))
32 interests = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 }))
33 website_1 = forms.URLField(required=False, widget=forms.TextInput(attrs={'size' : 64 }))
34 website_2 = forms.URLField(required=False, widget=forms.TextInput(attrs={'size' : 64 }))
35 website_3 = forms.URLField(required=False, widget=forms.TextInput(attrs={'size' : 64 }))
36
37 class Meta:
38 model = UserProfile
39 exclude = ('user', 'avatar', 'profile_html', 'signature_html')
40
41 class Media:
42 css = {
43 'all': ('js/markitup/skins/markitup/style.css',
44 'js/markitup/sets/markdown/style.css',
45 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/redmond/jquery-ui.css',
46 ),
47 }
48 js = (
49 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
50 'js/markitup/jquery.markitup.pack.js',
51 'js/markitup/sets/markdown/set.js',
52 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/jquery-ui.js',
53 'js/bio.js',
54 )
55
56
57 def get_image(file):
58 """
59 Returns a PIL Image from the supplied file.
60 Throws ValidationError if the file does not parse as an image file.
61 """
62 parser = ImageFile.Parser()
63 for chunk in file.chunks():
64 parser.feed(chunk)
65 try:
66 image = parser.close()
67 return image
68 except IOError:
69 pass
70 raise forms.ValidationError("Upload a valid image. " +
71 "The file you uploaded was either not an image or a corrupted image.")
72
73
74 def scale_image(image, size):
75 """Scales an image file if necessary."""
76
77 # don't upscale
78 if (size, size) >= image.size:
79 return image
80
81 (w, h) = image.size
82 if w > h:
83 diff = (w - h) / 2
84 image = image.crop((diff, 0, w - diff, h))
85 elif h > w:
86 diff = (h - w) / 2
87 image = image.crop((0, diff, w, h - diff))
88 image = image.resize((size, size), Image.ANTIALIAS)
89 return image
90
91
92 class UploadAvatarForm(forms.Form):
93 """Form used to change a user's avatar"""
94 avatar_file = forms.ImageField(required=False)
95 image = None
96
97 def clean_avatar_file(self):
98 file = self.cleaned_data['avatar_file']
99 if file is not None:
100 if file.size > settings.MAX_AVATAR_SIZE_BYTES:
101 raise forms.ValidationError("Please upload a file smaller than %s bytes." % \
102 settings.MAX_AVATAR_SIZE)
103 self.image = get_image(file)
104 self.format = self.image.format
105 return file
106
107 def get_file(self):
108 if self.image is not None:
109 self.image = scale_image(self.image, settings.MAX_AVATAR_SIZE_PIXELS)
110 s = StringIO()
111 self.image.save(s, self.format)
112 return ContentFile(s.getvalue())
113 return None
114
115 def get_filename(self):
116 return self.cleaned_data['avatar_file'].name
117
118 # vim: ts=4 sw=4