deleted djcelery

This commit is contained in:
Esther Kleinhenz
2018-10-30 12:26:17 +01:00
parent 92fda4831a
commit 65d167a8b8
309 changed files with 43 additions and 60217 deletions
-4
View File
@@ -1,5 +1 @@
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 -41
View File
@@ -4,8 +4,7 @@ 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):
@@ -23,42 +22,3 @@ 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)
-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)
-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
@@ -0,0 +1,37 @@
# Generated by Django 2.1.2 on 2018-10-30 11:23
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('application', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='reportrecipient',
name='scheduled_report',
),
migrations.RemoveField(
model_name='scheduledreportgroup',
name='report',
),
migrations.RemoveField(
model_name='scheduledreportgroup',
name='scheduled_report',
),
migrations.DeleteModel(
name='Report',
),
migrations.DeleteModel(
name='ReportRecipient',
),
migrations.DeleteModel(
name='ScheduledReport',
),
migrations.DeleteModel(
name='ScheduledReportGroup',
),
]
+1 -37
View File
@@ -26,40 +26,4 @@ class Post(models.Model):
self.save()
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)
return self.title
-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()