Skip to content

Commit a80d645

Browse files
skills list api
ENT-4239
1 parent 433b3ef commit a80d645

File tree

9 files changed

+109
-1
lines changed

9 files changed

+109
-1
lines changed

taxonomy/api/__init__.py

Whitespace-only changes.

taxonomy/api/v1/__init__.py

Whitespace-only changes.

taxonomy/api/v1/serializers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from rest_framework import serializers
2+
from taxonomy.models import Skill
3+
4+
5+
class SkillSerializer(serializers.ModelSerializer):
6+
""" Skill Searlizer """
7+
8+
class Meta:
9+
model = Skill
10+
fields = ('name', 'description')

taxonomy/api/v1/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.urls import path
2+
from taxonomy.api.v1.views import SkillsView
3+
4+
urlpatterns = [
5+
path('api/v1/skills/', SkillsView.as_view(), name='skill_list')
6+
]

taxonomy/api/v1/views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from rest_framework.generics import ListAPIView
2+
from rest_framework.permissions import IsAuthenticated
3+
from rest_framework import filters
4+
from taxonomy.models import Skill, CourseSkills
5+
from taxonomy.api.v1.serializers import SkillSerializer
6+
7+
8+
class SkillsView(ListAPIView):
9+
""" List view for Skills """
10+
filter_backends = [filters.SearchFilter]
11+
search_fields = ['^name']
12+
permission_classes = [IsAuthenticated]
13+
serializer_class = SkillSerializer
14+
15+
def get_queryset(self):
16+
blacklisted_skill_ids = list(CourseSkills.objcts.filter(is_blacklised=True).values_list('skill__id', flat=True))
17+
return Skill.objects.exclude(id__in=blacklisted_skill_ids)
18+

taxonomy/urls.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
Taxonomy Connector URL Configuration.
44
"""
55

6-
from django.urls import re_path
6+
from django.urls import re_path, include
77

88
from taxonomy import views
9+
from taxonomy.api.v1.urls import urlpatterns as api_v1_urlpatterns
10+
911

1012
urlpatterns = [
1113
re_path(
1214
r"^admin/taxonomy/refresh_course_skills/$", views.RefreshCourseSkills.as_view(), name="refresh_course_skills"
1315
),
1416
]
17+
18+
urlpatterns += api_v1_urlpatterns

test_settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ def root(*args):
6969
CELERY_BROKER_URL = 'memory://localhost/'
7070

7171
### END CELERY
72+
73+
REST_FRAMEWORK = {
74+
'DEFAULT_AUTHENTICATION_CLASSES': (
75+
'rest_framework.authentication.SessionAuthentication',
76+
'rest_framework.authentication.BasicAuthentication',
77+
),
78+
}

test_utils/factories.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import factory
66
from faker import Factory as FakerFactory
77

8+
from django.contrib.auth.models import User
9+
810
from taxonomy.models import CourseSkills, Job, JobPostings, JobSkills, Skill
911

1012
FAKER = FakerFactory.create()
@@ -94,3 +96,20 @@ class Meta:
9496
median_posting_duration = factory.LazyAttribute(lambda x: FAKER.pyint(min_value=0, max_value=100000000))
9597
unique_postings = factory.LazyAttribute(lambda x: FAKER.pyint(min_value=0, max_value=100000000))
9698
unique_companies = factory.LazyAttribute(lambda x: FAKER.pyint(min_value=0, max_value=100000000))
99+
100+
101+
class UserFactory(factory.django.DjangoModelFactory):
102+
"""
103+
Factory for User model.
104+
"""
105+
106+
class Meta:
107+
model = User
108+
109+
email = factory.LazyAttribute(lambda x: FAKER.email())
110+
username = factory.LazyAttribute(lambda x: FAKER.user_name())
111+
first_name = factory.LazyAttribute(lambda x: FAKER.first_name())
112+
last_name = factory.LazyAttribute(lambda x: FAKER.last_name())
113+
is_superuser = False
114+
is_staff = False
115+
is_active = True

tests/test_api_v1.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from pytest import mark
2+
from django.urls import reverse
3+
4+
from rest_framework.test import APITestCase
5+
from test_utils import factories
6+
7+
8+
TEST_USERNAME = 'taxonomy_user'
9+
TEST_EMAIL = '[email protected]'
10+
TEST_PASSWORD = 'password'
11+
12+
13+
@mark.django_db
14+
class TestSkillsView(APITestCase):
15+
def setUp(self):
16+
"""
17+
Perform operations common to all tests.
18+
"""
19+
super().setUp()
20+
self.user = self.create_user(username=TEST_USERNAME, email=TEST_EMAIL, password=TEST_PASSWORD)
21+
22+
self.url = reverse('skill_list')
23+
24+
self.skill_names = [
25+
('C++', False), ('CLI', False), ('Data Strcutures', False), ('Biochemistry', False),
26+
('Animations', False), ('Algorithms', False), ('VB', True), ('Oracle', True)
27+
]
28+
for skill_name, is_blacklisted in self.skill_names:
29+
skill = factories.SkillFactory(name=skill_name)
30+
factories.CourseSkillsFactory(skill=skill, is_blacklisted=is_blacklisted)
31+
32+
def create_user(self, username=TEST_USERNAME, password=TEST_PASSWORD, **kwargs):
33+
"""
34+
Create a test user and set its password.
35+
"""
36+
user = factories.UserFactory(username=username, **kwargs)
37+
user.set_password(password) # pylint: disable=no-member
38+
user.save() # pylint: disable=no-member
39+
return user
40+
41+
def test_search(self):
42+
self.client.login(username=TEST_USERNAME, password=TEST_PASSWORD)
43+
response = self.client.get(path=self.url)
44+
assert response.status_code == 200

0 commit comments

Comments
 (0)