247 lines
8.6 KiB
Python
247 lines
8.6 KiB
Python
|
|
import logging
|
||
|
|
|
||
|
|
from datetime import datetime
|
||
|
|
from django.shortcuts import get_object_or_404, render, redirect
|
||
|
|
from django.contrib import messages
|
||
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||
|
|
from django.urls import reverse_lazy
|
||
|
|
from django.views import generic
|
||
|
|
from django.db.models import Q, Prefetch
|
||
|
|
from .models import Intolerance, Symptoms, ChronicCondition, PastTreatment, MealRecord, Bowel, MealSymptomRecord, Medication
|
||
|
|
from django_datatables_view.base_datatable_view import BaseDatatableView
|
||
|
|
from module_iam.models import IAmPrincipal
|
||
|
|
from module_project import constants
|
||
|
|
from module_project.utils import JsonResponseUtil
|
||
|
|
from django.http import JsonResponse
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
class BaseView(generic.TemplateView):
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
action = None
|
||
|
|
template_name = None
|
||
|
|
model = Intolerance
|
||
|
|
context_objext_name = "obj"
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context["page_name"] = self.page_name
|
||
|
|
context["principal_id"] = self.kwargs.get('principal_id')
|
||
|
|
return context
|
||
|
|
|
||
|
|
class BaseListJson(BaseDatatableView):
|
||
|
|
model = Intolerance
|
||
|
|
columns = ["id", "name", "duration", "active", "deleted"]
|
||
|
|
order_columns = ["id", "name", "duration", "active", "deleted"]
|
||
|
|
|
||
|
|
def get_initial_queryset(self):
|
||
|
|
principal_id = self.kwargs.get('principal_id')
|
||
|
|
deleted_flag = self.request.GET.get('deleted_flag', None)
|
||
|
|
|
||
|
|
if deleted_flag == 'true':
|
||
|
|
# Show only deleted records
|
||
|
|
return self.model.objects.filter(principal=principal_id, deleted=True)
|
||
|
|
else:
|
||
|
|
# Show all records except deleted ones
|
||
|
|
return self.model.objects.filter(principal=principal_id, deleted=False)
|
||
|
|
|
||
|
|
def filter_queryset(self, qs):
|
||
|
|
search_value = self.request.GET.get("search[value]", None)
|
||
|
|
if search_value:
|
||
|
|
qs = qs.filter(
|
||
|
|
Q(name__icontains=search_value) |
|
||
|
|
Q(duration__icontains=search_value)
|
||
|
|
)
|
||
|
|
return qs
|
||
|
|
|
||
|
|
|
||
|
|
class BaseActionView(generic.View):
|
||
|
|
model = Intolerance
|
||
|
|
|
||
|
|
def post(self, request, *args, **kwargs):
|
||
|
|
action = request.POST.get('action') # 'archive', 'active', or 'unarchive'
|
||
|
|
ids = request.POST.getlist('ids[]') # List of user IDs to perform action on
|
||
|
|
active = request.POST.get('active')
|
||
|
|
print(f"arhive action {action} and id is {ids} and active data is {active}")
|
||
|
|
if action == 'archive':
|
||
|
|
# Update 'deleted' field to True for the selected users
|
||
|
|
self.model.objects.filter(id__in=ids).update(deleted=True, active=False)
|
||
|
|
message = 'Record archived successfully.'
|
||
|
|
elif action == 'active':
|
||
|
|
# Update 'active' field to True for the selected users
|
||
|
|
self.model.objects.filter(id__in=ids).update(active=active.capitalize())
|
||
|
|
message = 'Record activated successfully.'
|
||
|
|
elif action == 'unarchive':
|
||
|
|
# Update 'deleted' field to False for the selected users
|
||
|
|
self.model.objects.filter(id__in=ids).update(deleted=False)
|
||
|
|
message = 'Record unarchived successfully.'
|
||
|
|
else:
|
||
|
|
return JsonResponseUtil.error(message="Invalid Action")
|
||
|
|
|
||
|
|
return JsonResponseUtil.success(message=message)
|
||
|
|
|
||
|
|
|
||
|
|
class IntoleranceView(BaseView):
|
||
|
|
model = Intolerance
|
||
|
|
template_name = "module_activity/intolerance_list.html"
|
||
|
|
|
||
|
|
class IntoleranceListJson(BaseListJson):
|
||
|
|
model = Intolerance
|
||
|
|
|
||
|
|
class IntoleranceActionView(BaseActionView):
|
||
|
|
model = Intolerance
|
||
|
|
|
||
|
|
class SymptomsView(BaseView):
|
||
|
|
model = Symptoms
|
||
|
|
template_name = "module_activity/symptoms_list.html"
|
||
|
|
|
||
|
|
class SymptomsListJson(BaseListJson):
|
||
|
|
model = Symptoms
|
||
|
|
|
||
|
|
class SymptomsActionView(BaseActionView):
|
||
|
|
model = Symptoms
|
||
|
|
|
||
|
|
class PastTreatmentView(BaseView):
|
||
|
|
model = PastTreatment
|
||
|
|
template_name = "module_activity/past_treatment_list.html"
|
||
|
|
|
||
|
|
class PastTreatmentListJson(BaseListJson):
|
||
|
|
model = PastTreatment
|
||
|
|
|
||
|
|
class PastTreatmentActionView(BaseActionView):
|
||
|
|
model = PastTreatment
|
||
|
|
|
||
|
|
class ChronicConditionView(BaseView):
|
||
|
|
model = ChronicCondition
|
||
|
|
template_name = "module_activity/chronic_conditon_list.html"
|
||
|
|
|
||
|
|
class ChronicConditionListJson(BaseListJson):
|
||
|
|
model = ChronicCondition
|
||
|
|
|
||
|
|
class ChronicConditionActionView(BaseActionView):
|
||
|
|
model = ChronicCondition
|
||
|
|
|
||
|
|
class UserActivityRecordView(generic.View):
|
||
|
|
def serialize_record(self, record):
|
||
|
|
time_obj = datetime.strptime(str(record.time), '%H:%M:%S')
|
||
|
|
return {
|
||
|
|
"id": record.id,
|
||
|
|
"date": record.date,
|
||
|
|
"time": time_obj.strftime('%I:%M %p'),
|
||
|
|
}
|
||
|
|
|
||
|
|
def get(self, request, *args, **kwargs):
|
||
|
|
try:
|
||
|
|
principal_id = self.kwargs.get('principal_id')
|
||
|
|
date = request.GET.get("date")
|
||
|
|
print(f"principal_id is {principal_id} data is {date} and type is {type(date)}")
|
||
|
|
if not date:
|
||
|
|
return JsonResponseUtil.error(message="Date parameter is missing")
|
||
|
|
|
||
|
|
try:
|
||
|
|
date_obj = datetime.strptime(date, "%Y-%m-%d").date()
|
||
|
|
except ValueError:
|
||
|
|
return JsonResponseUtil.error(message="Invalid date format")
|
||
|
|
|
||
|
|
# Retrieve data from different models
|
||
|
|
meal_records = MealRecord.objects.filter(principal=principal_id, date=date_obj)
|
||
|
|
medication_records = Medication.objects.filter(principal=principal_id, date=date_obj)
|
||
|
|
bowel_records = Bowel.objects.filter(principal=principal_id, date=date_obj)
|
||
|
|
meal_symptom_records = MealSymptomRecord.objects.filter(principal=principal_id, date=date_obj)
|
||
|
|
print(f"==================meal record {meal_records}")
|
||
|
|
# Prepare combined results
|
||
|
|
data = []
|
||
|
|
for record in meal_records:
|
||
|
|
data.append({"type": "Meal", **self.serialize_record(record)})
|
||
|
|
|
||
|
|
for record in medication_records:
|
||
|
|
data.append({"type": "Medication", **self.serialize_record(record)})
|
||
|
|
|
||
|
|
for record in bowel_records:
|
||
|
|
data.append({"type": "Bowel", **self.serialize_record(record)})
|
||
|
|
|
||
|
|
for record in meal_symptom_records:
|
||
|
|
data.append({"type": "Symptom", **self.serialize_record(record)})
|
||
|
|
|
||
|
|
all_records_sorted = sorted(data, key=lambda x: x["time"], reverse=True)
|
||
|
|
|
||
|
|
response_data = {
|
||
|
|
"recordsTotal": len(all_records_sorted),
|
||
|
|
"recordsFiltered": len(all_records_sorted),
|
||
|
|
"data": all_records_sorted,
|
||
|
|
}
|
||
|
|
|
||
|
|
return JsonResponse(response_data)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
return JsonResponseUtil.error(message="Something went wrong", errors=str(e))
|
||
|
|
|
||
|
|
class MealDetialView(generic.TemplateView):
|
||
|
|
template_name = "module_activity/meal_detail.html"
|
||
|
|
model = MealRecord
|
||
|
|
|
||
|
|
def get_record(self):
|
||
|
|
id = self.kwargs.get('pk')
|
||
|
|
meal_record = get_object_or_404(
|
||
|
|
self.model.objects.prefetch_related(
|
||
|
|
'food_records', 'beverage_records', 'food_ingredient_records'
|
||
|
|
),
|
||
|
|
id=id
|
||
|
|
)
|
||
|
|
return meal_record
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context['obj'] = self.get_record()
|
||
|
|
return context
|
||
|
|
|
||
|
|
|
||
|
|
class MedicationDetailView(generic.TemplateView):
|
||
|
|
template_name = "module_activity/medication_detail.html"
|
||
|
|
model = Medication
|
||
|
|
|
||
|
|
def get_record(self):
|
||
|
|
id = self.kwargs.get('pk')
|
||
|
|
obj = get_object_or_404(
|
||
|
|
self.model.objects.prefetch_related('medicines'),
|
||
|
|
id=id
|
||
|
|
)
|
||
|
|
return obj
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context['obj'] = self.get_record()
|
||
|
|
return context
|
||
|
|
|
||
|
|
|
||
|
|
class BowelDetailView(generic.TemplateView):
|
||
|
|
template_name = "module_activity/bowel_detail.html"
|
||
|
|
model = Bowel
|
||
|
|
|
||
|
|
def get_record(self):
|
||
|
|
id = self.kwargs.get('pk')
|
||
|
|
obj = get_object_or_404(self.model, id=id)
|
||
|
|
return obj
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context['obj'] = self.get_record()
|
||
|
|
return context
|
||
|
|
|
||
|
|
class MealSymptomDetailView(generic.TemplateView):
|
||
|
|
template_name = "module_activity/meal_symptom_details.html"
|
||
|
|
model = MealSymptomRecord
|
||
|
|
|
||
|
|
def get_record(self):
|
||
|
|
pk = self.kwargs.get('pk')
|
||
|
|
obj = get_object_or_404(
|
||
|
|
MealSymptomRecord.objects.prefetch_related('symptoms_before_meal', 'symptoms_after_meal'),
|
||
|
|
id=pk
|
||
|
|
)
|
||
|
|
return obj
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context['obj'] = self.get_record()
|
||
|
|
return context
|