Skip to content

Commit

Permalink
feat: New Content System defined models
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Jul 21, 2023
1 parent c07a900 commit 1f8b01e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 103 deletions.
1 change: 1 addition & 0 deletions openedx-learning
Submodule openedx-learning added at fd8d3c
10 changes: 5 additions & 5 deletions openedx/features/content_tagging/fixtures/system_defined.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
required: true
allow_multiple: false
allow_free_text: false
system_defined: true
visible_to_authors: true
_taxonomy_class: openedx.features.content_tagging.models.ContentLanguageTaxonomy
- model: oel_tagging.taxonomy
pk: -2
fields:
Expand All @@ -17,9 +17,9 @@
enabled: true
required: true
allow_multiple: false
allow_free_text: true
system_defined: true
allow_free_text: false
visible_to_authors: false
_taxonomy_class: openedx.features.content_tagging.models.ContentAuthorTaxonomy
- model: oel_tagging.taxonomy
pk: -3
fields:
Expand All @@ -28,6 +28,6 @@
enabled: true
required: true
allow_multiple: false
allow_free_text: true
system_defined: true
allow_free_text: false
visible_to_authors: false
_taxonomy_class: openedx.features.content_tagging.models.ContentOrganizationTaxonomy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def load_system_defined_taxonomies(apps, schema_editor):
"""

# Create system defined taxonomy instances
call_command('loaddata', '--app=oel_tagging', 'system_defined.yaml')
call_command('loaddata', '--app=content_tagging', 'system_defined.yaml')

# Loads language tags
Expand All @@ -21,11 +20,7 @@ def revert_system_defined_taxonomies(apps, schema_editor):
Deletes all system defined taxonomies
"""
Taxonomy = apps.get_model('oel_tagging', 'Taxonomy')
Tag = apps.get_model('oel_tagging', 'Tag')

# For some reason the tags are not deleted when deleting the taxonomy
Tag.objects.filter(taxonomy__system_defined=True).delete()
Taxonomy.objects.filter(system_defined=True).delete()
Taxonomy.objects.filter(id__lt=0).delete()


class Migration(migrations.Migration):
Expand Down
5 changes: 5 additions & 0 deletions openedx/features/content_tagging/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
ContentTag,
ContentTaxonomy,
)
from .system_defined import (
ContentLanguageTaxonomy,
ContentAuthorTaxonomy,
ContentOrganizationTaxonomy,
)
99 changes: 99 additions & 0 deletions openedx/features/content_tagging/models/system_defined.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,108 @@
"""
System defined models
"""
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


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

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 OrganizarionSystemDefinedTaxonomy(ModelSystemDefinedTaxonomy):
"""
Organization based system taxonomy class.
"""

class Meta:
proxy = True

@property
def object_tag_class(self) -> Type:
"""
Returns OrganizationModelObjectTag as ObjectTag subclass associated with this taxonomy.
"""
return OrganizationModelObjectTag


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

class Meta:
proxy = True

def _check_tag(self, object_tag: ObjectTag) -> bool:
return super(LanguageTaxonomy, self)._check_tag(object_tag)


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

class Meta:
proxy = True

def _check_tag(self, object_tag: ObjectTag) -> bool:
return super(UserSystemDefinedTaxonomy, self)._check_tag(object_tag)


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

class Meta:
proxy = True

def _check_tag(self, object_tag: ObjectTag) -> bool:
return super(OrganizarionSystemDefinedTaxonomy, self)._check_tag(object_tag)

This file was deleted.

9 changes: 7 additions & 2 deletions openedx/features/content_tagging/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import ddt
from django.contrib.auth import get_user_model
from django.test.testcases import TestCase, override_settings
from openedx_tagging.core.tagging.models import ObjectTag, Tag
from openedx_tagging.core.tagging.models import (
ObjectTag,
Tag,
UserSystemDefinedTaxonomy,
)
from organizations.models import Organization

from common.djangoapps.student.auth import add_users, update_org_role
Expand Down Expand Up @@ -136,7 +140,8 @@ def test_system_taxonomy(self, perm):
system_taxonomy = api.create_taxonomy(
name="System Languages",
)
system_taxonomy.system_defined = True
system_taxonomy.taxonomy_class = UserSystemDefinedTaxonomy
system_taxonomy = system_taxonomy.cast()
assert self.superuser.has_perm(perm, system_taxonomy)
assert not self.staff.has_perm(perm, system_taxonomy)
assert not self.user_all_orgs.has_perm(perm, system_taxonomy)
Expand Down

0 comments on commit 1f8b01e

Please sign in to comment.