Skip to content

Commit

Permalink
#127 - Adding validation to prescription base.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiMarques98 committed Dec 6, 2017
1 parent a40cb41 commit 1694159
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions medical_prescription/prescription/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

# Empty constants
EMPTY = 0
EMPTY_PRESCRIPTION = "You need at least add a medicine, exam or recommendation."

# Constants for print prescription
MAX_LENGTH_CLINIC = 50
Expand Down
22 changes: 22 additions & 0 deletions medical_prescription/prescription/forms/createprescriptionform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Django
from django import forms
from prescription.validators import PrescriptionBaseValidator


class CreatePrescriptionForm(forms.Form):
Expand All @@ -15,3 +16,24 @@ class CreatePrescriptionForm(forms.Form):
cid = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control',
'placeholder': 'CID'}), required=False)
cid_id = forms.IntegerField(widget=forms.HiddenInput(), required=False)

# Get the legth of all form_sets.
def get_prescription_lengths(self, medicine, exam, recommendation):
self.medicine = medicine
self.exam = exam
self.recommendation = recommendation

def clean(self, *args, **kwargs):
patient = self.cleaned_data.get('patient')
patient_id = self.cleaned_data.get('patient_id')
email = self.cleaned_data.get('email')
cid = self.cleaned_data.get('cid')
cid_id = self.cleaned_data.get('cid_id')

self.validate_all(patient, patient_id, email, cid, cid_id)

def validate_all(self, patient, patient_id, email, cid, cid_id):
validator = PrescriptionBaseValidator()

validator.validate_patient(patient, patient_id)
validator.validate_length_prescription(self.medicine, self.exam, self.recommendation)
1 change: 1 addition & 0 deletions medical_prescription/prescription/validators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Local Django
from .patternvalidator import PatternValidator
from .prescriptionbasevalidator import PrescriptionBaseValidator
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# django
from django import forms
from django.utils.translation import ugettext_lazy as _

# local django
from prescription import constants
from user.constants import (NAME_MAX_LENGHT,
NAME_MIN_LENGTH,
NAME_SIZE
)


class PrescriptionBaseValidator():
"""
Validator to prescription.
"""

def validate_patient(self, patient, patient_id):
if len(patient) > NAME_MAX_LENGHT or len(patient) < NAME_MIN_LENGTH:
raise forms.ValidationError(_(NAME_SIZE))
else:
# Nothing to do.
pass

def validate_length_prescription(self, medicine, exam, recommendation):
valid = False

if len(medicine) == 1 and len(exam) == 1 and len(recommendation) == 1:

for medicine_data in medicine:
if len(medicine_data.data['form_medicine-0-medicine_id']) != 0:
valid = True
else:
# Nothing to do.
pass
for exam_data in exam:
if len(exam_data.data['form_exam-0-exam']) > 5:
valid = True
else:
# Nothing to do.
pass

for recommendation_data in recommendation:
if len(recommendation_data.data['form_recommendation-0-recommendation']) > 5:
valid = True
else:
# Nothing to do.
pass

if not valid:
raise forms.ValidationError(_(constants.EMPTY_PRESCRIPTION))
else:
# Nothing to do.
pass
6 changes: 6 additions & 0 deletions medical_prescription/prescription/views/createprescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ def post(self, request, *args, **kwargs):

data = dict()

prescription_form.get_prescription_lengths(
form_medicine,
form_exam,
form_recommendation
)

# Checks whether the completed forms are valid.
if (prescription_form.is_valid() and form_recommendation.is_valid() and form_exam.is_valid() and form_medicine.is_valid()):

Expand Down

0 comments on commit 1694159

Please sign in to comment.