From 31c4dce6bf3e40e2d33c10c93f6910759dd2e3bc Mon Sep 17 00:00:00 2001 From: Saleem Latif Date: Mon, 23 Oct 2023 15:53:16 +0500 Subject: [PATCH] feat: Removed direct usages objects in favour of whitelisted or blacklisted query sets. --- CHANGELOG.rst | 4 ++++ taxonomy/__init__.py | 2 +- taxonomy/algolia/serializers.py | 10 +++++++--- taxonomy/algolia/utils.py | 8 ++++---- taxonomy/api/v1/serializers.py | 11 ++++++++++- taxonomy/utils.py | 2 +- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c48e527d..e8b318fb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,10 @@ Change Log Unreleased +[1.46.0] - 2023-10-23 +--------------------- +* feat: Removed direct usages of JobSkills and IndustryJobSkills objects in favour of whitelisted or blacklisted query sets. + [1.45.0] - 2023-10-13 --------------------- * feat: Added the ability to blacklist job-skill relationship. diff --git a/taxonomy/__init__.py b/taxonomy/__init__.py index 6709612d..25c8c5e1 100644 --- a/taxonomy/__init__.py +++ b/taxonomy/__init__.py @@ -15,6 +15,6 @@ # 2. MINOR version when you add functionality in a backwards compatible manner, and # 3. PATCH version when you make backwards compatible bug fixes. # More details can be found at https://semver.org/ -__version__ = '1.45.0' +__version__ = '1.46.0' default_app_config = 'taxonomy.apps.TaxonomyConfig' # pylint: disable=invalid-name diff --git a/taxonomy/algolia/serializers.py b/taxonomy/algolia/serializers.py index 0c3e1f18..a70b1774 100644 --- a/taxonomy/algolia/serializers.py +++ b/taxonomy/algolia/serializers.py @@ -73,7 +73,11 @@ def get_skills(self, obj): obj (Job): Job instance whose skills need to be fetched. """ # We only need to fetch up-to 20 skills per job. - qs = JobSkills.objects.filter(job=obj).select_related('skill')[:EMBEDDED_OBJECT_LENGTH_CAP] + qs = JobSkills.get_whitelisted_job_skill_qs().filter( + job=obj + ).select_related( + 'skill' + )[:EMBEDDED_OBJECT_LENGTH_CAP] serializer = JobSkillSerializer(qs, many=True) return serializer.data @@ -97,7 +101,7 @@ def get_industry_names(self, obj): obj (Job): Job instance whose industries need to be fetched. """ return list( - IndustryJobSkill.objects.filter( + IndustryJobSkill.get_whitelisted_job_skill_qs().filter( job=obj ).order_by( 'industry__name' @@ -114,7 +118,7 @@ def get_industries(self, obj): """ industries = [] job_industries = list( - IndustryJobSkill.objects.filter( + IndustryJobSkill.get_whitelisted_job_skill_qs().filter( job=obj ).order_by( 'industry__name' diff --git a/taxonomy/algolia/utils.py b/taxonomy/algolia/utils.py index 77684668..c2143a95 100644 --- a/taxonomy/algolia/utils.py +++ b/taxonomy/algolia/utils.py @@ -102,7 +102,7 @@ def calculate_job_skills(jobs_qs): job_details = {} for job in jobs_qs.all(): skills = set( - JobSkills.objects.filter(job=job).values_list('skill__name', flat=True) + JobSkills.get_whitelisted_job_skill_qs().filter(job=job).values_list('skill__name', flat=True) ) job_details[job.name] = { 'skills': skills, @@ -207,7 +207,7 @@ def combine_industry_skills(): for industry in Industry.objects.all(): # sum all significances for the same skill and then sort on total significance skills = list( - IndustryJobSkill.objects.filter( + IndustryJobSkill.get_whitelisted_job_skill_qs().filter( industry=industry ).values_list( 'skill__name', flat=True @@ -262,8 +262,8 @@ def fetch_jobs_data(): context={ 'jobs_data': jobs_data, 'industry_skills': industry_skills, - 'jobs_having_job_skills': get_job_ids(JobSkills.objects), - 'jobs_having_industry_skills': get_job_ids(IndustryJobSkill.objects), + 'jobs_having_job_skills': get_job_ids(JobSkills.get_whitelisted_job_skill_qs()), + 'jobs_having_industry_skills': get_job_ids(IndustryJobSkill.get_whitelisted_job_skill_qs()), }, ) jobs.extend(job_serializer.data) diff --git a/taxonomy/api/v1/serializers.py b/taxonomy/api/v1/serializers.py index 20cc9343..34ebea84 100644 --- a/taxonomy/api/v1/serializers.py +++ b/taxonomy/api/v1/serializers.py @@ -47,13 +47,22 @@ class Meta: class JobsListSerializer(ModelSerializer): - skills = JobSkillSerializer(source='jobskills_set.all', many=True) + skills = serializers.SerializerMethodField() class Meta: model = Job fields = '__all__' extra_fields = ('skills',) + def get_skills(self, instance): + """ + Get JobSkill records. + """ + return JobSkillSerializer( + JobSkills.get_whitelisted_job_skill_qs().filter(job=instance), + many=True, + ).data + class CourseSkillsSerializer(ModelSerializer): diff --git a/taxonomy/utils.py b/taxonomy/utils.py index 11cfbcc1..7abd2897 100644 --- a/taxonomy/utils.py +++ b/taxonomy/utils.py @@ -492,7 +492,7 @@ def get_course_jobs(course_key, product_type=ProductTypes.Course): list: A list of dicts where each dict contain information about a particular job. """ course_skills = get_whitelisted_product_skills(course_key, product_type) - job_skills = JobSkills.objects.select_related( + job_skills = JobSkills.get_whitelisted_job_skill_qs().select_related( 'skill', 'job', 'job__jobpostings',