refactor datatables

This commit is contained in:
bobbyvish
2024-03-26 13:22:29 +05:30
parent 64c172ab59
commit 2a4f507a6f
22 changed files with 1191 additions and 391 deletions

View File

@@ -12,6 +12,7 @@ urlpatterns = [
path("notification/edit/<int:pk>", views.NotificationCreateOrUpdateView.as_view(), name="notification_edit"),
path("notification/list/", views.NotificationListJsonView.as_view(), name="notification_list"),
path("notification/action/", views.NotificationActionView.as_view(), name="notification_action"),
path("notification/archive/list/", views.NotificationArchiveView.as_view(), name="notification_archive"),
path("notification/send/", views.NotificationSendView.as_view(), name="notification_send"),
]

View File

@@ -15,7 +15,7 @@ from django_datatables_view.base_datatable_view import BaseDatatableView
from module_iam import iam_constant
from module_iam.iam_constant import PRINCIPAL_TYPE_USER
from module_iam.models import IAmPrincipal
from module_project import constants
from module_project import constants, date_utils
from module_project.mixins import ActionMixin
from module_project.service import OneSignalService
from module_project.utils import JsonResponseUtil
@@ -43,19 +43,36 @@ class NotificationListJsonView(BaseDatatableView):
def get_initial_queryset(self):
deleted_flag = self.request.GET.get("deleted_flag", None)
return self.model.objects.filter(deleted=deleted_flag)
# def render_column(self, row, column):
# if column == "timestamp":
# return date_utils.format_date_to_string(row.timestamp)
# return super().render_column(row, column)
def filter_queryset(self, qs):
# Implement your custom filtering logic here
print(f"request is {self.request.GET}")
search_value = self.request.GET.get("search[value]", None)
if search_value:
qs = qs.filter(
Q(id__icontains=search_value)
| Q(question__icontains=search_value)
| Q(answer__icontains=search_value)
)
# def filter_queryset(self, qs):
# # Implement your custom filtering logic here
# print(f"request is {self.request.GET}")
# search_value = self.request.GET.get("search[value]", None)
# if search_value:
# qs = qs.filter(
# Q(id__icontains=search_value)
# | Q(question__icontains=search_value)
# | Q(answer__icontains=search_value)
# )
# return qs
def ordering(self, qs):
order = self.request.GET.get('order[0][dir]', None)
if order:
column_index = int(self.request.GET.get('order[0][column]', None)) - 1
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
@@ -128,6 +145,17 @@ class NotificationCreateOrUpdateView(LoginRequiredMixin, generic.View):
class NotificationActionView(ActionMixin):
model = PushNotification
class NotificationArchiveView(LoginRequiredMixin, generic.TemplateView):
page_name = iam_constant.RESOURCE_MANAGE_NOTIFICATION
resource = None
action = None
template_name = "module_notification/notification_archive.html"
model = PushNotification
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class NotificationSendView(generic.View):
model = PushNotification
@@ -141,6 +169,17 @@ class NotificationSendView(generic.View):
def post(self, request, *args, **kwargs):
id = request.POST.get("id")
obj = self.model.objects.filter(pk=int(id)).first()
if not obj:
return JsonResponseUtil.error(
message="No notification with such ID exists."
)
if not obj.active:
return JsonResponseUtil.error(
message="The notification cannot be sent because it is inactive. Please activate the notification before attempting to send it."
)
# Get the current date and subtract 15 days
fifteen_days_ago = datetime.now() - timedelta(days=3)
@@ -154,11 +193,6 @@ class NotificationSendView(generic.View):
# removing none from list
player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids))
if not obj:
return JsonResponseUtil.error(
message="No notification with such ID exists."
)
try:
notification = OneSignalService()
response = notification.send_notification(