Permalink
Browse files

Adapted to the new forms API and newforms-admin

git-svn-id: https://django-profile.googlecode.com/svn/trunk@286 7b2eb738-463f-0410-8820-43b14e3dcc91
  • Loading branch information...
1 parent 0068a44 commit 88b0437aa1559bf298f794471522b62f85772b62 david.rubert committed Jul 21, 2008
Showing with 40 additions and 26 deletions.
  1. +5 −2 demo/urls.py
  2. +23 −0 userprofile/admin.py
  3. +11 −4 userprofile/forms.py
  4. +0 −14 userprofile/models.py
  5. +1 −6 userprofile/views.py
View
@@ -1,8 +1,11 @@
from django.conf.urls.defaults import *
+from django.contrib import admin
from django.views.generic.simple import direct_to_template
from django.conf import settings
from userprofile.views import get_profiles
+admin.autodiscover()
+
urlpatterns = patterns('',
# Demo FrontPage$
@@ -12,8 +15,8 @@
(r'^accounts/', include('userprofile.urls')),
# Admin (not really needed)
- (r'^admin/', include('django.contrib.admin.urls')),
-
+ (r'^admin/doc/', include('django.contrib.admindocs.urls')),
+ (r'^admin/(.*)', admin.site.root),
)
# Serves media content. WARNING!! Only for development uses.
View
@@ -0,0 +1,23 @@
+from django.contrib import admin
+from userprofile.models import Profile, Country, Continent, Validation
+
+class ContinentAdmin(admin.ModelAdmin):
+ prepopulated_fields = {'slug': ('name', )}
+
+class CountryAdmin(admin.ModelAdmin):
+ list_display = ('name', 'continent')
+ list_filter = ('continent', )
+ prepopulated_fields = {'slug': ('name', )}
+
+class ProfileAdmin(admin.ModelAdmin):
+ list_display = ('user', 'firstname', 'surname', 'date')
+ search_fields = ('name',)
+
+class ValidationAdmin(admin.ModelAdmin):
+ list_display = ('__unicode__',)
+ search_fields = ('user__username', 'user__first_name')
+
+admin.site.register(Continent, ContinentAdmin)
+admin.site.register(Country, CountryAdmin)
+admin.site.register(Profile, ProfileAdmin)
+admin.site.register(Validation, ValidationAdmin)
View
@@ -4,8 +4,8 @@
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from userprofile.models import Country, Validation
-
-IMAGE_TYPES = { 'JPEG image data': '.jpg', 'PNG image data': '.png', 'GIF image data': '.gif' }
+from django.core.files.uploadedfile import SimpleUploadedFile
+import mimetypes, urllib
class LocationForm(forms.ModelForm):
"""
@@ -31,11 +31,18 @@ class AvatarForm(forms.Form):
photo = forms.ImageField(required=False)
url = forms.URLField(required=False)
+ def clean_url(self):
+ url = self.cleaned_data.get('url')
+ if not url: return ''
+ filename, headers = urllib.urlretrieve(url)
+ if not mimetypes.guess_all_extensions(headers.get('Content-Type')):
+ raise forms.ValidationError(_('The file type is invalid: %s' % type))
+ return SimpleUploadedFile(filename, open(filename).read(), content_type=headers.get('Content-Type'))
+
def clean(self):
if not (self.cleaned_data.get('photo') or self.cleaned_data.get('url')):
raise forms.ValidationError(_('You must enter one of the options'))
- else:
- return self.cleaned_data
+ return self.cleaned_data
class AvatarCropForm(forms.Form):
"""
View
@@ -28,9 +28,6 @@ def __unicode__(self):
def get_absolute_url(self):
return "/continent/%s/" % self.slug
- class Admin:
- pass
-
class Meta:
verbose_name = _('Continent')
verbose_name_plural = _('Continents')
@@ -57,10 +54,6 @@ def __unicode__(self):
def get_absolute_url(self):
return "/country/%s/" % self.slug
- class Admin:
- list_display = ('name', 'continent')
- list_filter = ['continent']
-
class Meta:
ordering = ['name']
verbose_name = _('Country')
@@ -99,9 +92,6 @@ class Profile(models.Model):
avatar64 = models.ImageField(upload_to="avatars/%Y/%b/%d")
avatar96 = models.ImageField(upload_to="avatars/%Y/%b/%d")
- class Admin:
- pass
-
def get_genderimage_url(self):
return GENDER_IMAGES[self.gender]
@@ -207,10 +197,6 @@ class Validation(models.Model):
class Meta:
unique_together = ('type', 'user')
- class Admin:
- list_display = ('__unicode__',)
- search_fields = ('user__username', 'user__first_name')
-
def __unicode__(self):
return _("Email validation process for %(user)s of type %(type)s") % { 'user': self.user, 'type': self.get_type_display() }
View
@@ -186,12 +186,7 @@ def avatarchoose(request, template, section, websearch=False):
else:
form = AvatarForm(request.POST, request.FILES)
if form.is_valid():
- photo = form.cleaned_data.get('photo')
- url = form.cleaned_data.get('url')
- if url:
- photo = urllib2.urlopen(url).read()
- else:
- photo = photo.read()
+ photo = form.cleaned_data.get('url') or form.cleaned_data.get('photo')
profile.save_avatartemp_file("%s_temp.jpg" % request.user.username, photo)
image = Image.open(profile.get_avatartemp_filename())
image.thumbnail((480, 480), Image.ANTIALIAS)

0 comments on commit 88b0437

Please sign in to comment.