Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add more filters #25

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions project/accounts/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,60 @@
from django.db.models import Sum

from accounts.models import *
from .user import *




class PatientFilter(filters.FilterSet):
ADDRESS_ATTRIBUTES=['street','city','country']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.generate_address_filters()

def generate_address_filters(self):
for attribute in self.ADDRESS_ATTRIBUTES:
field_name = f'address__{attribute}'
lookup_fields = {
# 'contains': 'CharFilter',
'icontains': 'CharFilter',
'contains': 'CharFilter',
'istartswith': 'CharFilter',
'iexact': 'CharFilter',
'exact': 'CharFilter',
'startswith': 'CharFilter',
# 'gt': 'NumberFilter',
# 'lt': 'NumberFilter',
# 'gte': 'NumberFilter',
# 'lte': 'NumberFilter',

}


for lookup_expr, filter_type in lookup_fields.items():
filter_name = f'{field_name}__{lookup_expr}'
filter_class = getattr(filters, filter_type)
filter_instance = filter_class(field_name=field_name, lookup_expr=lookup_expr)
self.filters[filter_name] = filter_instance



class PatientFilter(filters.FilterSet):
address__street = filters.CharFilter(lookup_expr='icontains', label='Street')

class Meta:
model = Patient
fields = {
'user': ['exact'],
'full_name': ['exact', 'icontains', 'istartswith'],
'full_name': ['exact', 'icontains', 'istartswith','contains','startswith','iexact'],
'created_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],
'nationality': ['exact', 'icontains', 'istartswith'],
'nationality': ['exact', 'icontains', 'istartswith','contains','startswith','iexact'],
'national_id': ['exact'],
'disease_type': ['exact', 'icontains', 'istartswith'],
'blood_type': ['exact', 'icontains', 'istartswith'],
# 'address__street': ['exact', 'icontains', 'istartswith'],
# 'address__city': ['exact', 'icontains', 'istartswith'],
# 'address__governorate': ['exact', 'icontains', 'istartswith'],
# 'phone__mobile': ['exact', 'icontains', 'istartswith'],
'disease_type': ['exact', 'contains', 'startswith', 'iexact', 'icontains', 'istartswith'],
'blood_type': ['exact', 'contains', 'startswith', 'iexact', 'icontains', 'istartswith'],



}


class UserImageFilter(filters.FilterSet):
class Meta:
model = UserImage
Expand Down
10 changes: 10 additions & 0 deletions project/accounts/filters/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# from django_filters import rest_framework as filters
# class AddressFilter:
# def __init__(self, address_prop, lookups):
# self.address_prop = address_prop
# self.lookups = lookups

