Mercurial > public > sg101
comparison bio/forms.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/bio/forms.py@bbbc357ac5f3 |
children | 678a1a2ef55a |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 This file contains the forms used by the bio application. | |
3 """ | |
4 try: | |
5 from cStringIO import StringIO | |
6 except: | |
7 from StringIO import StringIO | |
8 | |
9 from django import forms | |
10 from django.conf import settings | |
11 from django.core.files.base import ContentFile | |
12 from django.contrib.auth.models import User | |
13 | |
14 from bio.models import UserProfile | |
15 from core.widgets import AutoCompleteUserInput | |
16 from core.image import parse_image, downscale_image_square | |
17 | |
18 | |
19 class EditUserForm(forms.ModelForm): | |
20 """Form for editing the fields of the User model.""" | |
21 email = forms.EmailField(label='Email', required=True) | |
22 class Meta: | |
23 model = User | |
24 fields = ('first_name', 'last_name', 'email') | |
25 | |
26 | |
27 class EditUserProfileForm(forms.ModelForm): | |
28 """Form for editing the fields of the UserProfile model.""" | |
29 location = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 })) | |
30 occupation = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 })) | |
31 interests = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 })) | |
32 time_zone = forms.CharField(required=False, widget=forms.HiddenInput()) | |
33 use_24_time = forms.BooleanField(label='Show times in 24-hour mode', required=False) | |
34 profile_text = forms.CharField(required=False, | |
35 widget=forms.Textarea(attrs={'class': 'markItUp'})) | |
36 signature = forms.CharField(required=False, | |
37 widget=forms.Textarea(attrs={'class': 'markItUp'})) | |
38 auto_favorite = forms.BooleanField( | |
39 label='Automatically favorite every forum topic I create or reply to', required=False) | |
40 auto_subscribe = forms.BooleanField( | |
41 label='Automatically subscribe to every forum topic I create or reply to', required=False) | |
42 | |
43 class Meta: | |
44 model = UserProfile | |
45 fields = ('location', 'birthday', 'occupation', 'interests', | |
46 'profile_text', 'hide_email', 'signature', 'time_zone', | |
47 'use_24_time', 'auto_favorite', 'auto_subscribe') | |
48 | |
49 class Media: | |
50 css = { | |
51 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + | |
52 settings.GPP_THIRD_PARTY_CSS['jquery-ui']) | |
53 } | |
54 js = (settings.GPP_THIRD_PARTY_JS['markitup'] + | |
55 settings.GPP_THIRD_PARTY_JS['jquery-ui'] + | |
56 ['js/bio.js', 'js/timezone.js']) | |
57 | |
58 | |
59 class UploadAvatarForm(forms.Form): | |
60 """Form used to change a user's avatar""" | |
61 avatar_file = forms.ImageField(required=False) | |
62 image = None | |
63 | |
64 def clean_avatar_file(self): | |
65 f = self.cleaned_data['avatar_file'] | |
66 if f is not None: | |
67 if f.size > settings.MAX_AVATAR_SIZE_BYTES: | |
68 raise forms.ValidationError("Please upload a file smaller than " | |
69 "%s bytes." % settings.MAX_AVATAR_SIZE_BYTES) | |
70 try: | |
71 self.image = parse_image(f) | |
72 except IOError: | |
73 raise forms.ValidationError("Please upload a valid image. " | |
74 "The file you uploaded was either not an image or a " | |
75 "corrupted image.") | |
76 self.file_type = self.image.format | |
77 return f | |
78 | |
79 def save(self): | |
80 """ | |
81 Perform any down-scaling needed on the new file, then return a tuple of | |
82 (filename, file object). Note that the file object returned may not | |
83 have a name; use the returned filename instead. | |
84 | |
85 """ | |
86 if not self.cleaned_data['avatar_file']: | |
87 return None, None | |
88 | |
89 name = self.cleaned_data['avatar_file'].name | |
90 dim = settings.MAX_AVATAR_SIZE_PIXELS | |
91 max_size = (dim, dim) | |
92 if self.image and self.image.size > max_size: | |
93 self.image = downscale_image_square(self.image, dim) | |
94 | |
95 # We need to return a Django File now. To get that from here, | |
96 # write the image data info a StringIO and then construct a | |
97 # Django ContentFile from that. The ContentFile has no name, | |
98 # that is why we return one ourselves explicitly. | |
99 s = StringIO() | |
100 self.image.save(s, self.file_type) | |
101 return name, ContentFile(s.getvalue()) | |
102 | |
103 return name, self.cleaned_data['avatar_file'] | |
104 | |
105 | |
106 class SearchUsersForm(forms.Form): | |
107 """ | |
108 A form to search for users. | |
109 """ | |
110 username = forms.CharField(max_length=30, widget=AutoCompleteUserInput()) | |
111 | |
112 class Media: | |
113 css = { | |
114 'all': settings.GPP_THIRD_PARTY_CSS['jquery-ui'] | |
115 } | |
116 js = settings.GPP_THIRD_PARTY_JS['jquery-ui'] | |
117 | |
118 def clean_username(self): | |
119 username = self.cleaned_data['username'].strip() | |
120 try: | |
121 User.objects.get(username=username, is_active=True) | |
122 except User.DoesNotExist: | |
123 raise forms.ValidationError("That username does not exist.") | |
124 return username |