123 lines
4.6 KiB
Python
123 lines
4.6 KiB
Python
import json
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.urls import reverse_lazy
|
|
from django.views import generic
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.contrib import messages
|
|
from django.core.serializers import serialize
|
|
from django.contrib.sites.shortcuts import get_current_site
|
|
from django.conf import settings
|
|
from accounts import resource_action
|
|
from goodtimes import constants
|
|
from manage_notifications.forms import PushNotificationForm
|
|
from manage_notifications.models import PushNotification
|
|
from manage_notifications.utils import (
|
|
get_eligible_principals_for_notification,
|
|
onesignal_send_notification,
|
|
send_notification,
|
|
)
|
|
|
|
# Create your views here.
|
|
|
|
|
|
class PushNotificationsCreateOrUpdateView(LoginRequiredMixin, generic.View):
|
|
# Set the page_name and resource
|
|
page_name = resource_action.RESOURCE_MANAGE_NOTIFICATIONS
|
|
resource = resource_action.RESOURCE_MANAGE_NOTIFICATIONS
|
|
|
|
# Initialize the action as ACTION_CREATE (can change based on logic)
|
|
action = resource_action.ACTION_CREATE # Default action
|
|
|
|
template_name = "manage_notifications/notification_add.html"
|
|
model = PushNotification
|
|
form_class = PushNotificationForm
|
|
success_url = reverse_lazy("manage_notifications:notification_list")
|
|
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 = resource_action.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 get_image_url(self, obj, field_name, request):
|
|
image_field = getattr(obj, field_name, None)
|
|
if image_field:
|
|
return request.build_absolute_uri(image_field.url)
|
|
return ""
|
|
|
|
def post(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 = resource_action.ACTION_UPDATE
|
|
|
|
form = self.form_class(request.POST, request.FILES, 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)
|
|
|
|
push_notification = form.save()
|
|
eligible_principals = get_eligible_principals_for_notification(
|
|
push_notification
|
|
)
|
|
print("eligible_principals: ", eligible_principals)
|
|
# Send notification
|
|
title = push_notification.title
|
|
message = push_notification.message
|
|
# image_url = self.get_image_url(push_notification, "banner_image", request)
|
|
# print(f"image url=========={image_url}")
|
|
send_notification(
|
|
title=title,
|
|
message=message,
|
|
# image_url=image_url,
|
|
eligible_principals=eligible_principals,
|
|
)
|
|
print(send_notification)
|
|
messages.success(request, self.get_success_message())
|
|
return redirect(self.success_url)
|
|
|
|
|
|
class PushNotificationView(LoginRequiredMixin, generic.ListView):
|
|
page_name = resource_action.RESOURCE_MANAGE_NOTIFICATIONS
|
|
resource = resource_action.RESOURCE_MANAGE_NOTIFICATIONS
|
|
action = resource_action.ACTION_READ
|
|
model = PushNotification
|
|
template_name = "manage_notifications/notification_list.html"
|
|
context_object_name = "notification_obj"
|
|
|
|
def get_queryset(self):
|
|
return super().get_queryset().filter(deleted=False)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
return context
|