# def apply_filter(self, queryset, lookup_type, value):
# lookup = f"{self.address_prop}__{lookup_type}"
# filter_args = {lookup: value}
# return queryset.filter(**filter_args)
42 changes: 37 additions & 5 deletions project/accounts/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ class PatientTestCase(TestSetup):
def setUp(self) -> None:
super().setUp()
self.staff, self.staff_token = self.create_staff()
self.patient1, self.patient1_token = self.create_patient(self.staff_token,national_id='11111111111111',full_name='test1')
self.patient1, self.patient1_token = self.create_patient(self.staff_token,
national_id='11111111111111',
full_name='test9',
address={
'street': 'test',
'city': 'egypt',
'governorate': 'test'
},)
self.patient2, self.patient2_token = self.create_patient(self.staff_token,national_id='22222222222222',full_name='test2')
self.doctor,self.doctor_token = self.create_doctor(self.staff_token,national_id='111111111111171')
self.visit1 = self.create_visit(self.staff_token,patient_id=self.patient1['id'],start_at='2020-01-01')
Expand All @@ -27,13 +34,38 @@ def test_get_ordering(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 2)
patients = response.data['results']
self.assertEqual(patients[0]['full_name'], 'test1')
self.assertEqual(patients[1]['full_name'], 'test2')
self.assertEqual(patients[0]['full_name'], 'test2')
self.assertEqual(patients[1]['full_name'], 'test9')
url ='/accounts/patient/?ordering=-full_name'
response = self.client.get(
url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 2)
patients = response.data['results']
self.assertEqual(patients[0]['full_name'], 'test2')
self.assertEqual(patients[1]['full_name'], 'test1')
self.assertEqual(patients[0]['full_name'], 'test9')
self.assertEqual(patients[1]['full_name'], 'test2')
def test_patient_address(self):
url='/accounts/patient/?address__city__istartswith=egypt'
response = self.client.get(
url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
def test_patient_address2(self):
url='/accounts/patient/?address__city__icontains=egypt'
response = self.client.get(
url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
def test_patient_address2(self):
url='/accounts/patient/?address__city__icontains=egypt'
response = self.client.get(
url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
def test_patient_full_name(self):
url='/accounts/patient/?full_name__icontains=test9'
response = self.client.get(
url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
# print(response.data)
self.assertEqual(len(response.data['results']), 1)
2 changes: 1 addition & 1 deletion project/accounts/views/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ def create(self , request, *args, **kwargs):


return Response(PatientSerializer(patient).data, status=status.HTTP_201_CREATED)

52 changes: 38 additions & 14 deletions project/visit/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,47 @@

from visit.models import *
from accounts.models import *
from django.db.models import Q


class VisitFilter(filters.FilterSet):


MEASUREMENT_ATTRIBUTES=['weight','height','blood_pressure','temperature','pulse','oxygen_level']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.generate_address_filters()

def generate_address_filters(self):
for attribute in self.MEASUREMENT_ATTRIBUTES:
field_name = f'measurement__{attribute}'
lookup_fields = {
'icontains': 'CharFilter',
'contains': 'CharFilter',
'istartswith': 'CharFilter',
'iexact': 'CharFilter',
'exact': 'CharFilter',
'startswith': 'CharFilter',
# 'gt': 'NumberFilter',
# 'lt': 'NumberFilter',
# 'gte': 'NumberFilter',
# 'lte': 'NumberFilter',

}


for lookup_expr, filter_type in lookup_fields.items():
filter_name = f'{field_name}__{lookup_expr}'
filter_class = getattr(filters, filter_type)
filter_instance = filter_class(field_name=field_name, lookup_expr=lookup_expr)
self.filters[filter_name] = filter_instance



class Meta:
model = Visit
fields = {
'ticket': ['exact'],
'ticket': ['exact', 'contains','startswith','iexact','icontains','istartswith'],
'status': ['exact'],
'start_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],
'end_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],
Expand All @@ -20,18 +55,7 @@ class Meta:
'created_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],

}
# class MeasurementFilter(filters.FilterSet):
# class Meta:
# model = Measurement
# fields = {
# 'visit': ['exact'],
# 'height': ['exact'],
# 'weight': ['exact'],
# 'blood_pressure': ['exact'],
# 'temperature': ['exact'],
# 'pulse': ['exact'],
# 'oxygen_level': ['exact'],
# 'created_at': ['year', 'month', 'day']

# }
class AttachmentFilter(filters.FilterSet):
class Meta:
Expand All @@ -40,7 +64,7 @@ class Meta:
'user': ['exact'],
'visit__patient': ['exact'],
'visit': ['exact'],
'kind': ['exact', 'icontains', 'istartswith'],
'kind': ['exact', 'contains','startswith','iexact','icontains','istartswith'],
'created_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],
}

Expand Down
18 changes: 17 additions & 1 deletion project/visit/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ def test_get_visit_start_at_gte(self):
url='/visit/visit/?start_at__gte=2020-01-02'
response = self.client.get(url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
self.assertEqual(len(response.data['results']), 1)
def test_get_visit_measurement(self):
measurement={
'height': "1cm",
'weight': "1kg",
"blood_pressure": "1/1",
"temperature": "1C",
"pulse": "1",
"oxygen_level": "1"
}
visit9=self.create_visit(self.staff_token,patient=self.patient1['id'],start_at='2020-01-09',measurement=measurement)

url='/visit/visit/?measurement__height__icontains=1'
response = self.client.get(url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)

9 changes: 7 additions & 2 deletions project/visit/tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ def create_visit(self, staff_token,
patient,
ticket='test',
start_at='2020-01-01',
end_at='2020-01-01',):
end_at='2020-01-01',
measurement={

}
):
data = {
'ticket': ticket,
'patient': patient,
'start_at': start_at,
'end_at': end_at
'end_at': end_at,
'measurement':measurement
}

response = self.client.post(
Expand Down
Loading