Skip to content

Commit

Permalink
skills list api
Browse files Browse the repository at this point in the history
ENT-4239
  • Loading branch information
muhammad-ammar committed Mar 4, 2021
1 parent 433b3ef commit a80d645
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 1 deletion.
Empty file added taxonomy/api/__init__.py
Empty file.
Empty file added taxonomy/api/v1/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions taxonomy/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import serializers
from taxonomy.models import Skill


class SkillSerializer(serializers.ModelSerializer):
""" Skill Searlizer """

class Meta:
model = Skill
fields = ('name', 'description')
6 changes: 6 additions & 0 deletions taxonomy/api/v1/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from taxonomy.api.v1.views import SkillsView

urlpatterns = [
path('api/v1/skills/', SkillsView.as_view(), name='skill_list')
]
18 changes: 18 additions & 0 deletions taxonomy/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework import filters
from taxonomy.models import Skill, CourseSkills
from taxonomy.api.v1.serializers import SkillSerializer


class SkillsView(ListAPIView):
""" List view for Skills """
filter_backends = [filters.SearchFilter]
search_fields = ['^name']
permission_classes = [IsAuthenticated]
serializer_class = SkillSerializer

def get_queryset(self):
blacklisted_skill_ids = list(CourseSkills.objcts.filter(is_blacklised=True).values_list('skill__id', flat=True))
return Skill.objects.exclude(id__in=blacklisted_skill_ids)

6 changes: 5 additions & 1 deletion taxonomy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
Taxonomy Connector URL Configuration.
"""

from django.urls import re_path
from django.urls import re_path, include

from taxonomy import views
from taxonomy.api.v1.urls import urlpatterns as api_v1_urlpatterns


urlpatterns = [
re_path(
r"^admin/taxonomy/refresh_course_skills/$", views.RefreshCourseSkills.as_view(), name="refresh_course_skills"
),
]

urlpatterns += api_v1_urlpatterns
7 changes: 7 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ def root(*args):
CELERY_BROKER_URL = 'memory://localhost/'

### END CELERY

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
19 changes: 19 additions & 0 deletions test_utils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import factory
from faker import Factory as FakerFactory

from django.contrib.auth.models import User

from taxonomy.models import CourseSkills, Job, JobPostings, JobSkills, Skill

FAKER = FakerFactory.create()
Expand Down Expand Up @@ -94,3 +96,20 @@ class Meta:
median_posting_duration = factory.LazyAttribute(lambda x: FAKER.pyint(min_value=0, max_value=100000000))
unique_postings = factory.LazyAttribute(lambda x: FAKER.pyint(min_value=0, max_value=100000000))
unique_companies = factory.LazyAttribute(lambda x: FAKER.pyint(min_value=0, max_value=100000000))


class UserFactory(factory.django.DjangoModelFactory):
"""
Factory for User model.
"""

class Meta:
model = User

email = factory.LazyAttribute(lambda x: FAKER.email())
username = factory.LazyAttribute(lambda x: FAKER.user_name())
first_name = factory.LazyAttribute(lambda x: FAKER.first_name())
last_name = factory.LazyAttribute(lambda x: FAKER.last_name())
is_superuser = False
is_staff = False
is_active = True
44 changes: 44 additions & 0 deletions tests/test_api_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from pytest import mark
from django.urls import reverse

from rest_framework.test import APITestCase
from test_utils import factories


TEST_USERNAME = 'taxonomy_user'
TEST_EMAIL = '[email protected]'
TEST_PASSWORD = 'password'


@mark.django_db
class TestSkillsView(APITestCase):
def setUp(self):
"""
Perform operations common to all tests.
"""
super().setUp()
self.user = self.create_user(username=TEST_USERNAME, email=TEST_EMAIL, password=TEST_PASSWORD)

self.url = reverse('skill_list')

self.skill_names = [
('C++', False), ('CLI', False), ('Data Strcutures', False), ('Biochemistry', False),
('Animations', False), ('Algorithms', False), ('VB', True), ('Oracle', True)
]
for skill_name, is_blacklisted in self.skill_names:
skill = factories.SkillFactory(name=skill_name)
factories.CourseSkillsFactory(skill=skill, is_blacklisted=is_blacklisted)

def create_user(self, username=TEST_USERNAME, password=TEST_PASSWORD, **kwargs):
"""
Create a test user and set its password.
"""
user = factories.UserFactory(username=username, **kwargs)
user.set_password(password) # pylint: disable=no-member
user.save() # pylint: disable=no-member
return user

def test_search(self):
self.client.login(username=TEST_USERNAME, password=TEST_PASSWORD)
response = self.client.get(path=self.url)
assert response.status_code == 200

0 comments on commit a80d645

Please sign in to comment.