2024-02-26 13:28:32 +05:30
|
|
|
import logging
|
2024-03-11 14:48:48 +05:30
|
|
|
from datetime import datetime
|
2024-03-21 13:09:14 +05:30
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
from django.contrib import messages
|
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
|
|
|
from django.contrib.auth.forms import PasswordResetForm
|
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2024-03-21 13:09:14 +05:30
|
|
|
from django.contrib.auth.views import (LoginView, LogoutView,
|
|
|
|
|
PasswordResetCompleteView,
|
|
|
|
|
PasswordResetConfirmView,
|
|
|
|
|
PasswordResetDoneView,
|
|
|
|
|
PasswordResetView)
|
|
|
|
|
from django.db.models import Prefetch, Q
|
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2024-02-26 13:28:32 +05:30
|
|
|
from django.urls import reverse_lazy
|
|
|
|
|
from django.views import generic
|
|
|
|
|
from django_datatables_view.base_datatable_view import BaseDatatableView
|
2024-03-21 13:09:14 +05:30
|
|
|
|
2024-03-29 00:45:21 +05:30
|
|
|
from module_activity.models import (Bowel, ChronicCondition, Intolerance, MealRecord, MealSymptomRecord, Medication,
|
2024-03-21 13:09:14 +05:30
|
|
|
PastTreatment, PrincipalHealthData,
|
|
|
|
|
Symptoms)
|
2024-04-02 19:23:52 +05:30
|
|
|
from module_iam import iam_constant, permission
|
2024-03-21 13:09:14 +05:30
|
|
|
from module_iam.models import IAmPrincipal, IAmPrincipalType
|
|
|
|
|
from module_project import constants
|
2024-03-11 14:48:48 +05:30
|
|
|
from module_project.mixins import ActionMixin
|
|
|
|
|
from module_project.utils import JsonResponseUtil
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-21 13:09:14 +05:30
|
|
|
from .forms import LoginForm, UserForm
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class AdminLoginView(generic.View):
|
|
|
|
|
template_name = "module_auth/login.html"
|
|
|
|
|
form_class = LoginForm
|
|
|
|
|
success_url = reverse_lazy("module_iam:dashboard")
|
|
|
|
|
error_url = reverse_lazy("module_auth:login")
|
|
|
|
|
success_message = constants.LOGIN_SUCCESS
|
|
|
|
|
error_message = constants.INVALID_EMAIL_PASSWORD
|
|
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
form = self.form_class()
|
|
|
|
|
|
|
|
|
|
return render(request, self.template_name, context={"form": form})
|
|
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
|
form = self.form_class(data=request.POST)
|
|
|
|
|
context = {"form": form}
|
|
|
|
|
if not form.is_valid():
|
|
|
|
|
messages.error(request, constants.INVALID_EMAIL_PASSWORD)
|
|
|
|
|
return render(request, self.template_name, context=context)
|
|
|
|
|
|
|
|
|
|
email = form.cleaned_data['email']
|
|
|
|
|
password = form.cleaned_data['password']
|
|
|
|
|
|
|
|
|
|
user = authenticate(request, email=email, password=password)
|
|
|
|
|
if user is None:
|
|
|
|
|
messages.error(request, constants.INVALID_EMAIL_PASSWORD)
|
|
|
|
|
return render(request, self.template_name, context=context)
|
|
|
|
|
|
|
|
|
|
login(request, user)
|
2024-03-29 00:45:21 +05:30
|
|
|
messages.success(request, constants.LOGIN_SUCCESS)
|
2024-02-26 13:28:32 +05:30
|
|
|
logging.info(f"User {user.email} logged in.")
|
|
|
|
|
|
|
|
|
|
return redirect(self.success_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AdminLogoutView(LogoutView):
|
|
|
|
|
next_page = reverse_lazy("module_auth:login")
|
|
|
|
|
|
|
|
|
|
class CustomPasswordResetView(PasswordResetView):
|
|
|
|
|
form_class = PasswordResetForm
|
|
|
|
|
template_name = "module_auth/password_reset_form.html"
|
|
|
|
|
email_template_name = "module_auth/password_reset_email_template.html"
|
|
|
|
|
success_url = reverse_lazy("module_auth:password_reset_done")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CustomPasswordResetDoneView(PasswordResetDoneView):
|
|
|
|
|
template_name = "module_auth/password_reset_done.html"
|
|
|
|
|
|
|
|
|
|
|
2024-04-02 19:23:52 +05:30
|
|
|
class UserDashView(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.TemplateView):
|
2024-03-11 14:48:48 +05:30
|
|
|
page_name = iam_constant.RESOURCE_MANAGE_USER
|
2024-04-02 19:23:52 +05:30
|
|
|
resource = iam_constant.RESOURCE_MANAGE_USER
|
2024-02-26 13:28:32 +05:30
|
|
|
action = None
|
|
|
|
|
template_name = "module_auth/users_list.html"
|
|
|
|
|
model = IAmPrincipal
|
|
|
|
|
context_objext_name = "obj"
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["page_name"] = self.page_name
|
|
|
|
|
return context
|
|
|
|
|
|
2024-04-02 19:23:52 +05:30
|
|
|
class UserCreateOrUpdateView(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.View):
|
2024-03-11 14:48:48 +05:30
|
|
|
page_name = iam_constant.RESOURCE_MANAGE_USER
|
2024-04-02 19:23:52 +05:30
|
|
|
resource = iam_constant.RESOURCE_MANAGE_USER
|
2024-03-11 14:48:48 +05:30
|
|
|
model = IAmPrincipal
|
|
|
|
|
form_class = UserForm
|
|
|
|
|
template_name = "module_auth/user_add.html"
|
|
|
|
|
success_url = reverse_lazy("module_auth:users")
|
|
|
|
|
success_message = "Saved Successfully"
|
|
|
|
|
error_message = "An error occurred while saving the data."
|
|
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
|
pk = self.kwargs.get("pk")
|
|
|
|
|
return get_object_or_404(self.model, pk=pk) if pk else None
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = {
|
|
|
|
|
"page_name": self.page_name,
|
|
|
|
|
"operation": "Edit" if self.object else "Add",
|
|
|
|
|
}
|
|
|
|
|
context.update(kwargs) # Include any additional context data passed to the view
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
form = self.form_class(instance=self.object)
|
|
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
|
return render(request, self.template_name, context=context)
|
|
|
|
|
|
|
|
|
|
# @transaction.atomic
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
print(request.POST)
|
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
form = self.form_class(request.POST, instance=self.object)
|
|
|
|
|
try:
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
principal = form.save(commit=False)
|
|
|
|
|
|
|
|
|
|
# Check if it's a new object (create action) or an existing one (update action)
|
|
|
|
|
if not principal.pk: # pk is None for new objects
|
|
|
|
|
principal.created_by = request.user
|
|
|
|
|
principal.principal_type = IAmPrincipalType.objects.filter(name=iam_constant.PRINCIPAL_TYPE_USER).first()
|
|
|
|
|
principal.modified_by = request.user
|
|
|
|
|
principal.modified_on = datetime.now()
|
|
|
|
|
|
|
|
|
|
# Save the object
|
|
|
|
|
principal.save()
|
|
|
|
|
|
|
|
|
|
messages.success(request, "Form submitted successfully")
|
|
|
|
|
return redirect(self.success_url)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.error_message = constants.ERROR_OCCURR.format(str(e))
|
|
|
|
|
print(self.error_message)
|
|
|
|
|
messages.error(request, self.error_message)
|
|
|
|
|
|
|
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
|
return render(request, template_name=self.template_name, context=context)
|
|
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
class UserListJson(BaseDatatableView):
|
|
|
|
|
model = IAmPrincipal
|
|
|
|
|
columns = ["id", "first_name", "email", "phone_no", "date_of_birth", "is_active"]
|
|
|
|
|
order_columns = ["id", "first_name", "email", "phone_no", "date_of_birth", "is_active"]
|
2024-04-01 11:31:16 +05:30
|
|
|
FILTER_ICONTAINS = "icontains"
|
|
|
|
|
|
|
|
|
|
def get_filter_method(self):
|
|
|
|
|
"""Returns preferred filter method"""
|
|
|
|
|
return self.FILTER_ICONTAINS
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
def get_initial_queryset(self):
|
|
|
|
|
deleted_flag = self.request.GET.get('deleted_flag', False)
|
|
|
|
|
return self.model.objects.filter(principal_type=IAmPrincipalType.get_principal_user(), deleted=deleted_flag)
|
|
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
def filter_queryset(self, qs):
|
|
|
|
|
print(f"request is {self.request.GET}")
|
|
|
|
|
search_value = self.request.GET.get("search[value]", None)
|
|
|
|
|
if search_value:
|
2024-03-26 13:22:29 +05:30
|
|
|
qs = super().filter_queryset(qs) # Call the built-in filtering first
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
for column in self.columns:
|
2024-03-29 00:45:21 +05:30
|
|
|
print(f" columen index pattern {self.request.GET.get(f'columns[{self.columns.index(column)+2}][search][value]', None)}")
|
2024-04-01 11:31:16 +05:30
|
|
|
search_value = self.request.GET.get(f'columns[{self.columns.index(column)+2}][search][value]', None)
|
2024-02-26 13:28:32 +05:30
|
|
|
if search_value:
|
2024-04-01 11:31:16 +05:30
|
|
|
column_data = self.request.GET.get(f'columns[{self.columns.index(column)+2}][data]')
|
2024-03-26 13:22:29 +05:30
|
|
|
if column_data == "is_active":
|
|
|
|
|
qs = qs.filter(**{f"{column}": search_value})
|
|
|
|
|
else:
|
|
|
|
|
qs = qs.filter(**{f"{column}__icontains": search_value})
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
return qs
|
|
|
|
|
|
2024-03-21 13:09:14 +05:30
|
|
|
def ordering(self, qs):
|
|
|
|
|
order = self.request.GET.get('order[0][dir]', None)
|
|
|
|
|
if order:
|
2024-03-29 00:45:21 +05:30
|
|
|
column_index = int(self.request.GET.get('order[0][column]', None)) - 2
|
2024-03-21 13:09:14 +05:30
|
|
|
order_column = self.order_columns[column_index]
|
|
|
|
|
|
|
|
|
|
if order == "asc":
|
|
|
|
|
qs = qs.order_by(order_column)
|
|
|
|
|
elif order == "desc":
|
|
|
|
|
qs = qs.order_by("-" + order_column)
|
|
|
|
|
|
|
|
|
|
return qs
|
|
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
class UserActionView(ActionMixin):
|
|
|
|
|
model = IAmPrincipal
|
|
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
|
|
action = request.POST.get('action') # 'archive', 'active', or 'unarchive'
|
|
|
|
|
ids = request.POST.getlist('ids[]') # List of 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, is_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(is_active=active.capitalize())
|
|
|
|
|
message = 'Record updated 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)
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-04-02 19:23:52 +05:30
|
|
|
class UserRecordView(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.View):
|
2024-03-11 14:48:48 +05:30
|
|
|
page_name = iam_constant.RESOURCE_MANAGE_USER
|
2024-04-02 19:23:52 +05:30
|
|
|
resource = iam_constant.RESOURCE_MANAGE_USER
|
2024-02-26 13:28:32 +05:30
|
|
|
action = None
|
|
|
|
|
model = IAmPrincipal
|
|
|
|
|
template_name = "module_auth/user_view.html"
|
|
|
|
|
|
2024-03-29 00:45:21 +05:30
|
|
|
def get_recent_meal(self, id):
|
|
|
|
|
recent_meal_records = MealRecord.objects.filter(
|
|
|
|
|
principal=id
|
|
|
|
|
).order_by('-id')[:5]
|
|
|
|
|
return recent_meal_records
|
|
|
|
|
|
|
|
|
|
def get_recent_medication(self, id):
|
|
|
|
|
recent_medication_records = Medication.objects.filter(
|
|
|
|
|
principal=id
|
|
|
|
|
).order_by('-id')[:5]
|
|
|
|
|
return recent_medication_records
|
|
|
|
|
|
|
|
|
|
def get_recent_bowel(self, id):
|
|
|
|
|
recent_bowel_records = Bowel.objects.filter(
|
|
|
|
|
principal=id
|
|
|
|
|
).order_by('-id')[:5]
|
|
|
|
|
return recent_bowel_records
|
|
|
|
|
|
|
|
|
|
def get_recent_meal_symptom(self, id):
|
|
|
|
|
recent_meal_symptom_records = MealSymptomRecord.objects.filter(
|
|
|
|
|
principal=id
|
|
|
|
|
).order_by('-id')[:5]
|
2024-04-08 00:33:02 +05:30
|
|
|
return recent_meal_symptom_records
|
2024-03-29 00:45:21 +05:30
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
def get(self, request, id):
|
|
|
|
|
# Retrieve the IAmPrincipal instance
|
|
|
|
|
principal_instance = get_object_or_404(IAmPrincipal, id=id)
|
|
|
|
|
|
|
|
|
|
# Prefetch related Intolerance objects for the principal
|
|
|
|
|
intolerance_prefetch = Prefetch(
|
|
|
|
|
"intolerance_principal",
|
|
|
|
|
queryset=Intolerance.objects.filter(principal=principal_instance),
|
|
|
|
|
to_attr="intolerance_data",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
symptom_prefetch = Prefetch(
|
|
|
|
|
"symptoms_principal",
|
|
|
|
|
queryset=Symptoms.objects.filter(principal=principal_instance),
|
|
|
|
|
to_attr="symptoms_data",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
pasttreatment_prefetch = Prefetch(
|
|
|
|
|
"pasttreatment_principal",
|
|
|
|
|
queryset=PastTreatment.objects.filter(principal=principal_instance),
|
|
|
|
|
to_attr="pasttreatment_data",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
chronic_prefetch = Prefetch(
|
|
|
|
|
"chronic_principal",
|
|
|
|
|
queryset=ChronicCondition.objects.filter(principal=principal_instance),
|
|
|
|
|
to_attr="chronic_data",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Now fetch the principal instance with prefetches
|
|
|
|
|
obj = IAmPrincipal.objects.prefetch_related(
|
|
|
|
|
intolerance_prefetch,
|
|
|
|
|
symptom_prefetch,
|
|
|
|
|
pasttreatment_prefetch,
|
|
|
|
|
chronic_prefetch
|
|
|
|
|
).get(id=id)
|
|
|
|
|
|
|
|
|
|
for data in obj.chronic_data:
|
|
|
|
|
print(f"data is {data.name, data.duration}")
|
|
|
|
|
|
2024-03-29 00:45:21 +05:30
|
|
|
context = {
|
|
|
|
|
'page_name': self.page_name,
|
|
|
|
|
'obj': obj,
|
|
|
|
|
"recent_meal": self.get_recent_meal(id),
|
|
|
|
|
"recent_medication": self.get_recent_medication(id),
|
|
|
|
|
"recent_bowel": self.get_recent_bowel(id),
|
|
|
|
|
"recent_meal_symptom": self.get_recent_meal_symptom(id),
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
# Render the template with the principal instance and related data
|
2024-03-29 00:45:21 +05:30
|
|
|
return render(request, self.template_name, context=context)
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
|
2024-04-02 19:23:52 +05:30
|
|
|
class UserArchiveList(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.TemplateView):
|
2024-03-11 14:48:48 +05:30
|
|
|
page_name = iam_constant.RESOURCE_MANAGE_USER
|
2024-04-02 19:23:52 +05:30
|
|
|
resource = iam_constant.RESOURCE_MANAGE_USER
|
2024-03-11 14:48:48 +05:30
|
|
|
action = None
|
|
|
|
|
template_name = "module_auth/users_archive_list.html"
|
|
|
|
|
model = IAmPrincipal
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["page_name"] = self.page_name
|
|
|
|
|
return context
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
class CustomPasswordResetConfirmView(PasswordResetConfirmView):
|
|
|
|
|
template_name = "module_auth/password_reset_confirm.html"
|
|
|
|
|
success_url = reverse_lazy("module_auth:password_reset_complete")
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
class CustomPasswordResetCompleteView(PasswordResetCompleteView):
|
|
|
|
|
template_name = "module_auth/password_reset_complete.html"
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
class UsersCountView(generic.View):
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
def get(self, request):
|
|
|
|
|
current_year = int(self.request.GET.get("year"))
|
|
|
|
|
user_counts = []
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
# Iterate over each month from January to December
|
|
|
|
|
for month in range(1, 13):
|
|
|
|
|
# Calculate the start and end dates for the current month
|
|
|
|
|
start_date = datetime(current_year, month, 1)
|
|
|
|
|
end_date = datetime(current_year, month + 1, 1) if month < 12 else datetime(current_year + 1, 1, 1)
|
|
|
|
|
# Query the User model to count users created within the current month
|
|
|
|
|
user_count = IAmPrincipal.objects.filter(date_joined__range=(start_date, end_date)).count()
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
# Append the count to the list
|
|
|
|
|
user_counts.append(user_count)
|
2024-02-26 13:28:32 +05:30
|
|
|
|
2024-03-11 14:48:48 +05:30
|
|
|
return JsonResponseUtil.success(message=constants.SUCCESS, data=user_counts)
|