-
Notifications
You must be signed in to change notification settings - Fork 18
Single taxonomy view [FC-0030] #78
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,7 +268,10 @@ def copy(self, taxonomy: Taxonomy) -> Taxonomy: | |
| self._taxonomy_class = taxonomy._taxonomy_class | ||
| return self | ||
|
|
||
| def get_tags(self, tag_set: models.QuerySet | None = None) -> list[Tag]: | ||
| def get_tags( | ||
| self, | ||
| tag_set: models.QuerySet[Tag] | None = None, | ||
| ) -> list[Tag]: | ||
| """ | ||
| Returns a list of all Tags in the current taxonomy, from the root(s) | ||
| down to TAXONOMY_MAX_DEPTH tags, in tree order. | ||
|
|
@@ -289,6 +292,7 @@ def get_tags(self, tag_set: models.QuerySet | None = None) -> list[Tag]: | |
| tag_set = self.tag_set.all() | ||
|
|
||
| parents = None | ||
|
|
||
| for depth in range(TAXONOMY_MAX_DEPTH): | ||
| filtered_tags = tag_set.prefetch_related("parent") | ||
| if parents is None: | ||
|
|
@@ -310,6 +314,42 @@ def get_tags(self, tag_set: models.QuerySet | None = None) -> list[Tag]: | |
| break | ||
| return tags | ||
|
|
||
| def get_filtered_tags( | ||
| self, | ||
| tag_set: models.QuerySet[Tag] | None = None, | ||
| parent_tag_id: int | None = None, | ||
| search_term: str | None = None, | ||
| search_in_all: bool = False, | ||
| ) -> models.QuerySet[Tag]: | ||
| """ | ||
| Returns a filtered QuerySet of tags. | ||
| By default returns the root tags of the given taxonomy | ||
|
|
||
| Use `parent_tag_id` to return the children of a tag. | ||
|
|
||
| Use `search_term` to filter the results by values that contains `search_term`. | ||
|
|
||
| Set `search_in_all` to True to make the search in all tags on the given taxonomy. | ||
|
|
||
| Note: This is mostly an 'internal' API and generally code outside of openedx_tagging | ||
| should use the APIs in openedx_tagging.api which in turn use this. | ||
| """ | ||
| if tag_set is None: | ||
| tag_set = self.tag_set.all() | ||
|
|
||
| if self.allow_free_text: | ||
| return tag_set.none() | ||
|
Contributor
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. I'm not really a big fan of the idea that we have to call different APIs to get the tags based on the type of taxonomy. It would be nice if the API could handle that implementation detail internally. We can think about this for later though.
Contributor
Author
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. Do you mean having two functions?
Contributor
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. No I mean for example that it would be nice if |
||
|
|
||
| if not search_in_all: | ||
| # If not search in all taxonomy, then apply parent filter. | ||
| tag_set = tag_set.filter(parent=parent_tag_id) | ||
|
|
||
| if search_term: | ||
| # Apply search filter | ||
| tag_set = tag_set.filter(value__icontains=search_term) | ||
|
|
||
| return tag_set.order_by("value", "id") | ||
|
|
||
| def validate_object_tag( | ||
| self, | ||
| object_tag: "ObjectTag", | ||
|
|
@@ -348,7 +388,9 @@ def _check_taxonomy( | |
| Subclasses can override this method to perform their own taxonomy validation checks. | ||
| """ | ||
| # Must be linked to this taxonomy | ||
| return (object_tag.taxonomy_id is not None) and object_tag.taxonomy_id == self.id | ||
| return ( | ||
| object_tag.taxonomy_id is not None | ||
| ) and object_tag.taxonomy_id == self.id | ||
|
|
||
| def _check_tag( | ||
| self, | ||
|
|
@@ -481,9 +523,11 @@ def autocomplete_tags( | |
| # Fetch tags that the object already has to exclude them from the result | ||
| excluded_tags: list[str] = [] | ||
| if object_id: | ||
| excluded_tags = list(self.objecttag_set.filter(object_id=object_id).values_list( | ||
| "_value", flat=True | ||
| )) | ||
| excluded_tags = list( | ||
| self.objecttag_set.filter(object_id=object_id).values_list( | ||
| "_value", flat=True | ||
| ) | ||
| ) | ||
| return ( | ||
| # Fetch object tags from this taxonomy whose value contains the search | ||
| self.objecttag_set.filter(_value__icontains=search) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from edx_rest_framework_extensions.paginators import DefaultPagination # type: ignore[import] | ||
|
|
||
| # From this point, the tags begin to be paginated | ||
| TAGS_THRESHOLD = 1000 | ||
|
|
||
| # From this point, search tags begin to be paginated | ||
| SEARCH_TAGS_THRESHOLD = 200 | ||
|
|
||
|
|
||
| class TagsPagination(DefaultPagination): | ||
| """ | ||
| Custom pagination configuration for taxonomies | ||
| with a large number of tags. Used on the get tags API view. | ||
| """ | ||
| page_size = 10 | ||
| max_page_size = 300 | ||
|
|
||
|
|
||
| class DisabledTagsPagination(DefaultPagination): | ||
| """ | ||
| Custom pagination configuration for taxonomies | ||
| with a small number of tags. Used on the get tags API view | ||
|
|
||
| This class allows to bring all the tags of the taxonomy. | ||
| It should be used if the number of tags within | ||
| the taxonomy does not exceed `TAGS_THRESHOLD`. | ||
| """ | ||
| page_size = TAGS_THRESHOLD | ||
| max_page_size = TAGS_THRESHOLD + 1 |
Uh oh!
There was an error while loading. Please reload this page.