Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-taggit
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
57e30c5f
authored
Apr 24, 2010
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some minor cleanup, renaming.
parent
f364ac09
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
152 additions
and
136 deletions
+152
-136
AUTHORS.txt
+1
-0
README.txt
+8
-6
taggit/contrib/suggest/README.txt
+18
-14
taggit/contrib/suggest/admin.py
+13
-10
taggit/contrib/suggest/models.py
+38
-33
taggit/contrib/suggest/tests/__init__.py
+0
-1
taggit/contrib/suggest/tests/settings.py
+3
-2
taggit/contrib/suggest/tests/tests.py
+34
-28
taggit/contrib/suggest/utils.py
+37
-42
No files found.
AUTHORS.txt
View file @
57e30c5f
...
@@ -7,3 +7,4 @@ fakeempire <adam@fakeempire.com>
...
@@ -7,3 +7,4 @@ fakeempire <adam@fakeempire.com>
Ben Firshman <ben@firshman.co.uk>
Ben Firshman <ben@firshman.co.uk>
Alex Gaynor <alex.gaynor@gmail.com>
Alex Gaynor <alex.gaynor@gmail.com>
Rob Hudson <rob@cogit8.org>
Rob Hudson <rob@cogit8.org>
Frank Wiles
README.txt
View file @
57e30c5f
...
@@ -3,19 +3,19 @@ django-taggit
...
@@ -3,19 +3,19 @@ django-taggit
``django-taggit`` a simpler approach to tagging with Django. Add it to your
``django-taggit`` a simpler approach to tagging with Django. Add it to your
``INSTALLED_APPS` then just add a TaggableManager to your model and go:
``INSTALLED_APPS` then just add a TaggableManager to your model and go:
from django.db import models
from django.db import models
from taggit.managers import TaggableManager
from taggit.managers import TaggableManager
class Food(models.Model):
class Food(models.Model):
# ... fields here
# ... fields here
tags = TaggableManager()
tags = TaggableManager()
Then you can use the API like so:
Then you can use the API like so:
>>> apple = Food.objects.create(name="apple")
>>> apple = Food.objects.create(name="apple")
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.all()
>>> apple.tags.all()
...
@@ -27,3 +27,5 @@ Then you can use the API like so:
...
@@ -27,3 +27,5 @@ Then you can use the API like so:
Tags will show up for you automatically in forms and the admin.
Tags will show up for you automatically in forms and the admin.
``django-taggit`` requires Django 1.1 or greater.
taggit/contrib/suggest/README.txt
View file @
57e30c5f
taggit.contrib.suggest
taggit.contrib.suggest
======================
======================
This add on module allows you to easily associate keywords and regular
This add on module allows you to easily associate keywords and regular
expressions with a Tag object. This is useful to help keep your database
expressions with a Tag object. This is useful to help keep your database
getting filled up with several similar tags that really represent the same
getting filled up with several similar tags that really represent the same
thing.
thing.
For example, if your site is a humor site you might want to collapse all of
For example, if your site is a humor site you might want to collapse all of
#fun, #funny, #funnies, #hilarious, #rofl, and #lol into one tag #funny. The
#fun, #funny, #funnies, #hilarious, #rofl, and #lol into one tag #funny. The
suggest_tags() function in taggit.contrib.suggest.utils will give you a list
``suggest_tags()`` function in ``taggit.contrib.suggest.utils`` will give you a
of tags that seem appropriate for the text content given to it.
list of tags that seem appropriate for the text content given to it.
Unlike the rest of ``django-taggit``, ``suggest`` requires Django 1.2.
Usage
Usage
=====
=====
Put 'taggit.contrib.suggest' into INSTALLED_APPS and run a syncdb to create
Put ``'taggit.contrib.suggest'`` into ``INSTALLED_APPS`` and run a syncdb to
the necessary models. This will add Keywords and Regular Expression inlines
create the necessary models. This will add ``Keywords`` and
to the default django-taggit admin. Once you've populated those based on your
``Regular Expression`` inlines to the default ``django-taggit`` admin. Once
site you can do a simple:
you've populated those based on your site you can do a simple:
.. sourcecode:: python
from taggit.contrib.suggest.utils import suggest_tags
from taggit.contrib.suggest.utils import suggest_tags
tags = suggest_tags(content='Some textual content...')
tags = suggest_tags(content='Some textual content...')
TODO
TODO
====
====
* In a later version I hope to a simple way to help determine keywords for you
* In a later version I hope to a simple way to help determine keywords for you
automatically, by learning from your past tags and content.
automatically, by learning from your past tags and content.
taggit/contrib/suggest/admin.py
View file @
57e30c5f
from
django.contrib
import
admin
from
django.contrib
import
admin
from
taggit.
models
import
Tag
from
taggit.
admin
import
TaggedItemInline
from
taggit.
admin
import
TaggedItemInline
from
taggit.
contrib.suggest.models
import
TagKeyword
,
TagRegex
from
taggit.
contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
taggit.
models
import
Tag
class
TagKeywordInline
(
admin
.
StackedInline
):
class
TagKeywordInline
(
admin
.
StackedInline
):
model
=
TagKeyword
model
=
TagKeyword
class
TagRegExpInline
(
admin
.
StackedInline
):
model
=
TagRegExp
class
TagSuggestAdmin
(
admin
.
ModelAdmin
):
class
TagRegxInline
(
admin
.
StackedInline
):
model
=
TagRegex
class
TagSuggestAdmin
(
admin
.
ModelAdmin
):
inlines
=
[
inlines
=
[
TaggedItemInline
,
TaggedItemInline
,
TagKeywordInline
,
TagKeywordInline
,
TagReg
Exp
Inline
,
TagReg
x
Inline
,
]
]
admin
.
site
.
unregister
(
Tag
)
admin
.
site
.
unregister
(
Tag
)
admin
.
site
.
register
(
Tag
,
TagSuggestAdmin
)
admin
.
site
.
register
(
Tag
,
TagSuggestAdmin
)
taggit/contrib/suggest/models.py
View file @
57e30c5f
import
re
import
re
from
django.db
import
models
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
from
django.db
import
models
from
taggit.models
import
Tag
HAS_PYSTEMMER
=
True
from
taggit.models
import
Tag
try
:
try
:
import
Stemmer
import
Stemmer
except
ImportError
:
except
ImportError
:
HAS_PYSTEMMER
=
False
Stemmer
=
None
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
)
def
__unicode__
(
self
):
def
__unicode__
(
self
):
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
.
id
and
not
self
.
stem
and
HAS_PYSTEMMER
:
if
not
self
.
pk
and
not
self
.
stem
and
Stemmer
:
stemmer
=
Stemmer
.
Stemmer
(
language
)
stemmer
=
Stemmer
.
Stemmer
(
language
)
self
.
stem
=
stemmer
.
stemWord
(
self
.
keyword
)
self
.
stem
=
stemmer
.
stemWord
(
self
.
keyword
)
super
(
TagKeyword
,
self
)
.
save
(
*
args
,
**
kwargs
)
super
(
TagKeyword
,
self
)
.
save
(
*
args
,
**
kwargs
)
def
validate_regexp
(
value
):
""" Make sure we have a valid regular expression """
def
validate_regex
(
value
):
try
:
""" Make sure we have a valid regular expression """
try
:
re
.
compile
(
value
)
re
.
compile
(
value
)
except
:
except
Exception
:
raise
ValidationError
(
'Please enter a valid regular expression'
)
raise
ValidationError
(
'Please enter a valid regular expression'
)
class
TagRegExp
(
models
.
Model
):
""" Model to associate regular expressions with a Tag """
class
TagRegex
(
models
.
Model
):
tag
=
models
.
ForeignKey
(
Tag
,
related_name
=
'regexps'
)
""" Model to associate regular expressions with a Tag """
tag
=
models
.
ForeignKey
(
Tag
,
related_name
=
'regexes'
)
name
=
models
.
CharField
(
max_length
=
30
)
name
=
models
.
CharField
(
max_length
=
30
)
regexp
=
models
.
CharField
(
max_length
=
250
,
regex
=
models
.
CharField
(
validators
=
[
validate_regexp
],
max_length
=
250
,
help_text
=
'Enter a valid Regular Expression. To make it case-insensitive include "(?i)" in your expression.'
validators
=
[
validate_regex
],
)
help_text
=
(
'Enter a valid Regular Expression. To make it '
'case-insensitive include "(?i)" in your expression.'
)
def
__unicode__
(
self
):
)
return
self
.
name
def
__unicode__
(
self
):
def
save
(
self
,
*
args
,
**
kwargs
):
return
self
.
name
""" Make sure to validate """
def
save
(
self
,
*
args
,
**
kwargs
):
""" Make sure to validate """
self
.
full_clean
()
self
.
full_clean
()
super
(
TagReg
Exp
,
self
)
.
save
(
*
args
,
**
kwargs
)
super
(
TagReg
ex
,
self
)
.
save
(
*
args
,
**
kwargs
)
taggit/contrib/suggest/tests/__init__.py
View file @
57e30c5f
from
taggit.contrib.suggest.tests.tests
import
SuggestCase
from
taggit.contrib.suggest.tests.tests
import
SuggestCase
taggit/contrib/suggest/tests/settings.py
View file @
57e30c5f
DATABASE_ENGINE
=
'sqlite3'
DATABASE_ENGINE
=
'sqlite3'
INSTALLED_APPS
=
[
INSTALLED_APPS
=
[
'django.contrib.contenttypes'
,
'django.contrib.contenttypes'
,
'taggit'
,
'taggit'
,
'taggit.tests'
,
'taggit.contrib.suggest'
,
'taggit.contrib.suggest'
,
]
]
taggit/contrib/suggest/tests/tests.py
View file @
57e30c5f
from
django.test
import
TestCase
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
from
django.test
import
TestCase
from
taggit.models
import
Tag
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegex
from
taggit.contrib.suggest.utils
import
suggest_tags
from
taggit.contrib.suggest.utils
import
suggest_tags
from
taggit.models
import
Tag
class
SuggestCase
(
TestCase
):
def
test_simple_suggest
(
self
):
class
SuggestCase
(
TestCase
):
def
test_simple_suggest
(
self
):
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
ku_keyword1
=
TagKeyword
.
objects
.
create
(
ku_keyword1
=
TagKeyword
.
objects
.
create
(
tag
=
ku_tag
,
tag
=
ku_tag
,
keyword
=
'kansas university'
)
keyword
=
'kansas university'
)
suggested_tags
=
suggest_tags
(
content
=
'I used to be a student at kansas university'
)
suggested_tags
=
suggest_tags
(
'I used to be a student at kansas university'
)
self
.
assertTrue
(
ku_tag
in
suggested_tags
)
self
.
assertTrue
(
ku_tag
in
suggested_tags
)
def
test_regex
p_suggest
(
self
):
def
test_regex
_suggest
(
self
):
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
new_regexp
=
TagRegExp
.
objects
.
create
(
TagRegex
.
objects
.
create
(
tag
=
ku_tag
,
tag
=
ku_tag
,
name
=
'Find University of Kansas'
,
name
=
'Find University of Kansas'
,
regexp
=
'University
\
s+of
\
s+Kansas'
)
regex
=
'University
\
s+of
\
s+Kansas'
)
suggested_tags
=
suggest_tags
(
content
=
'I was once a student at the University of Kansas'
)
suggested_tags
=
suggest_tags
(
'I was once a student at the University of Kansas'
)
self
.
assertTrue
(
ku_tag
in
suggested_tags
)
self
.
assertTrue
(
ku_tag
in
suggested_tags
)
def
test_bad_regex
p
(
self
):
def
test_bad_regex
(
self
):
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
ku_keyword1
=
TagKeyword
.
objects
.
create
(
ku_keyword1
=
TagKeyword
.
objects
.
create
(
tag
=
ku_tag
,
tag
=
ku_tag
,
keyword
=
'kansas university'
)
keyword
=
'kansas university'
new_regexp
=
TagRegExp
(
)
tag
=
ku_tag
,
new_regex
=
TagRegex
(
name
=
'Find University of Kansas'
,
tag
=
ku_tag
,
regexp
=
'University
\
s+of(
\
s+Kansas'
)
name
=
'Find University of Kansas'
,
self
.
assertRaises
(
ValidationError
,
new_regexp
.
save
)
regex
=
'University
\
s+of(
\
s+Kansas'
)
suggested_tags
=
suggest_tags
(
content
=
'I was once a student at the University of Kansas. Also known as kansas university by the way.'
)
self
.
assertRaises
(
ValidationError
,
new_regex
.
save
)
suggested_tags
=
suggest_tags
(
'I was once a student at the University '
'of Kansas. Also known as kansas university by the way.'
)
self
.
assertTrue
(
ku_tag
in
suggested_tags
)
self
.
assertTrue
(
ku_tag
in
suggested_tags
)
taggit/contrib/suggest/utils.py
View file @
57e30c5f
import
re
import
re
from
taggit.models
import
Tag
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
django.conf
import
settings
from
django.conf
import
settings
def
_suggest_keywords
(
content
=
None
):
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegex
""" Suggest by keywords """
from
taggit.models
import
Tag
def
_suggest_keywords
(
content
):
""" Suggest by keywords """
suggested_keywords
=
set
()
suggested_keywords
=
set
()
keywords
=
TagKeyword
.
objects
.
values_list
(
'keyword'
,
'stem'
,
'tag'
)
keywords
=
TagKeyword
.
objects
.
all
()
for
k
in
keywords
:
for
k
in
keywords
:
# Use the stem if available, otherwise use the whole keyword
# Use the stem if available, otherwise use the whole keyword
if
k
[
1
]:
if
k
.
stem
:
if
k
[
1
]
in
content
:
if
k
.
stem
in
content
:
suggested_keywords
.
add
(
k
[
2
])
suggested_keywords
.
add
(
k
.
tag_id
)
elif
k
[
0
]
in
content
:
elif
k
.
keyword
in
content
:
suggested_keywords
.
add
(
k
[
2
])
suggested_keywords
.
add
(
k
.
tag_id
)
return
suggested_keywords
return
suggested_keywords
def
_suggest_regexps
(
content
=
None
):
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_regexps
=
set
()
suggested_regexes
=
set
()
regexps
=
set
()
regex_keywords
=
TagRegex
.
objects
.
all
()
regexp_keywords
=
TagRegExp
.
objects
.
values_list
(
'regexp'
,
# Look for our regular expressions in the content
'tag'
,
for
r
in
regex_keywords
:
)
if
re
.
search
(
r
.
regex
,
content
):
suggested_regexes
.
add
(
r
.
tag_id
)
for
r
in
regexp_keywords
:
regexps
.
add
((
re
.
compile
(
r
[
0
]),
r
[
1
]))
return
suggested_regexes
# Look for our regular expressions in the content
def
suggest_tags
(
content
):
for
r
in
regexps
:
""" Suggest tags based on text content """
if
r
[
0
]
.
search
(
content
):
suggested_regexps
.
add
(
r
[
1
])
return
suggested_regexps
def
suggest_tags
(
content
=
None
):
""" Suggest tags based on text content """
suggested_keywords
=
_suggest_keywords
(
content
)
suggested_keywords
=
_suggest_keywords
(
content
)
suggested_regex
ps
=
_suggest_regexps
(
content
)
suggested_regex
es
=
_suggest_regexes
(
content
)
suggested_tag_ids
=
suggested_keywords
|
suggested_regex
p
s
suggested_tag_ids
=
suggested_keywords
|
suggested_regex
e
s
return
Tag
.
objects
.
filter
(
id__in
=
suggested_tag_ids
)
return
Tag
.
objects
.
filter
(
id__in
=
suggested_tag_ids
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment