Commit 7568a6ae by Alex Gaynor

Mark some strings as translatable, and a bunch of unrelated style changes.

parent 1baa050f
...@@ -2,6 +2,7 @@ import re ...@@ -2,6 +2,7 @@ import re
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _
from taggit.models import Tag from taggit.models import Tag
...@@ -12,7 +13,9 @@ except ImportError: ...@@ -12,7 +13,9 @@ except ImportError:
class TagKeyword(models.Model): class TagKeyword(models.Model):
""" Model to associate simple keywords to a Tag """ """
Model to associate simple keywords to a Tag
"""
tag = models.ForeignKey(Tag, related_name='keywords') tag = models.ForeignKey(Tag, related_name='keywords')
keyword = models.CharField(max_length=30) keyword = models.CharField(max_length=30)
stem = models.CharField(max_length=30) stem = models.CharField(max_length=30)
...@@ -21,7 +24,9 @@ class TagKeyword(models.Model): ...@@ -21,7 +24,9 @@ class TagKeyword(models.Model):
return "Keyword '%s' for Tag '%s'" % (self.keyword, self.tag.name) return "Keyword '%s' for Tag '%s'" % (self.keyword, self.tag.name)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" Stem the keyword on save if they have PyStemmer """ """
Stem the keyword on save if they have PyStemmer
"""
language = kwargs.pop('stemmer-language', 'english') language = kwargs.pop('stemmer-language', 'english')
if not self.pk and not self.stem and Stemmer: if not self.pk and not self.stem and Stemmer:
stemmer = Stemmer.Stemmer(language) stemmer = Stemmer.Stemmer(language)
...@@ -30,21 +35,26 @@ class TagKeyword(models.Model): ...@@ -30,21 +35,26 @@ class TagKeyword(models.Model):
def validate_regex(value): def validate_regex(value):
""" Make sure we have a valid regular expression """ """
Make sure we have a valid regular expression
"""
try: try:
re.compile(value) re.compile(value)
except Exception: except Exception:
# TODO: more restrictive in the exceptions
raise ValidationError('Please enter a valid regular expression') raise ValidationError('Please enter a valid regular expression')
class TagRegex(models.Model): class TagRegex(models.Model):
""" Model to associate regular expressions with a Tag """ """
Model to associate regular expressions with a Tag
"""
tag = models.ForeignKey(Tag, related_name='regexes') tag = models.ForeignKey(Tag, related_name='regexes')
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
regex = models.CharField( regex = models.CharField(
max_length=250, max_length=250,
validators=[validate_regex], validators=[validate_regex],
help_text=('Enter a valid Regular Expression. To make it ' help_text=_('Enter a valid Regular Expression. To make it '
'case-insensitive include "(?i)" in your expression.') 'case-insensitive include "(?i)" in your expression.')
) )
...@@ -52,6 +62,8 @@ class TagRegex(models.Model): ...@@ -52,6 +62,8 @@ class TagRegex(models.Model):
return self.name return self.name
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" Make sure to validate """ """
Make sure to validate
"""
self.full_clean() self.full_clean()
super(TagRegex,self).save(*args, **kwargs) super(TagRegex, self).save(*args, **kwargs)
...@@ -7,7 +7,9 @@ from taggit.models import Tag ...@@ -7,7 +7,9 @@ from taggit.models import Tag
def _suggest_keywords(content): def _suggest_keywords(content):
""" Suggest by keywords """ """
Suggest by keywords
"""
suggested_keywords = set() suggested_keywords = set()
keywords = TagKeyword.objects.all() keywords = TagKeyword.objects.all()
...@@ -22,7 +24,9 @@ def _suggest_keywords(content): ...@@ -22,7 +24,9 @@ def _suggest_keywords(content):
return suggested_keywords return suggested_keywords
def _suggest_regexes(content): def _suggest_regexes(content):
""" Suggest by regular expressions """ """
Suggest by regular expressions
"""
# Grab all regular expressions and compile them # Grab all regular expressions and compile them
suggested_regexes = set() suggested_regexes = set()
regex_keywords = TagRegex.objects.all() regex_keywords = TagRegex.objects.all()
...@@ -35,7 +39,9 @@ def _suggest_regexes(content): ...@@ -35,7 +39,9 @@ def _suggest_regexes(content):
return suggested_regexes return suggested_regexes
def suggest_tags(content): def suggest_tags(content):
""" Suggest tags based on text content """ """
Suggest tags based on text content
"""
suggested_keywords = _suggest_keywords(content) suggested_keywords = _suggest_keywords(content)
suggested_regexes = _suggest_regexes(content) suggested_regexes = _suggest_regexes(content)
suggested_tag_ids = suggested_keywords | suggested_regexes suggested_tag_ids = suggested_keywords | suggested_regexes
......
from django import forms from django import forms
from django.utils.translation import ugettext as _
from taggit.utils import parse_tags, edit_string_for_tags from taggit.utils import parse_tags, edit_string_for_tags
...@@ -16,4 +17,4 @@ class TagField(forms.CharField): ...@@ -16,4 +17,4 @@ class TagField(forms.CharField):
try: try:
return parse_tags(value) return parse_tags(value)
except ValueError: except ValueError:
raise forms.ValidationError("Please provide a comma-separated list of tags.") raise forms.ValidationError(_("Please provide a comma-separated list of tags."))
...@@ -5,6 +5,7 @@ from django.db import models ...@@ -5,6 +5,7 @@ from django.db import models
from django.db.models.fields.related import ManyToManyRel from django.db.models.fields.related import ManyToManyRel
from django.db.models.related import RelatedObject from django.db.models.related import RelatedObject
from django.db.models.query_utils import QueryWrapper from django.db.models.query_utils import QueryWrapper
from django.utils.translation import ugettext_lazy as _
from taggit.forms import TagField from taggit.forms import TagField
from taggit.models import Tag, TaggedItem from taggit.models import Tag, TaggedItem
...@@ -37,7 +38,7 @@ class TaggableRel(ManyToManyRel): ...@@ -37,7 +38,7 @@ class TaggableRel(ManyToManyRel):
class TaggableManager(object): class TaggableManager(object):
def __init__(self, verbose_name="Tags", through=None): def __init__(self, verbose_name=_("Tags"), through=None):
self.use_gfk = through is None self.use_gfk = through is None
self.through = through or TaggedItem self.through = through or TaggedItem
self.rel = TaggableRel(to=self.through) self.rel = TaggableRel(to=self.through)
...@@ -97,8 +98,8 @@ class TaggableManager(object): ...@@ -97,8 +98,8 @@ class TaggableManager(object):
def formfield(self, form_class=TagField, **kwargs): def formfield(self, form_class=TagField, **kwargs):
defaults = { defaults = {
"label": "Tags", "label": _("Tags"),
"help_text": "A comma-separated list of tags." "help_text": _("A comma-separated list of tags.")
} }
defaults.update(kwargs) defaults.update(kwargs)
return form_class(**kwargs) return form_class(**kwargs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment