digitaleagenturnc/notificsys/views.py

121 lines
3.8 KiB
Python

from django.shortcuts import render
from django.http import HttpResponseRedirect,HttpResponse, JsonResponse
from .models import UserNotification
from django.views import generic
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def CheckNotifications(request):
if request.method == 'GET':
if request.GET['action'] == 'checknotifications':
#print("HERE WE ARE")
unknownnotification = UserNotification.objects.filter(touser__pk=request.user.pk, wassend=False).order_by('-created')[:5]
data = {};
i = 0
for notify in unknownnotification:
elelink = ""
if notify.notificationtype == "agencynews":
elelink = "news/news/" + str(notify.elementid) + "/single"
elif notify.notificationtype == "messagereceived":
elelink = "messages/sl/" + str(notify.elementid)
elif notify.notificationtype == "newstandard":
elelink = "standards/standard/" + str(notify.elementid) + "/single"
else:
elelink = "notifications/showallnotificaions/"
formatedDate = notify.created.strftime("%d.%m.20%y um %H:%M")
data.update({ i : {
"not_id" : notify.pk,
"type": notify.notificationtype,
"date" : formatedDate,
"text" : notify.notificationtext,
"elelink" : elelink
}})
i += 1
notify.save()
return JsonResponse({"unknownnotification" : data})
# Create your views here.
@login_required
def GetBasicNotifications(request):
if request.method == 'GET':
if request.GET['action'] == 'oldnotifications':
#print("HERE WE ARE")
oldnotifications = UserNotification.objects.filter(touser__pk=request.user.pk, wassend=True, wasviewed=False).order_by('-created')[:5]
data = {};
i = 0
for notify in oldnotifications:
elelink = ""
if notify.notificationtype == "agencynews":
elelink = "news/news/" + str(notify.elementid) + "/single"
elif notify.notificationtype == "messagereceived":
elelink = "messages/sl/" + str(notify.elementid)
elif notify.notificationtype == "newstandard":
elelink = "standards/standard/" + str(notify.elementid) + "/single"
else:
elelink = "notifications/showallnotificaions/"
formatedDate = notify.created.strftime("%d.%m.20%y um %H:%M")
data.update({ i : {
"not_id" : notify.pk,
"type": notify.notificationtype,
"date" : formatedDate,
"text" : notify.notificationtext,
"elelink" : elelink
}})
i += 1
notify.save()
return JsonResponse({"oldnotifications" : data})
@login_required
def ShowAllNotifications(request):
context ={}
context["usernotifications"] = UserNotification.objects.filter(touser__pk=request.user.pk).order_by('-created')
return render(request, "notificsys/allnotifications.html", context)
@login_required
def ChangeNotificationsToViewed(request):
if request.method == 'GET':
if request.GET['action'] == 'newnotificationsviewed':
oldnotifications = UserNotification.objects.filter(touser__pk=request.user.pk, wassend=False, wasviewed=False).order_by('-created')[:5]
data = {};
i = 0
for notify in oldnotifications:
formatedDate = notify.created.strftime("%d.%m.20%y um %H:%M")
data.update({ i : {
"not_id" : notify.pk,
"type": notify.notificationtype,
"date" : formatedDate,
"text" : notify.notificationtext
}})
i += 1
notify.wassend = True
notify.save()
return JsonResponse({})
@login_required
def delSingleNotification(request):
if request.method == 'GET':
if request.GET['action'] == 'delsingle':
todelnotification = UserNotification.objects.get(pk=request.GET['todelid'])
todelnotification.delete()
return JsonResponse({})
@login_required
def delAllNotification(request):
if request.method == 'GET':
if request.GET['action'] == 'delall':
UserNotification.objects.filter(touser__pk=request.user.pk).delete()
return JsonResponse({})