-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: System defined taxonomies * style: models.py moved to models/base.py * feat: New Content System defined models * style: Lint and migration * fix: Fix migration error * chore: Rebase and compile requirements * refactor: adds ContentTaxonomyMixin for use when creating content system taxonomies Pulls the ContentTaxonomy-specific logic into a mixin class to bring the Content-specific logic into other Taxonony subclasses. * fix: Tests * test: System defined model validations * fix: Move language taxonomy creation to openedx-learning * style: Rename of OrganizationSystemDefinedTaxonomy * style: nits * chore: Update openedx-learning dependency --------- Co-authored-by: Jillian Vogel <[email protected]>
- Loading branch information
1 parent
8beb627
commit 9bea447
Showing
13 changed files
with
304 additions
and
12 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
openedx/features/content_tagging/fixtures/system_defined.yaml
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- model: oel_tagging.taxonomy | ||
pk: -2 | ||
fields: | ||
name: Organizations | ||
description: Allows tags for any organization ID created on the instance. | ||
enabled: true | ||
required: true | ||
allow_multiple: false | ||
allow_free_text: false | ||
visible_to_authors: false | ||
_taxonomy_class: openedx.features.content_tagging.models.ContentAuthorTaxonomy | ||
- model: oel_tagging.taxonomy | ||
pk: -3 | ||
fields: | ||
name: Content Authors | ||
description: Allows tags for any user ID created on the instance. | ||
enabled: true | ||
required: true | ||
allow_multiple: false | ||
allow_free_text: false | ||
visible_to_authors: false | ||
_taxonomy_class: openedx.features.content_tagging.models.ContentOrganizationTaxonomy |
59 changes: 59 additions & 0 deletions
59
openedx/features/content_tagging/migrations/0002_system_defined_taxonomies.py
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Generated by Django 3.2.20 on 2023-07-31 21:07 | ||
|
||
from django.db import migrations | ||
import openedx.features.content_tagging.models.base | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('oel_tagging', '0005_language_taxonomy'), | ||
('content_tagging', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ContentAuthorTaxonomy', | ||
fields=[ | ||
], | ||
options={ | ||
'proxy': True, | ||
'indexes': [], | ||
'constraints': [], | ||
}, | ||
bases=(openedx.features.content_tagging.models.base.ContentTaxonomyMixin, 'oel_tagging.usersystemdefinedtaxonomy'), | ||
), | ||
migrations.CreateModel( | ||
name='ContentLanguageTaxonomy', | ||
fields=[ | ||
], | ||
options={ | ||
'proxy': True, | ||
'indexes': [], | ||
'constraints': [], | ||
}, | ||
bases=(openedx.features.content_tagging.models.base.ContentTaxonomyMixin, 'oel_tagging.languagetaxonomy'), | ||
), | ||
migrations.CreateModel( | ||
name='ContentOrganizationTaxonomy', | ||
fields=[ | ||
], | ||
options={ | ||
'proxy': True, | ||
'indexes': [], | ||
'constraints': [], | ||
}, | ||
bases=(openedx.features.content_tagging.models.base.ContentTaxonomyMixin, 'oel_tagging.modelsystemdefinedtaxonomy'), | ||
), | ||
migrations.CreateModel( | ||
name='OrganizationModelObjectTag', | ||
fields=[ | ||
], | ||
options={ | ||
'proxy': True, | ||
'indexes': [], | ||
'constraints': [], | ||
}, | ||
bases=('oel_tagging.modelobjecttag',), | ||
), | ||
] |
39 changes: 39 additions & 0 deletions
39
openedx/features/content_tagging/migrations/0003_system_defined_fixture.py
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Generated by Django 3.2.20 on 2023-07-11 22:57 | ||
|
||
from django.db import migrations | ||
from django.core.management import call_command | ||
from openedx.features.content_tagging.models import ContentLanguageTaxonomy | ||
|
||
|
||
def load_system_defined_taxonomies(apps, schema_editor): | ||
""" | ||
Creates system defined taxonomies | ||
""" | ||
|
||
# Create system defined taxonomy instances | ||
call_command('loaddata', '--app=content_tagging', 'system_defined.yaml') | ||
|
||
# Adding taxonomy class to the language taxonomy | ||
Taxonomy = apps.get_model('oel_tagging', 'Taxonomy') | ||
language_taxonomy = Taxonomy.objects.get(id=-1) | ||
language_taxonomy.taxonomy_class = ContentLanguageTaxonomy | ||
|
||
|
||
def revert_system_defined_taxonomies(apps, schema_editor): | ||
""" | ||
Deletes all system defined taxonomies | ||
""" | ||
Taxonomy = apps.get_model('oel_tagging', 'Taxonomy') | ||
Taxonomy.objects.get(id=-2).delete() | ||
Taxonomy.objects.get(id=-3).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('content_tagging', '0002_system_defined_taxonomies'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(load_system_defined_taxonomies, revert_system_defined_taxonomies), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
Content Tagging and System defined models | ||
""" | ||
from .base import ( | ||
TaxonomyOrg, | ||
ContentObjectTag, | ||
ContentTaxonomy, | ||
) | ||
from .system_defined import ( | ||
ContentLanguageTaxonomy, | ||
ContentAuthorTaxonomy, | ||
ContentOrganizationTaxonomy, | ||
) |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
System defined models | ||
""" | ||
from typing import Type | ||
|
||
from openedx_tagging.core.tagging.models import ( | ||
ModelSystemDefinedTaxonomy, | ||
ModelObjectTag, | ||
UserSystemDefinedTaxonomy, | ||
LanguageTaxonomy, | ||
) | ||
|
||
from organizations.models import Organization | ||
from .base import ContentTaxonomyMixin | ||
|
||
|
||
class OrganizationModelObjectTag(ModelObjectTag): | ||
""" | ||
ObjectTags for the OrganizationSystemDefinedTaxonomy. | ||
""" | ||
|
||
class Meta: | ||
proxy = True | ||
|
||
@property | ||
def tag_class_model(self) -> Type: | ||
""" | ||
Associate the organization model | ||
""" | ||
return Organization | ||
|
||
@property | ||
def tag_class_value(self) -> str: | ||
""" | ||
Returns the organization name to use it on Tag.value when creating Tags for this taxonomy. | ||
""" | ||
return "name" | ||
|
||
|
||
class ContentOrganizationTaxonomy(ContentTaxonomyMixin, ModelSystemDefinedTaxonomy): | ||
""" | ||
Organization system-defined taxonomy that accepts ContentTags | ||
Side note: The organization of an object is already encoded in its usage ID, | ||
but a Taxonomy with Organization as Tags is being used so that the objects can be | ||
indexed and can be filtered in the same tagging system, without any special casing. | ||
""" | ||
|
||
class Meta: | ||
proxy = True | ||
|
||
@property | ||
def object_tag_class(self) -> Type: | ||
""" | ||
Returns OrganizationModelObjectTag as ObjectTag subclass associated with this taxonomy. | ||
""" | ||
return OrganizationModelObjectTag | ||
|
||
|
||
class ContentLanguageTaxonomy(ContentTaxonomyMixin, LanguageTaxonomy): | ||
""" | ||
Language system-defined taxonomy that accepts ContentTags | ||
""" | ||
|
||
class Meta: | ||
proxy = True | ||
|
||
|
||
class ContentAuthorTaxonomy(ContentTaxonomyMixin, UserSystemDefinedTaxonomy): | ||
""" | ||
Author system-defined taxonomy that accepts ContentTags | ||
""" | ||
|
||
class Meta: | ||
proxy = True |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
Test for Content models | ||
""" | ||
import ddt | ||
from django.test.testcases import TestCase | ||
|
||
from openedx_tagging.core.tagging.models import ( | ||
ObjectTag, | ||
Tag, | ||
) | ||
from openedx_tagging.core.tagging.api import create_taxonomy | ||
from ..models import ( | ||
ContentLanguageTaxonomy, | ||
ContentAuthorTaxonomy, | ||
ContentOrganizationTaxonomy, | ||
) | ||
|
||
|
||
@ddt.ddt | ||
class TestSystemDefinedModels(TestCase): | ||
""" | ||
Test for System defined models | ||
""" | ||
|
||
@ddt.data( | ||
(ContentLanguageTaxonomy, "taxonomy"), # Invalid object key | ||
(ContentLanguageTaxonomy, "tag"), # Invalid external_id, invalid language | ||
(ContentLanguageTaxonomy, "object"), # Invalid object key | ||
(ContentAuthorTaxonomy, "taxonomy"), # Invalid object key | ||
(ContentAuthorTaxonomy, "tag"), # Invalid external_id, User don't exits | ||
(ContentAuthorTaxonomy, "object"), # Invalid object key | ||
(ContentOrganizationTaxonomy, "taxonomy"), # Invalid object key | ||
(ContentOrganizationTaxonomy, "tag"), # Invalid external_id, Organization don't exits | ||
(ContentOrganizationTaxonomy, "object"), # Invalid object key | ||
) | ||
@ddt.unpack | ||
def test_validations( | ||
self, | ||
taxonomy_cls, | ||
check, | ||
): | ||
""" | ||
Test that the respective validations are being called | ||
""" | ||
taxonomy = create_taxonomy( | ||
name='Test taxonomy', | ||
taxonomy_class=taxonomy_cls, | ||
) | ||
|
||
tag = Tag( | ||
value="value", | ||
external_id="external_id", | ||
taxonomy=taxonomy, | ||
) | ||
tag.save() | ||
|
||
object_tag = ObjectTag( | ||
object_id='object_id', | ||
taxonomy=taxonomy, | ||
tag=tag, | ||
) | ||
|
||
check_taxonomy = check == 'taxonomy' | ||
check_object = check == 'object' | ||
check_tag = check == 'tag' | ||
assert not taxonomy.validate_object_tag( | ||
object_tag=object_tag, | ||
check_taxonomy=check_taxonomy, | ||
check_object=check_object, | ||
check_tag=check_tag, | ||
) |
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
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
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
Oops, something went wrong.