From 3524d1bd8e88a9e981c1ff042a7a49e60ec21636 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Thu, 11 Oct 2012 12:42:06 +0200 Subject: Improve the 'add tag' form --- hyperkitty/views/forms.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'hyperkitty/views/forms.py') diff --git a/hyperkitty/views/forms.py b/hyperkitty/views/forms.py index fc885c8..8e458d0 100644 --- a/hyperkitty/views/forms.py +++ b/hyperkitty/views/forms.py @@ -1,6 +1,8 @@ from django import forms from django.core import validators from django.contrib.auth.models import User +from django.utils.safestring import mark_safe + def isValidUsername(username): try: @@ -9,6 +11,7 @@ def isValidUsername(username): return raise validators.ValidationError('The username "%s" is already taken.' % username) + class RegistrationForm(forms.Form): username = forms.CharField(label='username', help_text=None, @@ -32,10 +35,30 @@ class RegistrationForm(forms.Form): return u +class TextInputWithButton(forms.TextInput): + """ + Render a text field and a button following the Twitter Bootstrap + directives: http://twitter.github.com/bootstrap/base-css.html#buttons + + Use the 'button_text' class attribute to set the button's text. + """ + + def render(self, name, value, attrs=None): + button_text = self.attrs.pop("button_text", u"") + initial_rendering = forms.TextInput.render( + self, name, value, attrs) + button = mark_safe(u'' + % button_text) + return "".join([u'', + initial_rendering, button, u'']) + + class AddTagForm(forms.Form): tag = forms.CharField(label='', help_text=None, - widget=forms.TextInput( - attrs={'placeholder': 'Add a tag...'} + widget=TextInputWithButton( + attrs={'placeholder': 'Add a tag...', + 'class': 'span2', + 'button_text': 'Add'} ) ) from_url = forms.CharField(widget=forms.HiddenInput, required=False) -- cgit