Skip to content

Commit

Permalink
refactor: adds ContentTaxonomyMixin for use when creating content sys…
Browse files Browse the repository at this point in the history
…tem taxonomies

Pulls the ContentTaxonomy-specific logic into a mixin class to bring
the Content-specific logic into other Taxonony subclasses.
  • Loading branch information
pomegranited committed Jul 24, 2023
1 parent ecd8e07 commit 9d195a0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Generated by Django 3.2.20 on 2023-07-22 18:30
# Generated by Django 3.2.20 on 2023-07-24 01:58

from django.db import migrations
import openedx.features.content_tagging.models.base


class Migration(migrations.Migration):

dependencies = [
('oel_tagging', '0003_auto_20230721_1238'),
('oel_tagging', '__latest__'),
('content_tagging', '0002_system_defined_taxonomies'),
]

Expand All @@ -20,7 +21,7 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('oel_tagging.usersystemdefinedtaxonomy',),
bases=(openedx.features.content_tagging.models.base.ContentTaxonomyMixin, 'oel_tagging.usersystemdefinedtaxonomy'),
),
migrations.CreateModel(
name='ContentLanguageTaxonomy',
Expand All @@ -31,29 +32,29 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('oel_tagging.languagetaxonomy',),
bases=(openedx.features.content_tagging.models.base.ContentTaxonomyMixin, 'oel_tagging.languagetaxonomy'),
),
migrations.CreateModel(
name='OrganizarionSystemDefinedTaxonomy',
name='OrganizationModelObjectTag',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('oel_tagging.modelsystemdefinedtaxonomy',),
bases=('oel_tagging.modelobjecttag',),
),
migrations.CreateModel(
name='OrganizationModelObjectTag',
name='OrganizationSystemDefinedTaxonomy',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('oel_tagging.modelobjecttag',),
bases=('oel_tagging.modelsystemdefinedtaxonomy',),
),
migrations.CreateModel(
name='ContentOrganizationTaxonomy',
Expand All @@ -64,6 +65,6 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('content_tagging.organizarionsystemdefinedtaxonomy',),
bases=(openedx.features.content_tagging.models.base.ContentTaxonomyMixin, 'content_tagging.organizationsystemdefinedtaxonomy'),
),
]
22 changes: 19 additions & 3 deletions openedx/features/content_tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,20 @@ def object_key(self) -> OpaqueKey:
return None


class ContentTaxonomy(Taxonomy):
class ContentTaxonomyMixin:
"""
Taxonomy that accepts ContentTags,
and ensures a valid TaxonomyOrg owner relationship with the content object.
"""

class Meta:
proxy = True
@property
def object_tag_class(self) -> Type:
"""
Returns the ObjectTag subclass associated with this taxonomy, which is ObjectTag by default.
Taxonomy subclasses may override this method to use different subclasses of ObjectTag.
"""
return ContentTag

def _check_object(self, object_tag: ObjectTag) -> bool:
"""
Expand All @@ -124,3 +130,13 @@ def _check_taxonomy(self, object_tag: ObjectTag) -> bool:
and object_key
and TaxonomyOrg.is_owner(self, object_key.org)
)


class ContentTaxonomy(ContentTaxonomyMixin, Taxonomy):
"""
Taxonomy that accepts ContentTags,
and ensures a valid TaxonomyOrg owner relationship with the content object.
"""

class Meta:
proxy = True
43 changes: 6 additions & 37 deletions openedx/features/content_tagging/models/system_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
from typing import Type

from openedx_tagging.core.tagging.models import (
ObjectTag,
ModelSystemDefinedTaxonomy,
ModelObjectTag,
UserSystemDefinedTaxonomy,
LanguageTaxonomy,
)

from organizations.models import Organization
from .base import ContentTaxonomy
from .base import ContentTaxonomyMixin


class OrganizationModelObjectTag(ModelObjectTag):
"""
ObjectTags for the OrganizarionSystemDefinedTaxonomy.
ObjectTags for the OrganizationSystemDefinedTaxonomy.
"""

class Meta:
Expand All @@ -38,7 +37,7 @@ def tag_class_value(self) -> str:
return "name"


class OrganizarionSystemDefinedTaxonomy(ModelSystemDefinedTaxonomy):
class OrganizationSystemDefinedTaxonomy(ModelSystemDefinedTaxonomy):
"""
Organization based system taxonomy class.
"""
Expand All @@ -54,61 +53,31 @@ def object_tag_class(self) -> Type:
return OrganizationModelObjectTag


class ContentLanguageTaxonomy(LanguageTaxonomy):
class ContentLanguageTaxonomy(ContentTaxonomyMixin, LanguageTaxonomy):
"""
Language system-defined taxonomy that accepts ContentTags
Inherit `_check_tag` from LanguageTaxonomy and uses
`_check_object` and `_check_taxonomy` from ContentTaxonomy
"""

class Meta:
proxy = True

def _check_object(self, object_tag: ObjectTag) -> bool:
taxonomy = ContentTaxonomy().copy(self)
return taxonomy._check_object(object_tag) # pylint: disable=protected-access

def _check_taxonomy(self, object_tag: ObjectTag) -> bool:
taxonomy = ContentTaxonomy().copy(self)
return taxonomy._check_taxonomy(object_tag) # pylint: disable=protected-access


class ContentAuthorTaxonomy(UserSystemDefinedTaxonomy):
class ContentAuthorTaxonomy(ContentTaxonomyMixin, UserSystemDefinedTaxonomy):
"""
Author system-defined taxonomy that accepts Content Tags
Inherit `_check_tag` from UserSystemDefinedTaxonomy and uses
`_check_object` and `_check_taxonomy` from ContentTaxonomy
"""

class Meta:
proxy = True

def _check_object(self, object_tag: ObjectTag) -> bool:
taxonomy = ContentTaxonomy().copy(self)
return taxonomy._check_object(object_tag) # pylint: disable=protected-access

def _check_taxonomy(self, object_tag: ObjectTag) -> bool:
taxonomy = ContentTaxonomy().copy(self)
return taxonomy._check_taxonomy(object_tag) # pylint: disable=protected-access


class ContentOrganizationTaxonomy(OrganizarionSystemDefinedTaxonomy):
class ContentOrganizationTaxonomy(ContentTaxonomyMixin, OrganizationSystemDefinedTaxonomy):
"""
Organization system-defined taxonomy that accepts Content Tags
Inherit `_check_tag` from OrganizarionSystemDefinedTaxonomy and uses
`_check_object` and `_check_taxonomy` from ContentTaxonomy
"""

class Meta:
proxy = True

def _check_object(self, object_tag: ObjectTag) -> bool:
taxonomy = ContentTaxonomy().copy(self)
return taxonomy._check_object(object_tag) # pylint: disable=protected-access

def _check_taxonomy(self, object_tag: ObjectTag) -> bool:
taxonomy = ContentTaxonomy().copy(self)
return taxonomy._check_taxonomy(object_tag) # pylint: disable=protected-access

0 comments on commit 9d195a0

Please sign in to comment.