Mercurial > personas_backend
changeset 97:b69d96149997
Um... This is my first merge... But I don't know what the difference is between this and changeset 6acab0e0bca5 on hg.toolness.com.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 28 Mar 2008 12:08:12 -0700 |
parents | 89c6f5ddaded (diff) 6acab0e0bca5 (current diff) |
children | 305fb3a1e0ca |
files | |
diffstat | 4 files changed, 74 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/PersonasBackend/personas/forms.py Fri Mar 28 11:52:06 2008 -0700 +++ b/PersonasBackend/personas/forms.py Fri Mar 28 12:08:12 2008 -0700 @@ -1,24 +1,7 @@ -from django.newforms import ModelForm, ValidationError +import django.newforms as forms from PersonasBackend.personas import models -def newforms_validator( func ): - """ - Turns a framework-independent validator that throws a ValueError - into a newforms-style validator that throws a ValidationError. - - This is needed because Django's admin interface currently uses - old-style forms. - """ - - def wrapper( *args, **kwargs ): - try: - return func( *args, **kwargs ) - except ValueError, e: - raise ValidationError( e.message ) - - return wrapper - -class PersonaForm( ModelForm ): +class PersonaForm( forms.ModelForm ): """ Form given to normal users who don't have the permission to publish Personas. @@ -28,9 +11,24 @@ model = models.Persona exclude = ["owner", "date_published", "popularity", "status"] - @newforms_validator + agree_to_terms = forms.BooleanField( + label = "I agree to the terms of use.", + # TODO: setting required to True doesn't seem to have any effect, + # but the Django docs say it should ensure that the checkbox + # is filled out. + required = True, + help_text = ("Terms of use: I agree that Mozilla is providing " + "a service by hosting the " + "content I am submitting, and that they are in no " + "way responsible for any damages that occur as " + "a result of hosting said content.") + ) + def _color_cleaner( self, field ): - models.ensure_color_is_valid( self.cleaned_data[field] ) + models.ensure_color_is_valid( + self.cleaned_data[field], + error_class = forms.ValidationError + ) return self.cleaned_data[field] def clean_text_color( self ): @@ -42,8 +40,14 @@ def clean( self ): form_data = dict( self.cleaned_data ) form_data.update( self.files ) - newforms_validator( - models.ensure_header_and_footer_are_valid - )( form_data ) + models.ensure_header_and_footer_are_valid( + form_data, + error_class = forms.ValidationError + ) + if not self.cleaned_data["agree_to_terms"]: + raise forms.ValidationError( + "You must agree to the terms of service to " + "submit your Persona." + ) return self.cleaned_data
--- a/PersonasBackend/personas/models.py Fri Mar 28 11:52:06 2008 -0700 +++ b/PersonasBackend/personas/models.py Fri Mar 28 12:08:12 2008 -0700 @@ -54,12 +54,14 @@ from django.db import models from django.contrib.auth.models import User from django.contrib.sites.models import Site +from django.core.validators import ValidationError MAX_COLOR_LENGTH = 7 -def ensure_color_is_valid( color ): +def ensure_color_is_valid( color, + error_class = ValidationError ): """ - Given a color string, raises a ValueError if the color isn't + Given a color string, raises a ValidationError if the color isn't formatted properly. Examples: @@ -67,22 +69,22 @@ >>> ensure_color_is_valid('blah') Traceback (most recent call last): ... - ValueError: The color must start with a '#'. + ValidationError: [u"The color must start with a '#'."] >>> ensure_color_is_valid('#aaaaaaa') Traceback (most recent call last): ... - ValueError: The color is too long. + ValidationError: [u'The color is too long.'] >>> ensure_color_is_valid('#aaaaa') Traceback (most recent call last): ... - ValueError: The color is too short. + ValidationError: [u'The color is too short.'] >>> ensure_color_is_valid('#aaazaa') Traceback (most recent call last): ... - ValueError: The character 'z' isn't valid. + ValidationError: [u"The character 'z' isn't valid."] >>> ensure_color_is_valid('#aaaaaa') >>> ensure_color_is_valid('#FFFF11') @@ -91,19 +93,20 @@ HEX_CHARS = "0123456789abcdef" if not color.startswith("#"): - raise ValueError( "The color must start with a '#'." ) + raise error_class( "The color must start with a '#'." ) if len(color) > MAX_COLOR_LENGTH: - raise ValueError( "The color is too long." ) + raise error_class( "The color is too long." ) if len(color) < MAX_COLOR_LENGTH: - raise ValueError( "The color is too short." ) + raise error_class( "The color is too short." ) for char in color.lower()[1:MAX_COLOR_LENGTH]: if char not in HEX_CHARS: - raise ValueError( "The character '%s' isn't valid." % char ) + raise error_class( "The character '%s' isn't valid." % char ) -def ensure_header_and_footer_are_valid( form_data ): +def ensure_header_and_footer_are_valid( form_data, + error_class = ValidationError ): """ Given a dictionry that corresponds to the form data for a Persona, - raises a ValueError if there's any problems with the way the + raises a ValidationError if there's any problems with the way the header/footer information have been filled out. Examples: @@ -117,13 +120,13 @@ >>> ensure_header_and_footer_are_valid( form_data ) Traceback (most recent call last): ... - ValueError: Please enter a URL for the header or upload a file. + ValidationError: [u'Please enter a URL for the header or upload a file.'] >>> form_data['header_img_url'] = u'http://mystuff.com/img.jpg' >>> ensure_header_and_footer_are_valid( form_data ) Traceback (most recent call last): ... - ValueError: Please enter a URL for the footer or upload a file. + ValidationError: [u'Please enter a URL for the footer or upload a file.'] >>> form_data['footer_img_url'] = u'http://mystuff.com/img2.jpg' >>> ensure_header_and_footer_are_valid( form_data ) @@ -132,7 +135,7 @@ >>> ensure_header_and_footer_are_valid( form_data ) Traceback (most recent call last): ... - ValueError: You can't submit both a URL and a file for the footer. + ValidationError: [u"You can't submit both a URL and a file for the footer."] """ for name in ["header", "footer"]: @@ -141,43 +144,16 @@ form_data["%s_img_file" % name], ) if num_filled == 0: - raise ValueError( + raise error_class( "Please enter a URL for the %s or " "upload a file." % name ) elif num_filled == 2: - raise ValueError( + raise error_class( "You can't submit both a URL and a " "file for the %s." % name ) -def oldforms_validator( func ): - """ - Turns a framework-independent validator that throws a ValueError - into an oldforms-style validator that throws a ValidationError. - - This is needed because - Django's admin interface currently uses old-style forms. - """ - - def wrapper( field_data, all_data ): - from django.core import validators - - try: - func( field_data, all_data ) - except ValueError, e: - raise validators.ValidationError( e.message ) - - return wrapper - -@oldforms_validator -def header_and_footer_validator( field_data, all_data ): - ensure_header_and_footer_are_valid( all_data ) - -@oldforms_validator -def color_validator( field_data, all_data ): - ensure_color_is_valid( field_data ) - def get_num_filled( arg1, arg2 ): """ Returns how many of the given arguments evaluate to a boolean @@ -279,7 +255,7 @@ name = models.CharField( max_length=MAX_NAME_LENGTH, blank=False, - validator_list = [header_and_footer_validator], + validator_list = [ensure_header_and_footer_are_valid], ) owner = models.ForeignKey( @@ -349,7 +325,7 @@ "of the color of text that will be displayed " "on the persona."), max_length=MAX_COLOR_LENGTH, - validator_list=[color_validator], + validator_list=[ensure_color_is_valid], blank=False, default="#000000" ) @@ -359,7 +335,7 @@ "of the accent colors that will be displayed " "on the persona."), max_length=MAX_COLOR_LENGTH, - validator_list=[color_validator], + validator_list=[ensure_color_is_valid], blank=False, default="#000000" )
--- a/PersonasBackend/personas/urls.py Fri Mar 28 11:52:06 2008 -0700 +++ b/PersonasBackend/personas/urls.py Fri Mar 28 12:08:12 2008 -0700 @@ -7,6 +7,10 @@ (r'^en-US/personas_all.dat$', 'PersonasBackend.personas.json_feeds.personas'), + # Dynamic wrappers for views + url(r'^legacy/(?P<name>[_a-z]+)$', + 'PersonasBackend.personas.views.legacy_cbeard_persona'), + # Auth url(r'^login/$', 'django.contrib.auth.views.login', {'template_name':'personas/login.html'},
--- a/PersonasBackend/personas/views.py Fri Mar 28 11:52:06 2008 -0700 +++ b/PersonasBackend/personas/views.py Fri Mar 28 12:08:12 2008 -0700 @@ -1,4 +1,5 @@ from django.http import HttpResponseRedirect, HttpResponseForbidden +from django.http import HttpResponse, HttpResponseBadRequest from django.template import RequestContext from django.contrib.auth.decorators import login_required from django.core.urlresolvers import reverse @@ -7,6 +8,24 @@ from PersonasBackend.personas import models from PersonasBackend.personas import forms +def legacy_cbeard_persona( request, name ): + FOOTER_URL = "http://people.mozilla.com/~cbeard/personas/skins/%(name)s/stbar-%(name)s.jpg" + HEADER_URL = "http://people.mozilla.com/~cbeard/personas/skins/%(name)s/tbox-%(name)s.jpg" + + urldict = {"name" : name} + + action = request.GET.get( "action", "" ) + + if action == "footer": + img_src = FOOTER_URL % urldict + elif action == "header": + img_src = FOOTER_URL % urldict + else: + return HttpResponseBadRequest( "Invalid or unsupported action: %s" % + action ) + + return HttpResponse( '<img src="%s" />' % img_src ) + def list_view( request ): personas = models.Persona.objects.filter( status="published" ) return render_to_response(