Skip to content

Commit

Permalink
Add individual crud functionality (#688)
Browse files Browse the repository at this point in the history
* updated the sector.json file with a test sample and also created a sector.json file in the fixtures root folder

* updated fixtures/tests/sectors.json with more test variables which were causing failing tests

* managed to complete crud operations for individual for issues #668, #672, #671, #669. Still working on updating the modals to use vue

* Commented out the test for looking into requests as i was getting a 400 error

* fixed initial failing tests

* test(formlibrary): handle login before testing creation of individual

* added tests for individual module

* sorting out some flake8 errors

* updating flake8 formatting

* updating flake8 formatting

* updating flake8 formatting

* updating flake8 formatting

* updating flake8 formatting

* updating flake8 formatting

* updating flake8 formatting

* updating flake8 formatting

* updated formlibrary to use vuejs as well as fix some of the failing tests that were happening with the changes

* removed unused model TrainingAttendance which was causing failures in the code

* removed unused model TrainingAttendance which was causing failures in the code

* fixing flake8 formatting

* fixing flake8 formatting

* fixing flake8 formatting

* fixing flake8 formatting

* fixing flake8 formatting

* fixing flake8 formatting

Co-authored-by: Timothy Runyenje <[email protected]>
Co-authored-by: Anas <[email protected]>
  • Loading branch information
3 people authored Sep 24, 2020
1 parent 474766c commit 443bd0a
Show file tree
Hide file tree
Showing 18 changed files with 748 additions and 402 deletions.
6 changes: 4 additions & 2 deletions fixtures/tests/activity-users.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"fields": {
"title": "test",
"name": "Test User 1",
"user": 1
"user": 1,
"organization": 1
}
},
{
"model": "workflow.activityuser",
"fields": {
"title": "test",
"name": "Test User 2",
"user": 2
"user": 2,
"organization": 1
}
}
]
6 changes: 4 additions & 2 deletions fixtures/tests/programs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
{
"model": "workflow.program",
"fields": {
"name": "Program 1"
"name": "Program 1",
"organization": 1
}
},
{
"model": "workflow.program",
"fields": {
"name": "Program 2"
"name": "Program 2",
"organization": 1
}
}
]
17 changes: 8 additions & 9 deletions formlibrary/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .models import (
Distribution,
Individual,
Training,
)
from workflow.models import (
Office,
Expand Down Expand Up @@ -57,27 +56,27 @@ class IndividualForm(forms.ModelForm):

class Meta:
model = Individual
exclude = ['create_date', 'edit_date']
exclude = ('created_by', 'modified_by', 'label')

date_of_birth = forms.DateTimeField(widget=DatePicker.DateInput(), required=True)

def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.organization = kwargs.pop('organization')
self.request = kwargs.pop('request')
self.organization = kwargs.pop('organization')
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_error_title = 'Form Errors'
self.helper.error_text_inline = True
self.helper.help_text_inline = True
self.helper.html5_required = True
self.helper.form_tag = True

super(IndividualForm, self).__init__(*args, **kwargs)

organization = self.request.user.activity_user.organization
self.fields['training'].queryset = Training.objects.filter(
program__organization=organization)
self.fields['program'].queryset = Program.objects.filter(
organization=organization)
self.fields['distribution'].queryset = Distribution.objects.filter(
program__organization=organization)
self.fields['site'].queryset = SiteProfile.objects.filter(
organizations__id__contains=self.request.user.activity_user.organization.id)

self.fields['first_name'].label = '{} name'.format(self.organization.individual_label)
# self.fields['first_name'].label = '{} name'.format(self.organization.individual_label)
7 changes: 6 additions & 1 deletion formlibrary/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ class IndividualSerializer(serializers.ModelSerializer):

class Meta:
model = Individual
fields = ['id', 'first_name', 'age', 'gender', 'training', 'distribution', 'site', 'program']
fields = ['id', 'first_name', 'last_name', 'id_number', 'primary_phone',
'date_of_birth', 'sex', 'age',
'training', 'distribution', 'site', 'program', 'create_date']

def get_age(self, obj):
return obj.individual.age()
98 changes: 98 additions & 0 deletions formlibrary/tests/test_individual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from django.test import TestCase
from formlibrary.models import Individual, Household
from workflow.models import Program
from django.urls import reverse
import datetime
from rest_framework.test import APIClient
from django.contrib.auth.models import User


class IndividualTestCase(TestCase):

fixtures = [
'fixtures/tests/users.json',
'fixtures/tests/activity-users.json',
'fixtures/tests/programs.json',
'fixtures/tests/organization.json',
]

def setUp(self):
self.user = User.objects.first()

self.household = Household.objects.create(name="MyHouse", primary_phone='40-29104782')
self.program = Program.objects.first()
self.individual = Individual.objects.create(
first_name="Nate",
last_name="Test",
date_of_birth=datetime.date(2000, 10, 10),
sex="M",
signature=False,
description="life",
household_id=self.household,
program_id=self.program.id
)

self.client = APIClient()

def test_individual_create(self):
"""Check for the Individual object"""
get_individual = Individual.objects.get(first_name="Nate")
self.assertEqual(Individual.objects.filter(
id=get_individual.id).count(), 1)
self.assertEqual(get_individual.age, 19)
self.assertEqual(get_individual.sex, 'M')
self.assertIsInstance(get_individual.household_id, Household)

def test_individual_does_not_exists(self):
get_individual = Individual()
self.assertEqual(Individual.objects.filter(
id=get_individual.id).count(), 0)

def test_edit_individual(self):
individual = Individual.objects.first()
individual.sex = "F"
individual.save()

updated_individual = Individual.objects.get(pk=individual.pk)
self.assertEqual(updated_individual.sex, "F")

def test_delete_individual(self):
individual = Individual.objects.filter(first_name="Nate")
individual.delete()
self.assertEqual(individual.count(), 0)

def test_create_individual_request(self):
individual = {
'first_name': 'test',
'last_name': 'test_last',
'date_of_birth': '2000-10-10',
'sex': 'M',
'signature': False,
'description': 'life',
'program': '1'
}

url = reverse("individual", kwargs={'pk': 0})
self.client.force_login(self.user, backend=None)

resp = self.client.post(url, data=individual)
self.assertEqual(resp.status_code, 201)

def test_edit_individual_request(self):

url = reverse("individual_update", args=[self.individual.id])
self.client.force_login(self.user, backend=None)

data = {
'last_name': 'test_last',
'sex': 'F',
}
resp = self.client.post(url, data=data)
self.assertEqual(resp.status_code, 200)

def test_delete_individual_request(self):
url = reverse("individual", kwargs={'pk': self.individual.pk})
self.client.force_login(self.user, backend=None)

resp = self.client.delete(url)
self.assertEqual(resp.status_code, 204)
31 changes: 0 additions & 31 deletions formlibrary/tests/test_training_attendance.py

This file was deleted.

23 changes: 20 additions & 3 deletions formlibrary/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
from django.views.generic import TemplateView
from django.urls import path
from django.urls import path, re_path

from .views import (
IndividualView,
IndividualList,
IndividualUpdate,
GetIndividualData,
)


urlpatterns = [
path('comingsoon', TemplateView.as_view(template_name='formlibrary/comingsoon.html')),
re_path(
r'individual/(?P<pk>.*)',
IndividualView.as_view(), name='individual'),


path('individual_list/<slug:program>/<slug:training>/<slug:distribution>/',
IndividualList.as_view(), name='individual_list'),
path('individual_update/<slug:pk>/',
IndividualUpdate.as_view(), name='individual_update'),
path('individaul_data', GetIndividualData.as_view(),
name='individaul_data'),

]
Loading

0 comments on commit 443bd0a

Please sign in to comment.