-
-
Notifications
You must be signed in to change notification settings - Fork 625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have tags default to being in creation order #871
Closed
Closed
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ba771cc
add TestTaggableManager
steverecio b227495
fix _to_tag_model_instances to preserve order
steverecio 9bb0827
ensure tag order is preserved
steverecio 2461afb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a6dc1e2
missing arg
steverecio 62e5cfb
clean up tests
steverecio 7b82c28
fix conditional on ordering arg
steverecio 3469e61
make test more explicit
steverecio 8c8ee13
fix case insensitive handling and tag_kwargs
steverecio 71635d5
fix processing logic
steverecio e3afa7d
fixed duplicates issue
steverecio 53e566d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 418fa01
fix most_common test
steverecio 121ed60
a more elegant solution of imposing through table ordering only if fi…
steverecio 73dc9d8
unused import
steverecio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
from django.test import TestCase, override_settings | ||
|
||
from taggit.models import Tag | ||
from tests.models import TestModel | ||
|
||
|
||
class TestTaggableManager(TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this suite of tests should be in this file or tests.py |
||
def test_set_ordering(self): | ||
""" | ||
Test that the tags are set and returned exactly | ||
""" | ||
str_tags = ["red", "green", "delicious"] | ||
sample_obj = TestModel.objects.create() | ||
|
||
sample_obj.tags.set(str_tags) | ||
self.assertEqual(str_tags, [tag.name for tag in sample_obj.tags.all()]) | ||
|
||
def test_set_mixed(self): | ||
""" | ||
Test with mixed str and obj tags | ||
""" | ||
tag_obj = Tag.objects.create(name="mcintosh") | ||
str_tags = ["red", "green", "delicious"] | ||
sample_obj = TestModel.objects.create() | ||
|
||
sample_obj.tags.set(str_tags + [tag_obj]) | ||
results = str_tags + [tag_obj.name] | ||
self.assertEqual(results, [tag.name for tag in sample_obj.tags.all()]) | ||
|
||
def test_duplicates(self): | ||
sample_obj = TestModel.objects.create() | ||
sample_obj.tags.set(["green", "green"]) | ||
desired_result = ["green"] | ||
self.assertEqual(desired_result, [tag.name for tag in sample_obj.tags.all()]) | ||
|
||
|
||
class TestSlugification(TestCase): | ||
def test_unicode_slugs(self): | ||
""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So here this preserves the order of creation, right? But if someone has
ordering
set toname
should this actually order byname
instead?Basically shouldn't we try to keep the ordering consistent across the board? This might be pretty straightforward to do in one query.
I'm not a super fan of this method already (kinda hard to get what it's trying to accomplish) so any sort of simplification would definitely be appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This processes the
tags
arg in the order its passed in. It attempts to get or create the Tag object associated with each t in tags while ignoring duplicates (and handling case insensitivity).I would argue that name (and tag creation order) is much easier to order by on the retrieval end. By preserving the order of the original list in this function, we can order by any key we want when retrieving the model (tag name, tag pk, or through table entry pk).