Fetching contributors…
Cannot retrieve contributors at this time
73 lines (53 sloc) 2.35 KB
import logging
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from articles.models import Article, Tag
log = logging.getLogger('articles.forms')
USE_TAGGIT = 'taggit' in settings.INSTALLED_APPS
def tag(name):
"""Returns a Tag object for the given name"""
slug = Tag.clean_tag(name)
log.debug('Looking for Tag with slug "%s"...' % (slug,))
t, created = Tag.objects.get_or_create(slug=slug, defaults={'name': name})
log.debug('Found Tag %s. Name: %s Slug: %s Created: %s' % (,, t.slug, created))
if not = name
return t
class ArticleAdminForm(forms.ModelForm):
if not USE_TAGGIT:
tags = forms.CharField(initial='', required=False,
widget=forms.TextInput(attrs={'size': 100}),
help_text=_('Words that describe this article'))
def __init__(self, *args, **kwargs):
"""Sets the list of tags to be a string"""
instance = kwargs.get('instance', None)
if instance:
init = kwargs.get('initial', {})
init['tags'] = ', '.join([ for t in instance.tags.all()])
kwargs['initial'] = init
super(ArticleAdminForm, self).__init__(*args, **kwargs)
def clean_tags(self):
"""Turns the string of tags into a list"""
tags = [tag(t.strip()) for t in self.cleaned_data['tags'].split(',') if len(t.strip())]
log.debug('Tagging Article %s with: %s' % (self.cleaned_data['title'], tags))
self.cleaned_data['tags'] = tags
return self.cleaned_data['tags']
def save(self, *args, **kwargs):
"""Remove any old tags that may have been set that we no longer need"""
return super(ArticleAdminForm, self).save(*args, **kwargs)
class Meta:
model = Article
class Media:
css = {
'all': ('articles/css/jquery.autocomplete.css',),
js = (