digitaleagenturnc/message/views.py

53 lines
2.0 KiB
Python

from django.shortcuts import render, redirect
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView, ListView, UpdateView, DetailView, DeleteView
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from .forms import MessageForm
from notificsys.models import UserNotification
from django.core.mail import send_mail
from django.template.loader import render_to_string
# ALLE STANDARDS EINER AGENTUR
@login_required
def mainmessageview(request):
if request.method == 'POST':
context = {
'active_link' : 'messages'
}
targetuser_id = request.POST["target_user"]
message = request.POST["message_content"]
targetuser = User.objects.get(pk=targetuser_id)
notificationtext = message + " Grüße, " + request.user.first_name + " " + request.user.last_name
if(targetuser.profile.user_messages_mail):
username = targetuser.first_name + " " + targetuser.last_name
msg_html = render_to_string('notificsys/notification_mail.html', {'username': username, 'notificationtext' : notificationtext})
send_mail(
'Agentur-Benachrichtigung',
'Hallo ' + targetuser.first_name + ' ' + targetuser.last_name + '! ' + notificationtext,
'support@digitale-agentur.com',
[targetuser.email],
html_message=msg_html,
fail_silently=False
)
if(targetuser.profile.user_messages_push):
newnotification = UserNotification(touser=targetuser, notificationtext='Hallo ' + targetuser.first_name + ' ' + targetuser.last_name + '! ' + notificationtext, notificationtype="messagereceived")
newnotification.save()
return render (request, 'message/message_send.html', context)
else:
context = {
'active_link' : 'messages',
'form' : MessageForm(request.user)
}
# Adding active_link
# Loading only user same agency
# Change context and return for template-data
# # Get all Users of the Same Agency as logged user
return render (request, 'message/message.html', context)