Skip to content

Commit

Permalink
Merge pull request #5 from RommelTJ/feature/employees-search
Browse files Browse the repository at this point in the history
Feature/employees search
  • Loading branch information
RommelTJ authored Jan 1, 2018
2 parents 0659d0c + 9199012 commit 5ce7be6
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 102 deletions.
19 changes: 17 additions & 2 deletions employees/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,31 @@ def get_variations_urls(self, obj):
return_object[key] = super(StdImageFieldSerializer, self).to_representation(field_obj)

# Also include the original (if possible)
if hasattr(obj, 'url'):
return_object['original'] = super(StdImageFieldSerializer, self).to_representation(obj)
try:
if hasattr(obj, 'url'):
return_object['original'] = super(StdImageFieldSerializer, self).to_representation(obj)
except ValueError:
pass

return return_object


class EmployeeModelSerializer(serializers.ModelSerializer):
user = UserDisplaySerializer(read_only=True)
primary_activity = serializers.SerializerMethodField()
secondary_activity = serializers.SerializerMethodField()
type = serializers.SerializerMethodField()
image = StdImageFieldSerializer()

def get_primary_activity(self, obj):
return Employee.ACTIVITY_DICT[obj.primary_activity]

def get_secondary_activity(self, obj):
return Employee.ACTIVITY_DICT[obj.secondary_activity]

def get_type(self, obj):
return Employee.EMPLOYEE_DICT[obj.type]

class Meta:
model = Employee
fields = "__all__"
32 changes: 31 additions & 1 deletion employees/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import generics, permissions
from django.db.models import Q

from employees.models import Employee
from .pagination import StandardResultsPagination
Expand All @@ -15,9 +16,38 @@ def get_serializer_context(self, *args, **kwargs):
return context

def get_queryset(self, *args, **kwargs):
qs = Employee.objects.filter(type__in=["MEM", "AFF", "PRE", "SEC", "CFO"])
qs = Employee.objects.exclude(type__in=["RET", "KIA"])

query = self.request.GET.get("q", None)
if query is not None:
type_search = query
activity_search = query
if query is not "":
type_search = self.search(Employee.EMPLOYEE_DICT, query)
activity_search = self.search(Employee.ACTIVITY_DICT, query)

if type_search is not "":
qs = qs.filter(type__icontains=type_search)
elif activity_search is not "":
qs = qs.filter(
Q(primary_activity__icontains=activity_search) |
Q(secondary_activity__icontains=activity_search)
)
else:
qs = qs.filter(
Q(user__last_name__icontains=query) |
Q(user__first_name__icontains=query) |
Q(callsign__icontains=query)
)
return qs

def search(self, values, searchFor):
# This search is not very efficient. We should replace it.
for k in values:
if searchFor.lower() in values[k].lower():
return k
return ""


class EmployeeDetailAPIView(generics.ListAPIView):
queryset = Employee.objects.all()
Expand Down
17 changes: 17 additions & 0 deletions employees/migrations/0006_auto_20171228_0646.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0 on 2017-12-28 14:46

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('employees', '0005_auto_20171216_1956'),
]

operations = [
migrations.AlterModelOptions(
name='employee',
options={'ordering': ['emp_no']},
),
]
17 changes: 17 additions & 0 deletions employees/migrations/0007_auto_20171231_1618.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0 on 2018-01-01 00:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('employees', '0006_auto_20171228_0646'),
]

operations = [
migrations.AlterModelOptions(
name='employee',
options={'ordering': ['-emp_no']},
),
]
3 changes: 3 additions & 0 deletions employees/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,6 @@ def __str__(self):

def get_absolute_url(self):
return reverse("employee:detail", kwargs={"pk":self.emp_no})

class Meta:
ordering = ["-emp_no"]
Loading

0 comments on commit 5ce7be6

Please sign in to comment.