-
Notifications
You must be signed in to change notification settings - Fork 28
feat: integrate django-modeltranslation for academies #1122
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
Closed
Closed
Changes from all commits
Commits
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
93 changes: 93 additions & 0 deletions
93
enterprise_catalog/apps/academy/migrations/0004_academy_long_description_en_and_more.py
This file contains hidden or 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,93 @@ | ||
| # Generated by Django 5.2.8 on 2025-12-12 09:55 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('academy', '0003_mariadb_uuid_conversion'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='academy', | ||
| name='long_description_en', | ||
| field=models.TextField(help_text='Long description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='academy', | ||
| name='long_description_es', | ||
| field=models.TextField(help_text='Long description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='academy', | ||
| name='short_description_en', | ||
| field=models.TextField(help_text='Short description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='academy', | ||
| name='short_description_es', | ||
| field=models.TextField(help_text='Short description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='academy', | ||
| name='title_en', | ||
| field=models.CharField(help_text='Academy title', max_length=255, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='academy', | ||
| name='title_es', | ||
| field=models.CharField(help_text='Academy title', max_length=255, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalacademy', | ||
| name='long_description_en', | ||
| field=models.TextField(help_text='Long description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalacademy', | ||
| name='long_description_es', | ||
| field=models.TextField(help_text='Long description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalacademy', | ||
| name='short_description_en', | ||
| field=models.TextField(help_text='Short description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalacademy', | ||
| name='short_description_es', | ||
| field=models.TextField(help_text='Short description of the academy.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalacademy', | ||
| name='title_en', | ||
| field=models.CharField(help_text='Academy title', max_length=255, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalacademy', | ||
| name='title_es', | ||
| field=models.CharField(help_text='Academy title', max_length=255, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='tag', | ||
| name='description_en', | ||
| field=models.TextField(help_text='Tag description.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='tag', | ||
| name='description_es', | ||
| field=models.TextField(help_text='Tag description.', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='tag', | ||
| name='title_en', | ||
| field=models.CharField(help_text='Tag title', max_length=255, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='tag', | ||
| name='title_es', | ||
| field=models.CharField(help_text='Tag title', max_length=255, null=True), | ||
| ), | ||
| ] |
97 changes: 97 additions & 0 deletions
97
enterprise_catalog/apps/academy/migrations/0005_populate_translation_fields.py
This file contains hidden or 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,97 @@ | ||
| from django.db import migrations | ||
| from django.db.models import Q | ||
|
|
||
|
|
||
| def batch_update_fields(queryset, field_mapping, batch_size=500): | ||
| """ | ||
| Helper function to update fields in batches using iterator and bulk_update. | ||
|
|
||
| Args: | ||
| queryset: Django queryset (can be filtered) | ||
| field_mapping: dict mapping source field -> target field (e.g., {'title': 'title_en'}) | ||
| batch_size: number of records to process at once | ||
| """ | ||
| records_to_update = [] | ||
|
|
||
| for record in queryset.iterator(chunk_size=batch_size): | ||
| updated = False | ||
|
|
||
| for source_field, target_field in field_mapping.items(): | ||
| source_value = getattr(record, source_field) | ||
| target_value = getattr(record, target_field) | ||
|
|
||
| # Only copy if target is empty but source has data | ||
| if not target_value and source_value: | ||
| setattr(record, target_field, source_value) | ||
| updated = True | ||
|
|
||
| if updated: | ||
| records_to_update.append(record) | ||
|
|
||
| # Bulk update when batch is full | ||
| if len(records_to_update) >= batch_size: | ||
| queryset.model.objects.bulk_update( | ||
| records_to_update, | ||
| list(field_mapping.values()), | ||
| batch_size=batch_size | ||
| ) | ||
| records_to_update = [] | ||
|
|
||
| # Update any remaining records | ||
| if records_to_update: | ||
| queryset.model.objects.bulk_update( | ||
| records_to_update, | ||
| list(field_mapping.values()), | ||
| batch_size=batch_size | ||
| ) | ||
|
|
||
|
|
||
| def populate_translation_fields(apps, schema_editor): | ||
| """ | ||
| Populate English translation fields from original fields for Academy and Tag models. | ||
| """ | ||
| Academy = apps.get_model('academy', 'Academy') | ||
| Tag = apps.get_model('academy', 'Tag') | ||
|
|
||
| batch_size = 500 | ||
|
|
||
| # Update Academy records, only fetch those needing translation | ||
| academy_filter = ( | ||
| Q(title_en__isnull=True) | Q(title_en='') | | ||
| Q(short_description_en__isnull=True) | Q(short_description_en='') | | ||
| Q(long_description_en__isnull=True) | Q(long_description_en='') | ||
| ) | ||
| batch_update_fields( | ||
| Academy.objects.filter(academy_filter), | ||
| { | ||
| 'title': 'title_en', | ||
| 'short_description': 'short_description_en', | ||
| 'long_description': 'long_description_en', | ||
| }, | ||
| batch_size | ||
| ) | ||
|
|
||
| # Update Tag records, only fetch those needing translation | ||
| tag_filter = ( | ||
| Q(title_en__isnull=True) | Q(title_en='') | | ||
| Q(description_en__isnull=True) | Q(description_en='') | ||
| ) | ||
| batch_update_fields( | ||
| Tag.objects.filter(tag_filter), | ||
| { | ||
| 'title': 'title_en', | ||
| 'description': 'description_en', | ||
| }, | ||
| batch_size | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('academy', '0004_academy_long_description_en_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(populate_translation_fields, migrations.RunPython.noop), | ||
| ] |
This file contains hidden or 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 hidden or 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,37 @@ | ||
| """ | ||
| Tests for Academy app models. | ||
| """ | ||
| from django.test import TestCase | ||
|
|
||
| from enterprise_catalog.apps.academy.tests.factories import ( | ||
| AcademyFactory, | ||
| TagFactory, | ||
| ) | ||
|
|
||
|
|
||
| class TagModelTests(TestCase): | ||
| """ | ||
| Tests for the Tag model. | ||
| """ | ||
|
|
||
| def test_tag_str_method(self): | ||
| """ | ||
| Test that the Tag __str__ method returns the expected format. | ||
| """ | ||
| tag = TagFactory(title='Test Tag') | ||
| expected_str = '<Tag title="Test Tag">' | ||
| self.assertEqual(str(tag), expected_str) | ||
|
|
||
|
|
||
| class AcademyModelTests(TestCase): | ||
| """ | ||
| Tests for the Academy model. | ||
| """ | ||
|
|
||
| def test_academy_creation(self): | ||
| """ | ||
| Test that an Academy can be created successfully. | ||
| """ | ||
| academy = AcademyFactory(title='Test Academy') | ||
| self.assertIsNotNone(academy.uuid) | ||
| self.assertEqual(academy.title, 'Test Academy') |
This file contains hidden or 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,18 @@ | ||
| import simple_history | ||
| from modeltranslation.translator import TranslationOptions, register | ||
|
|
||
| from enterprise_catalog.apps.academy.models import Academy, Tag | ||
|
|
||
|
|
||
| @register(Academy) | ||
| class AcademyTranslationOptions(TranslationOptions): | ||
| fields = ('title', 'short_description', 'long_description',) | ||
|
|
||
|
|
||
| @register(Tag) | ||
| class TagTranslationOptions(TranslationOptions): | ||
| fields = ('title', 'description',) | ||
|
|
||
|
|
||
| # https://django-simple-history.readthedocs.io/en/latest/common_issues.html#usage-with-django-modeltranslation | ||
| simple_history.register(Academy, inherit=True) | ||
|
muhammad-ammar marked this conversation as resolved.
|
||
This file contains hidden or 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.
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.
Note to Reviewers: Please see https://django-simple-history.readthedocs.io/en/latest/common_issues.html#usage-with-django-modeltranslation for details