Skip to content

Commit

Permalink
refactor: remove filter words from the app
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Jan 16, 2025
1 parent f031a74 commit f4ae8ee
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 102 deletions.
28 changes: 14 additions & 14 deletions course_discovery/apps/tagging/admin.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)
36 changes: 18 additions & 18 deletions course_discovery/apps/tagging/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')),
Expand All @@ -30,30 +30,30 @@ 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')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('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')),
Expand All @@ -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')),
Expand All @@ -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',)},
Expand Down
33 changes: 18 additions & 15 deletions course_discovery/apps/tagging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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']

Expand All @@ -29,21 +29,21 @@ def has_changed(self):
return False
return self.has_model_changed()

Check warning on line 30 in course_discovery/apps/tagging/models.py

View check run for this annotation

Codecov / codecov/patch

course_discovery/apps/tagging/models.py#L30

Added line #L30 was not covered by tests

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

Check warning on line 43 in course_discovery/apps/tagging/models.py

View check run for this annotation

Codecov / codecov/patch

course_discovery/apps/tagging/models.py#L43

Added line #L43 was not covered by tests

class Meta:
verbose_name_plural = 'Sub Vertical Filters'
verbose_name_plural = 'Sub Verticals'
ordering = ['name']
unique_together = ['name']

Expand All @@ -53,15 +53,15 @@ def has_changed(self):
return False
return self.has_model_changed()

Check warning on line 54 in course_discovery/apps/tagging/models.py

View check run for this annotation

Codecov / codecov/patch

course_discovery/apps/tagging/models.py#L54

Added line #L54 was not covered by tests

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()

Expand All @@ -84,13 +84,16 @@ def has_changed(self):
return False
return self.has_model_changed()

Check warning on line 85 in course_discovery/apps/tagging/models.py

View check run for this annotation

Codecov / codecov/patch

course_discovery/apps/tagging/models.py#L85

Added line #L85 was not covered by tests

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):
Expand Down
26 changes: 13 additions & 13 deletions course_discovery/apps/tagging/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 18 additions & 18 deletions course_discovery/apps/tagging/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,60 @@
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,
)

self.non_draft_course = CourseFactory(title="Try Course", draft=False)
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

self.assertIn(self.active_vertical, queryset)
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
Expand All @@ -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

Expand Down
Loading

0 comments on commit f4ae8ee

Please sign in to comment.