added post-office addon

This commit is contained in:
Esther Kleinhenz committed 2018-10-29 11:30:21 +01:00
1 parent 806718824c
commit 4046784e15
384 files changed
+11771 -56980

No files matched your search

-2
View File
@@ -1,5 +1,3 @@
from __future__ import absolute_import, unicode_literals
# This will make sure celery is always imported when
# Django starts so that shared_task will use this app.
from .celeryapp import app as celery_app
__all__ = ['celery_app']
+1 -44
View File
@@ -4,9 +4,6 @@ from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import Post, CustomUser
from .models import ScheduledReport, ReportRecipient, ScheduledReportGroup
from .forms import ScheduledReportForm
class CustomUserInline(admin.StackedInline):
model = CustomUser
@@ -21,44 +18,4 @@ class UserAdmin(BaseUserAdmin):
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(Post)
class ReportRecipientAdmin(admin.TabularInline):
model = ReportRecipient
class ScheduledReportAdmin(admin.ModelAdmin):
"""
List display for Scheduled reports in Django admin
"""
model = ScheduledReport
list_display = ('id', 'get_recipients')
inlines = [
ReportRecipientAdmin
]
form = ScheduledReportForm
def get_recipients(self, model):
recipients = model.reportrecep.all().values_list('email', flat=True)
if not recipients:
return 'No recipients added'
recipient_list = ''
for recipient in recipients:
recipient_list = recipient_list + recipient + ', '
return recipient_list[:-2]
get_recipients.short_description = 'Recipients'
get_recipients.allow_tags = True
class ScheduledReportGroupAdmin(admin.ModelAdmin):
"""
List display for ScheduledReportGroup Admin
"""
model = ScheduledReportGroup
list_display = ('get_scheduled_report_name','get_report_name')
def get_scheduled_report_name(self, model):
return model.scheduled_report.subject
def get_report_name(self, model):
return model.report.name
get_scheduled_report_name.short_description = "Scheduled Report Name"
get_report_name.short_description = "Report Name"
show_change_link = True
get_report_name.allow_tags = True
admin.site.register(ScheduledReport, ScheduledReportAdmin)
admin.site.register(ScheduledReportGroup, ScheduledReportGroupAdmin)
admin.site.register(Post)
-13
View File
@@ -1,13 +0,0 @@
from __future__ import absolute_import
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
from django.conf import settings
app = Celery('application')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
app.config_from_object('django.conf:settings')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
-57
View File
@@ -1,57 +0,0 @@
from datetime import datetime, timedelta
from django.core.mail import send_mail
from django.template import Template, Context
from django.http import HttpResponse
from django.conf import settings
from .models import ScheduledReport, ScheduledReportGroup, ReportRecipient
class ScheduledReportConfig(object):
def __init__(self, scheduled_report):
"""
Expects a scheduled report object and inititializes
its own scheduled_report attribute with it
"""
self.scheduled_report = scheduled_report
def get_report_config(self):
"""
Returns the configuration related to a scheduled report, needed
to populate the email
"""
return {
"template_context": self._get_related_reports_data(),
"recipients": self._get_report_recipients()
}
def _get_related_reports_data(self):
"""
Returns the list of reports data which needs to be sent out in a scheduled report
"""
pass
def _get_report_recipients(self):
"""
Returns the recipient list for a scheduled report
"""
pass
def create_email_data(content=None):
content = '''
''' + str(content) + ''''''
return content
def send_emails():
current_time = datetime.utcnow()
scheduled_reports = ScheduledReport.objects.filter(next_run_at__lt = current_time)
for scheduled_report in scheduled_reports:
report_config = ScheduledReportConfig(scheduled_report).get_report_config()
""" Specify the template path you want to send out in the email. """
template = Template(create_email_data('path/to/your/email_template.html'))
""" //Create your email html using Django's context processor """
report_template = template.render(Context(report_config['template_context']))
scheduled_report.save()
if not scheduled_report.subject:
""" Handle exception for subject not provided """
if not report_config['recipients']:
""" Handle exception for recipients not provided """
send_mail(
scheduled_report.subject, 'Here is the message.',
settings.EMAIL_HOST_USER, report_config['recipients'],
fail_silently=False, html_message=report_template
)
-25
View File
@@ -9,7 +9,6 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from datetime import datetime
from croniter import croniter
from django.forms import ModelForm, ValidationError
from .models import ScheduledReport
class PostForm(forms.ModelForm):
class Meta:
@@ -21,27 +20,3 @@ class NewTagForm(forms.ModelForm):
class Meta:
model = CustomUser
fields = ['m_tags']
class ScheduledReportForm(ModelForm):
class Meta:
model = ScheduledReport
fields = ['subject', 'cron_expression']
fields = ['subject', 'cron_expression']
help_texts = {'cron_expression': 'Scheduled time is considered in UTC'}
def clean(self):
cleaned_data = super(ScheduledReportForm, self).clean()
cron_expression = cleaned_data.get("cron_expression")
try:
iter = croniter(cron_expression, datetime.now())
except:
raise ValidationError("Incorrect cron expression:\
The information you must include is (in order of appearance):\
A number (or list of numbers, or range of numbers), m, representing the minute of the hour\
A number (or list of numbers, or range of numbers), h, representing the hour of the day\
A number (or list of numbers, or range of numbers), dom, representing the day of the month\
A number (or list, or range), or name (or list of names), mon, representing the month of the year\
A number (or list, or range), or name (or list of names), dow, representing the day of the week\
The asterisks (*) in our entry tell cron that for that unit of time, the job should be run every.\
Eg. */5 * * * * cron for executing every 5 mins")
return cleaned_data
-36
View File
@@ -27,39 +27,3 @@ class Post(models.Model):
def __str__(self):
return self.title
class Report(models.Model):
report_text = models.TextField()
class ScheduledReport(models.Model):
"""
Contains email subject and cron expression,to evaluate when the email has to be sent
"""
subject = models.CharField(max_length=200)
last_run_at = models.DateTimeField(null=True, blank=True)
next_run_at = models.DateTimeField(null=True, blank=True)
cron_expression = models.CharField(max_length=200)
def save(self, *args, **kwargs):
"""
function to evaluate "next_run_at" using the cron expression, so that it is updated once the report is sent.
"""
self.last_run_at = datetime.now()
iter = croniter(self.cron_expression, self.last_run_at)
self.next_run_at = iter.get_next(datetime)
super(ScheduledReport, self).save(*args, **kwargs)
def __unicode__(self):
return self.subject
class ScheduledReportGroup(models.Model):
"""
Many to many mapping between reports which will be sent out in a scheduled report
"""
report = models.ForeignKey(Report, related_name='report', on_delete=models.CASCADE)
scheduled_report = models.ForeignKey(ScheduledReport,
related_name='relatedscheduledreport', on_delete=models.CASCADE)
class ReportRecipient(models.Model):
"""
Stores all the recipients of the given scheduled report
"""
email = models.EmailField()
scheduled_report = models.ForeignKey(ScheduledReport, related_name='reportrecep', on_delete=models.CASCADE)
-7
View File
@@ -1,7 +0,0 @@
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from .email_service import send_emails
# this will run every minute, see http://celeryproject.org/docs/reference/celery.task.schedules.html#celery.task.schedules.crontab
@periodic_task(run_every=crontab(hour="*", minute="*", day_of_week="*"))
def trigger_emails():
send_emails()
+9
View File
@@ -14,6 +14,7 @@ import collections
from taggit_templatetags2.views import TagCanvasListView
from django.contrib.auth.models import User
from django.contrib import messages
from post_office import mail
@@ -192,3 +193,11 @@ def blog_search_list_view(request):
def tag_cloud(request):
return render(request, 'tag_cloud.html', {})
mail.send(
'esther.kleinhenz@web.de', # List of email addresses also accepted
'esther.kleinhenz@web.de',
subject='My email',
message='Hi there!',
html_message='Hi <strong>there</strong>!',
)