179 lines
6.4 KiB
Python
179 lines
6.4 KiB
Python
import itertools
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
|
|
from django.contrib import messages
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.db.models import Q
|
|
from django.http import HttpRequest
|
|
from django.http.response import HttpResponse as HttpResponse
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.urls import reverse_lazy
|
|
from django.views import generic
|
|
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.mixins import ActionMixin
|
|
from module_project.service import OneSignalService
|
|
from module_project.utils import JsonResponseUtil
|
|
|
|
from .forms import PushNotificationForm
|
|
from .models import PushNotification
|
|
|
|
|
|
# Create your views here.
|
|
class NotificationView(LoginRequiredMixin, generic.TemplateView):
|
|
page_name = iam_constant.RESOURCE_MANAGE_NOTIFICATION
|
|
resource = iam_constant.RESOURCE_MANAGE_NOTIFICATION
|
|
template_name = "module_notification/notification.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
return context
|
|
|
|
|
|
class NotificationListJsonView(BaseDatatableView):
|
|
model = PushNotification
|
|
columns = ["id", "title", "message", "active", "timestamp"]
|
|
order_columns = ["id", "title", "message", "active", "timestamp"]
|
|
|
|
def get_initial_queryset(self):
|
|
deleted_flag = self.request.GET.get("deleted_flag", None)
|
|
|
|
return self.model.objects.filter(deleted=deleted_flag)
|
|
|
|
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
|
|
|
|
|
|
class NotificationCreateOrUpdateView(LoginRequiredMixin, generic.View):
|
|
# Set the page_name and resource
|
|
page_name = iam_constant.RESOURCE_MANAGE_NOTIFICATION
|
|
resource = iam_constant.RESOURCE_MANAGE_NOTIFICATION
|
|
|
|
# Initialize the action as ACTION_CREATE (can change based on logic)
|
|
action = iam_constant.ACTION_CREATE # Default action
|
|
|
|
template_name = "module_notification/add_notification.html"
|
|
model = PushNotification
|
|
form_class = PushNotificationForm
|
|
success_url = reverse_lazy("module_notification:notification")
|
|
error_message = "An error occurred while saving the data."
|
|
|
|
# Determine the success message dynamically based on whether it's an update or create
|
|
def get_success_message(self):
|
|
self.success_message = (
|
|
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
|
|
)
|
|
return self.success_message
|
|
|
|
# Get the object (if exists) based on URL parameter 'pk
|
|
def get_object(self):
|
|
pk = self.kwargs.get("pk")
|
|
return get_object_or_404(self.model, pk=pk) if pk else None
|
|
|
|
# Add page_name and operation to the context
|
|
def get_context_data(self, **kwargs):
|
|
context = {
|
|
"page_name": self.page_name,
|
|
"operation": "Add" if not self.object else "Edit",
|
|
}
|
|
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()
|
|
|
|
# If an object is found, change action to ACTION_UPDATE
|
|
if self.object is not None:
|
|
self.action = iam_constant.ACTION_UPDATE
|
|
|
|
form = self.form_class(instance=self.object)
|
|
context = self.get_context_data(form=form)
|
|
return render(request, self.template_name, context=context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
print("Request data: ", request.POST)
|
|
self.object = self.get_object()
|
|
|
|
# If an object is found, change action to ACTION_UPDATE
|
|
if self.object is not None:
|
|
self.action = iam_constant.ACTION_UPDATE
|
|
|
|
form = self.form_class(request.POST, instance=self.object)
|
|
if not form.is_valid():
|
|
print(form.errors)
|
|
context = self.get_context_data(form=form)
|
|
return render(request, self.template_name, context=context)
|
|
|
|
form.save()
|
|
messages.success(self.request, self.get_success_message())
|
|
return redirect(self.success_url)
|
|
|
|
|
|
class NotificationActionView(ActionMixin):
|
|
model = PushNotification
|
|
|
|
|
|
class NotificationSendView(generic.View):
|
|
model = PushNotification
|
|
|
|
def get_image_url(self, obj, field_name, request):
|
|
image_field = getattr(obj, field_name)
|
|
if image_field:
|
|
return request.build_absolute_uri(image_field.url)
|
|
return ""
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
id = request.POST.get("id")
|
|
obj = self.model.objects.filter(pk=int(id)).first()
|
|
# Get the current date and subtract 15 days
|
|
fifteen_days_ago = datetime.now() - timedelta(days=3)
|
|
|
|
# Filter the IAmPrincipal objects based on the last_login field being greater than or equal to fifteen_days_ago
|
|
player_ids = list(
|
|
IAmPrincipal.objects.filter(
|
|
principal_type__name=PRINCIPAL_TYPE_USER,
|
|
last_login__gte=fifteen_days_ago,
|
|
).values_list("player_id", flat=True)
|
|
)
|
|
# 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(
|
|
headings=obj.title,
|
|
contents=obj.message,
|
|
# include_player_ids=["5643e132-5266-4dc2-9131-1b4a81f0cbd0"], # single player id
|
|
include_player_ids=player_ids,
|
|
)
|
|
except Exception as e:
|
|
error_response = {
|
|
"status": 400,
|
|
"message": constants.INTERNAL_SERVER_ERROR,
|
|
"errors": str(e),
|
|
}
|
|
return JsonResponseUtil.error(**error_response)
|
|
|
|
return JsonResponseUtil.success(message="success")
|