From f4ae8eedff00fa67afcfe946422294b9d320cff6 Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Thu, 16 Jan 2025 12:46:32 +0500 Subject: [PATCH] refactor: remove filter words from the app --- course_discovery/apps/tagging/admin.py | 28 +++++------ .../apps/tagging/migrations/0001_initial.py | 36 +++++++------- course_discovery/apps/tagging/models.py | 33 +++++++------ .../apps/tagging/tests/factories.py | 26 +++++----- .../apps/tagging/tests/test_admin.py | 36 +++++++------- .../apps/tagging/tests/test_models.py | 48 +++++++++---------- 6 files changed, 105 insertions(+), 102 deletions(-) diff --git a/course_discovery/apps/tagging/admin.py b/course_discovery/apps/tagging/admin.py index a353cb199e..b78477aa7e 100644 --- a/course_discovery/apps/tagging/admin.py +++ b/course_discovery/apps/tagging/admin.py @@ -1,33 +1,33 @@ from django.contrib import admin from course_discovery.apps.course_metadata.models import Course -from course_discovery.apps.tagging.models import CourseVerticalFilter, SubVerticalFilter, VerticalFilter +from course_discovery.apps.tagging.models import CourseVertical, SubVertical, Vertical -@admin.register(VerticalFilter) -class VerticalFilterAdmin(admin.ModelAdmin): +@admin.register(Vertical) +class VerticalAdmin(admin.ModelAdmin): """ - Admin class for VerticalFilter model. + Admin class for Vertical model. """ list_display = ('name', 'is_active', 'slug',) search_fields = ('name',) -@admin.register(SubVerticalFilter) -class SubVericalFilterAdmin(admin.ModelAdmin): +@admin.register(SubVertical) +class SubVerticalAdmin(admin.ModelAdmin): """ - Admin class for SubVerticalFilter model. + Admin class for SubVertical model. """ - list_display = ('name', 'is_active', 'slug', 'vertical_filters') - list_filter = ('vertical_filters', ) + list_display = ('name', 'is_active', 'slug', 'verticals') + list_filter = ('verticals', ) search_fields = ('name',) ordering = ('name',) -@admin.register(CourseVerticalFilter) -class CourseVerticalFiltersAdmin(admin.ModelAdmin): +@admin.register(CourseVertical) +class CourseVerticalAdmin(admin.ModelAdmin): """ - Admin class for CourseVerticalFilters model. + Admin class for CourseVertical model. """ list_display = ('course', 'vertical', 'sub_vertical') list_filter = ('vertical', 'sub_vertical') @@ -42,7 +42,7 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == 'course': kwargs['queryset'] = Course.objects.filter(draft=False) elif db_field.name == 'vertical': - kwargs['queryset'] = VerticalFilter.objects.filter(is_active=True) + kwargs['queryset'] = Vertical.objects.filter(is_active=True) elif db_field.name == 'sub_vertical': - kwargs['queryset'] = SubVerticalFilter.objects.filter(is_active=True) + kwargs['queryset'] = SubVertical.objects.filter(is_active=True) return super().formfield_for_foreignkey(db_field, request, **kwargs) diff --git a/course_discovery/apps/tagging/migrations/0001_initial.py b/course_discovery/apps/tagging/migrations/0001_initial.py index b8e9853563..1f03bc2a71 100644 --- a/course_discovery/apps/tagging/migrations/0001_initial.py +++ b/course_discovery/apps/tagging/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.14 on 2025-01-15 14:59 +# Generated by Django 4.2.14 on 2025-01-16 07:38 from django.conf import settings from django.db import migrations, models @@ -20,7 +20,7 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='VerticalFilter', + name='Vertical', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), @@ -30,13 +30,13 @@ class Migration(migrations.Migration): ('is_active', models.BooleanField(default=True)), ], options={ - 'verbose_name_plural': 'Vertical Filters', + 'verbose_name_plural': 'Verticals', 'ordering': ['name'], 'unique_together': {('name',)}, }, ), migrations.CreateModel( - name='SubVerticalFilter', + name='SubVertical', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), @@ -44,16 +44,16 @@ class Migration(migrations.Migration): ('name', models.CharField(max_length=255, unique=True)), ('slug', django_extensions.db.fields.AutoSlugField(blank=True, editable=False, max_length=255, populate_from='name', unique=True)), ('is_active', models.BooleanField(default=True)), - ('vertical_filters', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sub_vertical_filters', to='tagging.verticalfilter')), + ('verticals', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sub_verticals', to='tagging.vertical')), ], options={ - 'verbose_name_plural': 'Sub Vertical Filters', + 'verbose_name_plural': 'Sub Verticals', 'ordering': ['name'], 'unique_together': {('name',)}, }, ), migrations.CreateModel( - name='HistoricalVerticalFilter', + name='HistoricalVertical', fields=[ ('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')), ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), @@ -68,15 +68,15 @@ class Migration(migrations.Migration): ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), ], options={ - 'verbose_name': 'historical vertical filter', - 'verbose_name_plural': 'historical Vertical Filters', + 'verbose_name': 'historical vertical', + 'verbose_name_plural': 'historical Verticals', 'ordering': ('-history_date', '-history_id'), 'get_latest_by': ('history_date', 'history_id'), }, bases=(simple_history.models.HistoricalChanges, models.Model), ), migrations.CreateModel( - name='HistoricalSubVerticalFilter', + name='HistoricalSubVertical', fields=[ ('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')), ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), @@ -89,28 +89,28 @@ class Migration(migrations.Migration): ('history_change_reason', models.CharField(max_length=100, null=True)), ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)), ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), - ('vertical_filters', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='tagging.verticalfilter')), + ('verticals', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='tagging.vertical')), ], options={ - 'verbose_name': 'historical sub vertical filter', - 'verbose_name_plural': 'historical Sub Vertical Filters', + 'verbose_name': 'historical sub vertical', + 'verbose_name_plural': 'historical Sub Verticals', 'ordering': ('-history_date', '-history_id'), 'get_latest_by': ('history_date', 'history_id'), }, bases=(simple_history.models.HistoricalChanges, models.Model), ), migrations.CreateModel( - name='CourseVerticalFilter', + name='CourseVertical', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), ('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')), - ('course', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='vertical_filters', to='course_metadata.course')), - ('sub_vertical', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_sub_vertical_filters', to='tagging.subverticalfilter')), - ('vertical', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_vertical_filters', to='tagging.verticalfilter')), + ('course', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='verticals', to='course_metadata.course')), + ('sub_vertical', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_sub_verticals', to='tagging.subvertical')), + ('vertical', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_verticals', to='tagging.vertical')), ], options={ - 'verbose_name_plural': 'Course Vertical Filters', + 'verbose_name_plural': 'Course Verticals', 'ordering': ['vertical', 'sub_vertical'], 'abstract': False, 'unique_together': {('course',)}, diff --git a/course_discovery/apps/tagging/models.py b/course_discovery/apps/tagging/models.py index 9ca2bba707..f3e6ace228 100644 --- a/course_discovery/apps/tagging/models.py +++ b/course_discovery/apps/tagging/models.py @@ -6,9 +6,9 @@ from course_discovery.apps.course_metadata.models import Course, ManageHistoryMixin -class VerticalFilter(ManageHistoryMixin, TimeStampedModel): +class Vertical(ManageHistoryMixin, TimeStampedModel): """ - Model for defining vertical filters used to categorize product types under specific verticals. + Model for defining verticals used to categorize product types """ name = models.CharField(max_length=255, unique=True) slug = AutoSlugField(populate_from='name', max_length=255, unique=True) @@ -19,7 +19,7 @@ def __str__(self): return self.name class Meta: - verbose_name_plural = 'Vertical Filters' + verbose_name_plural = 'Verticals' ordering = ['name'] unique_together = ['name'] @@ -29,21 +29,21 @@ def has_changed(self): return False return self.has_model_changed() -class SubVerticalFilter(ManageHistoryMixin, TimeStampedModel): +class SubVertical(ManageHistoryMixin, TimeStampedModel): """ - Model for defining sub vertical filters used to categorize product types under specific verticals. + Model for defining sub-verticals used to categorize product types under specific verticals. """ name = models.CharField(max_length=255, unique=True) slug = AutoSlugField(populate_from='name', max_length=255, unique=True) is_active = models.BooleanField(default=True) - vertical_filters = models.ForeignKey(VerticalFilter, on_delete=models.CASCADE, related_name='sub_vertical_filters') + verticals = models.ForeignKey(Vertical, on_delete=models.CASCADE, related_name='sub_verticals') history = HistoricalRecords() def __str__(self): return self.name class Meta: - verbose_name_plural = 'Sub Vertical Filters' + verbose_name_plural = 'Sub Verticals' ordering = ['name'] unique_together = ['name'] @@ -53,15 +53,15 @@ def has_changed(self): return False return self.has_model_changed() -class BaseVerticalFilter(ManageHistoryMixin, TimeStampedModel): +class BaseVertical(ManageHistoryMixin, TimeStampedModel): """ - Abstract base model for assigning vertical and sub vertical filters to product types. + Abstract base model for assigning vertical and sub verticals to product types. """ vertical = models.ForeignKey( - VerticalFilter, on_delete=models.CASCADE, related_name="%(class)s_vertical_filters" + Vertical, on_delete=models.CASCADE, related_name="%(class)s_verticals" ) sub_vertical = models.ForeignKey( - SubVerticalFilter, on_delete=models.CASCADE, related_name="%(class)s_sub_vertical_filters" + SubVertical, on_delete=models.CASCADE, related_name="%(class)s_sub_verticals" ) history = HistoricalRecords() @@ -84,13 +84,16 @@ def has_changed(self): return False return self.has_model_changed() -class CourseVerticalFilter(BaseVerticalFilter): +class CourseVertical(BaseVertical): + """ + Model for assigning vertical and sub verticals to courses + """ course = models.OneToOneField( - Course, on_delete=models.CASCADE, related_name="vertical_filters" + Course, on_delete=models.CASCADE, related_name="verticals" ) - class Meta(BaseVerticalFilter.Meta): - verbose_name_plural = "Course Vertical Filters" + class Meta(BaseVertical.Meta): + verbose_name_plural = "Course Verticals" unique_together = ["course"] def get_object_title(self): diff --git a/course_discovery/apps/tagging/tests/factories.py b/course_discovery/apps/tagging/tests/factories.py index a4ad63e523..fca56261f9 100644 --- a/course_discovery/apps/tagging/tests/factories.py +++ b/course_discovery/apps/tagging/tests/factories.py @@ -4,38 +4,38 @@ from factory.fuzzy import FuzzyText from course_discovery.apps.course_metadata.tests.factories import CourseFactory -from course_discovery.apps.tagging.models import CourseVerticalFilter, SubVerticalFilter, VerticalFilter +from course_discovery.apps.tagging.models import CourseVertical, SubVertical, Vertical -class VerticalFilterFactory(DjangoModelFactory): +class VerticalFactory(DjangoModelFactory): """ - Factory for VerticalFilter model + Factory for Vertical model """ class Meta: - model = VerticalFilter + model = Vertical name = FuzzyText() is_active = True -class SubVerticalFilterFactory(DjangoModelFactory): +class SubVerticalFactory(DjangoModelFactory): """ - Factory for SubVerticalFilter model + Factory for SubVertical model """ class Meta: - model = SubVerticalFilter + model = SubVertical name = FuzzyText() - vertical_filters = factory.SubFactory(VerticalFilterFactory) + verticals = factory.SubFactory(VerticalFactory) is_active = True -class CourseVerticalFilterFactory(DjangoModelFactory): +class CourseVerticalFactory(DjangoModelFactory): """ - Factory for CourseVerticalFilter model + Factory for CourseVertical model """ class Meta: - model = CourseVerticalFilter + model = CourseVertical course = factory.SubFactory(CourseFactory) - vertical = factory.SubFactory(VerticalFilterFactory) - sub_vertical = factory.SubFactory(SubVerticalFilterFactory) + vertical = factory.SubFactory(VerticalFactory) + sub_vertical = factory.SubFactory(SubVerticalFactory) diff --git a/course_discovery/apps/tagging/tests/test_admin.py b/course_discovery/apps/tagging/tests/test_admin.py index d55acaf112..a43efe278d 100644 --- a/course_discovery/apps/tagging/tests/test_admin.py +++ b/course_discovery/apps/tagging/tests/test_admin.py @@ -5,27 +5,27 @@ from django.test import RequestFactory, TestCase from course_discovery.apps.course_metadata.tests.factories import CourseFactory -from course_discovery.apps.tagging.admin import CourseVerticalFiltersAdmin -from course_discovery.apps.tagging.models import CourseVerticalFilter, SubVerticalFilter, VerticalFilter -from course_discovery.apps.tagging.tests.factories import SubVerticalFilterFactory, VerticalFilterFactory +from course_discovery.apps.tagging.admin import CourseVerticalAdmin +from course_discovery.apps.tagging.models import CourseVertical, SubVertical, Vertical +from course_discovery.apps.tagging.tests.factories import SubVerticalFactory, VerticalFactory class AdminTests(TestCase): def setUp(self): self.factory = RequestFactory() - self.active_vertical = VerticalFilterFactory(name="Technology", is_active=True) - self.inactive_vertical = VerticalFilterFactory( + self.active_vertical = VerticalFactory(name="Technology", is_active=True) + self.inactive_vertical = VerticalFactory( name="Inactive Vertical", is_active=False ) - self.active_sub_vertical = SubVerticalFilterFactory( + self.active_sub_vertical = SubVerticalFactory( name="Software Engineering", - vertical_filters=self.active_vertical, + verticals=self.active_vertical, is_active=True, ) - self.inactive_sub_vertical = SubVerticalFilterFactory( + self.inactive_sub_vertical = SubVerticalFactory( name="Inactive SubVertical", - vertical_filters=self.active_vertical, + verticals=self.active_vertical, is_active=False, ) @@ -33,20 +33,20 @@ def setUp(self): self.draft_course = CourseFactory(title="Draft Try Course", draft=True) def test_vertical_filter_admin_registration(self): - self.assertIn(VerticalFilter, site._registry) + self.assertIn(Vertical, site._registry) def test_sub_vertical_filter_admin_registration(self): - self.assertIn(SubVerticalFilter, site._registry) + self.assertIn(SubVertical, site._registry) def test_course_vertical_filter_admin_registration(self): - self.assertIn(CourseVerticalFilter, site._registry) + self.assertIn(CourseVertical, site._registry) def test_formfield_for_foreignkey_filters_active_verticals(self): - admin_instance = CourseVerticalFiltersAdmin(CourseVerticalFilter, site) + admin_instance = CourseVerticalAdmin(CourseVertical, site) request = self.factory.get("/admin/") formfield = admin_instance.formfield_for_foreignkey( - db_field=CourseVerticalFilter._meta.get_field("vertical"), request=request + db_field=CourseVertical._meta.get_field("vertical"), request=request ) queryset = formfield.queryset @@ -54,11 +54,11 @@ def test_formfield_for_foreignkey_filters_active_verticals(self): self.assertNotIn(self.inactive_vertical, queryset) def test_formfield_for_foreignkey_filters_active_sub_verticals(self): - admin_instance = CourseVerticalFiltersAdmin(CourseVerticalFilter, site) + admin_instance = CourseVerticalAdmin(CourseVertical, site) request = self.factory.get("/admin/") formfield = admin_instance.formfield_for_foreignkey( - db_field=CourseVerticalFilter._meta.get_field("sub_vertical"), + db_field=CourseVertical._meta.get_field("sub_vertical"), request=request, ) queryset = formfield.queryset @@ -67,11 +67,11 @@ def test_formfield_for_foreignkey_filters_active_sub_verticals(self): self.assertNotIn(self.inactive_sub_vertical, queryset) def test_formfield_for_foreignkey_filters_non_draft_courses(self): - admin_instance = CourseVerticalFiltersAdmin(CourseVerticalFilter, site) + admin_instance = CourseVerticalAdmin(CourseVertical, site) request = self.factory.get("/admin/") formfield = admin_instance.formfield_for_foreignkey( - db_field=CourseVerticalFilter._meta.get_field("course"), request=request + db_field=CourseVertical._meta.get_field("course"), request=request ) queryset = formfield.queryset diff --git a/course_discovery/apps/tagging/tests/test_models.py b/course_discovery/apps/tagging/tests/test_models.py index ab56ce9a72..3f635d5cd1 100644 --- a/course_discovery/apps/tagging/tests/test_models.py +++ b/course_discovery/apps/tagging/tests/test_models.py @@ -5,58 +5,58 @@ from course_discovery.apps.course_metadata.tests.factories import CourseFactory from course_discovery.apps.tagging.tests.factories import ( - CourseVerticalFilterFactory, SubVerticalFilterFactory, VerticalFilterFactory + CourseVerticalFactory, SubVerticalFactory, VerticalFactory ) class VerticalFilterModelTests(TestCase): def setUp(self): - self.vertical_filter = VerticalFilterFactory(name="Test Vertical Filter", is_active=True) + self.vertical = VerticalFactory(name="Test Vertical", is_active=True) def test_vertical_filter_creation(self): - self.assertEqual(self.vertical_filter.name, "Test Vertical Filter") - self.assertTrue(self.vertical_filter.is_active) - self.assertTrue(self.vertical_filter.slug == "test-vertical-filter") - self.assertEqual(str(self.vertical_filter), "Test Vertical Filter") + self.assertEqual(self.vertical.name, "Test Vertical") + self.assertTrue(self.vertical.is_active) + self.assertTrue(self.vertical.slug == "test-vertical") + self.assertEqual(str(self.vertical), "Test Vertical") def test_unique_name_constraint(self): with self.assertRaises(Exception): - VerticalFilterFactory(name="Test Vertical Filter") + VerticalFactory(name="Test Vertical") class SubVerticalFilterModelTests(TestCase): def setUp(self): - self.vertical_filter = VerticalFilterFactory(name="Technology") - self.sub_vertical_filter = SubVerticalFilterFactory( - name="Software Engineering", vertical_filters=self.vertical_filter + self.vertical = VerticalFactory(name="Technology") + self.sub_vertical = SubVerticalFactory( + name="Software Engineering", verticals=self.vertical ) def test_sub_vertical_filter_creation(self): - self.assertEqual(self.sub_vertical_filter.name, "Software Engineering") - self.assertEqual(self.sub_vertical_filter.vertical_filters, self.vertical_filter) - self.assertTrue(self.sub_vertical_filter.slug == "software-engineering") - self.assertTrue(self.sub_vertical_filter.is_active) + self.assertEqual(self.sub_vertical.name, "Software Engineering") + self.assertEqual(self.sub_vertical.verticals, self.vertical) + self.assertTrue(self.sub_vertical.slug == "software-engineering") + self.assertTrue(self.sub_vertical.is_active) def test_unique_name_constraint(self): with self.assertRaises(Exception): - SubVerticalFilterFactory(name="Software Engineering", vertical_filters=self.vertical_filter) + SubVerticalFactory(name="Software Engineering", verticals=self.vertical) class CourseVerticalFilterModelTests(TestCase): def setUp(self): self.course_draft = CourseFactory(title="Test Course", draft=True) self.course = CourseFactory(title="Test Course", draft=False, draft_version_id=self.course_draft.id) - self.vertical_filter = VerticalFilterFactory(name="Test Vertical") - self.sub_vertical_filter = SubVerticalFilterFactory( - name="Test Sub Vertical", vertical_filters=self.vertical_filter + self.vertical = VerticalFactory(name="Test Vertical") + self.sub_vertical = SubVerticalFactory( + name="Test Sub Vertical", verticals=self.vertical ) - self.course_vertical_filter = CourseVerticalFilterFactory( - course=self.course, vertical=self.vertical_filter, sub_vertical=self.sub_vertical_filter + self.course_vertical = CourseVerticalFactory( + course=self.course, vertical=self.vertical, sub_vertical=self.sub_vertical ) def test_course_vertical_filter_creation(self): - self.assertEqual(self.course_vertical_filter.course, self.course) - self.assertEqual(self.course_vertical_filter.vertical, self.vertical_filter) - self.assertEqual(self.course_vertical_filter.sub_vertical, self.sub_vertical_filter) - self.assertEqual(str(self.course_vertical_filter), "Test Course - Test Vertical - Test Sub Vertical") + self.assertEqual(self.course_vertical.course, self.course) + self.assertEqual(self.course_vertical.vertical, self.vertical) + self.assertEqual(self.course_vertical.sub_vertical, self.sub_vertical) + self.assertEqual(str(self.course_vertical), "Test Course - Test Vertical - Test Sub Vertical")