Add notifications for Posts
This commit is contained in:
parent
d293c09e64
commit
e935911bd5
@ -1,4 +1,5 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.sites.shortcuts import get_current_site
|
from django.contrib.sites.shortcuts import get_current_site
|
||||||
from templated_email import send_templated_mail
|
from templated_email import send_templated_mail
|
||||||
|
|
||||||
@ -22,7 +23,7 @@ class CommentCreateForm(forms.ModelForm):
|
|||||||
'content_type': forms.HiddenInput(),
|
'content_type': forms.HiddenInput(),
|
||||||
'object_id': forms.HiddenInput(),
|
'object_id': forms.HiddenInput(),
|
||||||
'content': forms.Textarea(attrs={
|
'content': forms.Textarea(attrs={
|
||||||
'placeholder': "Add your comment here, supports markdown"
|
'placeholder': 'Add your comment here, supports markdown'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +33,7 @@ class CommentCreateForm(forms.ModelForm):
|
|||||||
post = Post.objects.get(pk=self.cleaned_data['object_id'])
|
post = Post.objects.get(pk=self.cleaned_data['object_id'])
|
||||||
url = get_current_site(request).domain + post.get_absolute_url()
|
url = get_current_site(request).domain + post.get_absolute_url()
|
||||||
recipients = list(post.recipients.all().values_list('email', flat=True))
|
recipients = list(post.recipients.all().values_list('email', flat=True))
|
||||||
if author in recipients:
|
if author.email in recipients:
|
||||||
recipients.remove(author.email)
|
recipients.remove(author.email)
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
@ -44,7 +45,7 @@ class CommentCreateForm(forms.ModelForm):
|
|||||||
|
|
||||||
send_templated_mail(
|
send_templated_mail(
|
||||||
template_name=NOTIFICATION_EMAIL_TEMPLATE,
|
template_name=NOTIFICATION_EMAIL_TEMPLATE,
|
||||||
from_email=author.email,
|
from_email=settings.DEFAULT_FROM_EMAIL,
|
||||||
recipient_list=recipients,
|
recipient_list=recipients,
|
||||||
context=context
|
context=context
|
||||||
)
|
)
|
||||||
@ -64,3 +65,39 @@ class PostForm(forms.ModelForm):
|
|||||||
widgets = {
|
widgets = {
|
||||||
'recipients': forms.CheckboxSelectMultiple()
|
'recipients': forms.CheckboxSelectMultiple()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class PostCreateForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = Post
|
||||||
|
fields = [
|
||||||
|
'title',
|
||||||
|
'content',
|
||||||
|
'recipients'
|
||||||
|
]
|
||||||
|
labels = {
|
||||||
|
'content': ''
|
||||||
|
}
|
||||||
|
widgets = {
|
||||||
|
'recipients': forms.CheckboxSelectMultiple()
|
||||||
|
}
|
||||||
|
|
||||||
|
def send_notification(self, request, post):
|
||||||
|
recipients = list(post.recipients.values_list('email', flat=True))
|
||||||
|
url = get_current_site(request).domain + post.get_absolute_url()
|
||||||
|
if author.email in recipients:
|
||||||
|
recipients.remove(author.email)
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'subject': post.title,
|
||||||
|
'author': post.author,
|
||||||
|
'message': post.content,
|
||||||
|
'url': url
|
||||||
|
}
|
||||||
|
|
||||||
|
send_templated_mail(
|
||||||
|
template_name=NOTIFICATION_EMAIL_TEMPLATE,
|
||||||
|
from_email=settings.DEFAULT_FROM_EMAIL,
|
||||||
|
recipient_list=recipients,
|
||||||
|
context=context
|
||||||
|
)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
from django.shortcuts import render, reverse, redirect, get_object_or_404
|
from django.shortcuts import render, reverse, redirect, get_object_or_404
|
||||||
from django.urls import reverse, reverse_lazy
|
from django.urls import reverse, reverse_lazy
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.views.generic.base import TemplateView
|
from django.views.generic.base import TemplateView
|
||||||
from django.views.generic.detail import DetailView
|
from django.views.generic.detail import DetailView
|
||||||
@ -13,7 +14,7 @@ from django.contrib.messages.views import SuccessMessageMixin
|
|||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
from .models import Topic, Post, Comment
|
from .models import Topic, Post, Comment
|
||||||
from .forms import PostForm, CommentCreateForm
|
from .forms import PostForm, PostCreateForm, CommentCreateForm
|
||||||
|
|
||||||
|
|
||||||
class CommentListView(LoginRequiredMixin, ListView):
|
class CommentListView(LoginRequiredMixin, ListView):
|
||||||
@ -105,7 +106,7 @@ class PostCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
|
|||||||
model = Post
|
model = Post
|
||||||
success_message = 'Post created.'
|
success_message = 'Post created.'
|
||||||
template_name_suffix = '_create_form'
|
template_name_suffix = '_create_form'
|
||||||
form_class = PostForm
|
form_class = PostCreateForm
|
||||||
|
|
||||||
def get_context_data(self, *args, **kwargs):
|
def get_context_data(self, *args, **kwargs):
|
||||||
context = super().get_context_data(*args, **kwargs)
|
context = super().get_context_data(*args, **kwargs)
|
||||||
@ -117,7 +118,9 @@ class PostCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
|
|||||||
form.instance.topic = get_object_or_404(
|
form.instance.topic = get_object_or_404(
|
||||||
Topic, pk=self.kwargs['topic_pk']
|
Topic, pk=self.kwargs['topic_pk']
|
||||||
)
|
)
|
||||||
return super().form_valid(form)
|
self.object = form.save()
|
||||||
|
form.send_notification(self.request, self.object)
|
||||||
|
return HttpResponseRedirect(self.get_success_url())
|
||||||
|
|
||||||
|
|
||||||
class PostDetailView(LoginRequiredMixin, DetailView):
|
class PostDetailView(LoginRequiredMixin, DetailView):
|
||||||
|
|||||||
@ -16,6 +16,7 @@ DATABASE_CONFIG = {
|
|||||||
SECRET_KEY = os.environ.get('SECRET_KEY', '')
|
SECRET_KEY = os.environ.get('SECRET_KEY', '')
|
||||||
|
|
||||||
STATIC_ROOT_PATH = os.environ.get('STATIC_ROOT_PATH', '/var/www/forum/static/')
|
STATIC_ROOT_PATH = os.environ.get('STATIC_ROOT_PATH', '/var/www/forum/static/')
|
||||||
|
LOGGING_FILE_LOCATION = os.environ.get('LOGGING_FILE_LOCATION', '/home/nathanchapman/debug.log')
|
||||||
|
|
||||||
ANYMAIL_CONFIG = {
|
ANYMAIL_CONFIG = {
|
||||||
'MAILGUN_API_KEY': os.environ.get('MAILGUN_API_KEY', ''),
|
'MAILGUN_API_KEY': os.environ.get('MAILGUN_API_KEY', ''),
|
||||||
|
|||||||
@ -150,7 +150,7 @@ LOGGING = {
|
|||||||
'level': 'DEBUG',
|
'level': 'DEBUG',
|
||||||
'class': 'logging.FileHandler',
|
'class': 'logging.FileHandler',
|
||||||
'filters': ['require_debug_false'],
|
'filters': ['require_debug_false'],
|
||||||
'filename': '/home/nathanchapman/debug.log',
|
'filename': LOGGING_FILE_LOCATION,
|
||||||
'formatter': 'verbose',
|
'formatter': 'verbose',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
{% block html %}
|
{% block html %}
|
||||||
<h3>{{ author }} posted:</h3>
|
<h3>{{ author }} posted:</h3>
|
||||||
|
|
||||||
{{ message|markdown|safe|truncatechars_html:64 }}
|
{{ message|markdown|safe }}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<a href="{{ url }}">View or reply</a>
|
<a href="{{ url }}">View or reply</a>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user