Skip to content

Commit

Permalink
removed regex and unrequired comments and functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-sharmav committed Nov 5, 2024
1 parent 639aab7 commit 2f4769f
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions care/facility/api/serializers/prescription.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import re
import contextlib

from django.shortcuts import get_object_or_404
from django.utils import timezone
Expand Down Expand Up @@ -100,14 +100,6 @@ class Meta:
)

def validate(self, attrs):
def extract_numeric_value(dosage):
match = re.match(
r"(\d+(\.\d+)?)", dosage
) # Matches digits and optional decimal part
if match:
return float(match.group(1))
return None

if "medicine" in attrs:
attrs["medicine"] = get_object_or_404(
MedibaseMedicine, external_id=attrs["medicine"]
Expand Down Expand Up @@ -137,16 +129,13 @@ def extract_numeric_value(dosage):
{"base_dosage": "Base dosage is required."}
)

# Validate max_dosage is greater than or equal to base_dosage
base_dosage = attrs.get("base_dosage")
max_dosage = attrs.get("max_dosage")

if base_dosage and max_dosage:
# Extract numeric values from dosage strings
base_dosage_value = extract_numeric_value(base_dosage)
max_dosage_value = extract_numeric_value(max_dosage)

# Raise error if max_dosage is less than base_dosage
with contextlib.suppress(ValueError):
base_dosage_value = float(base_dosage.split(" ", maxsplit=1)[0])
max_dosage_value = float(max_dosage.split(" ", maxsplit=1)[0])
if max_dosage_value < base_dosage_value:
raise serializers.ValidationError(
{
Expand Down

0 comments on commit 2f4769f

Please sign in to comment.