Log newsletter start/end date and uuid

This commit is contained in:
JonnyWong16 2018-03-17 23:58:39 -07:00
parent 960e147e10
commit c9618322c2
4 changed files with 76 additions and 40 deletions

View file

@ -532,7 +532,6 @@
url: 'send_newsletter', url: 'send_newsletter',
data: { data: {
newsletter_id: $('#newsletter_id').val(), newsletter_id: $('#newsletter_id').val(),
subject: $('#email_subject').val(),
notify_action: 'test' notify_action: 'test'
}, },
cache: false, cache: false,

View file

@ -637,7 +637,7 @@ def dbcheck():
c_db.execute( c_db.execute(
'CREATE TABLE IF NOT EXISTS newsletter_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, ' 'CREATE TABLE IF NOT EXISTS newsletter_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, '
'newsletter_id INTEGER, agent_id INTEGER, agent_name TEXT, notify_action TEXT, ' 'newsletter_id INTEGER, agent_id INTEGER, agent_name TEXT, notify_action TEXT, '
'subject_text TEXT, success INTEGER DEFAULT 0)' 'subject_text TEXT, start_date TEXT, end_date TEXT, uuid TEXT UNIQUE, success INTEGER DEFAULT 0)'
) )
# poster_urls table :: This table keeps record of the notification poster urls # poster_urls table :: This table keeps record of the notification poster urls

View file

@ -64,46 +64,45 @@ def notify(newsletter_id=None, notify_action=None, **kwargs):
if not newsletter_config: if not newsletter_config:
return return
if notify_action in ('test', 'api'):
subject_string = kwargs.pop('subject', 'Tautulli Newsletter')
else:
# Get the subject string
subject_string = newsletter_config['email_config']['subject']
newsletter_agent = newsletters.get_agent_class(agent_id=newsletter_config['agent_id'], newsletter_agent = newsletters.get_agent_class(agent_id=newsletter_config['agent_id'],
config=newsletter_config['config'], config=newsletter_config['config'],
email_config=newsletter_config['email_config']) email_config=newsletter_config['email_config'])
subject = newsletter_agent.format_subject(subject_string)
if notify_action in ('test', 'api'):
subject_string = kwargs.pop('subject', None)
if subject_string:
newsletter_agent.subject = newsletter_agent.format_subject(subject_string)
# Set the newsletter state in the db # Set the newsletter state in the db
newsletter_log_id = set_notify_state(newsletter=newsletter_config, newsletter_log_id = set_notify_state(newsletter=newsletter_config,
notify_action=notify_action, notify_action=notify_action,
subject=subject) subject=newsletter_agent.subject,
start_date=newsletter_agent.start_date.format('YYYY-MM-DD'),
end_date=newsletter_agent.end_date.format('YYYY-MM-DD'))
# Send the notification # Send the notification
success = newsletters.send_newsletter(newsletter_id=newsletter_config['id'], success = newsletter_agent.send()
subject=subject,
notify_action=notify_action,
newsletter_log_id=newsletter_log_id,
**kwargs)
if success: if success:
set_notify_success(newsletter_log_id) set_notify_success(newsletter_log_id)
return True return True
def set_notify_state(newsletter, notify_action, subject): def set_notify_state(newsletter, notify_action, subject, start_date, end_date):
if newsletter and notify_action: if newsletter and notify_action:
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
keys = {'timestamp': int(time.time()), keys = {'timestamp': int(time.time()),
'newsletter_id': newsletter['id'], 'uuid': get_newsletter_uuid()}
'agent_id': newsletter['agent_id'],
'notify_action': notify_action}
values = {'agent_name': newsletter['agent_name'], values = {'newsletter_id': newsletter['id'],
'subject_text': subject} 'agent_id': newsletter['agent_id'],
'agent_name': newsletter['agent_name'],
'notify_action': notify_action,
'subject_text': subject,
'start_date': start_date,
'end_date': end_date}
monitor_db.upsert(table_name='newsletter_log', key_dict=keys, value_dict=values) monitor_db.upsert(table_name='newsletter_log', key_dict=keys, value_dict=values)
return monitor_db.last_insert_id() return monitor_db.last_insert_id()
@ -117,3 +116,19 @@ def set_notify_success(newsletter_log_id):
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
monitor_db.upsert(table_name='newsletter_log', key_dict=keys, value_dict=values) monitor_db.upsert(table_name='newsletter_log', key_dict=keys, value_dict=values)
def get_newsletter_uuid():
uuid = ''
uuid_exists = 1
db = database.MonitorDatabase()
while not uuid or uuid_exists:
if uuid:
result = db.select_single(
'SELECT EXISTS(SELECT uuid FROM newsletter_log WHERE uuid = ?) as uuid_exists', [uuid])
uuid_exists = result['uuid_exists']
uuid = plexpy.generate_uuid()[:8]
return uuid

View file

@ -19,7 +19,6 @@ from itertools import groupby
from mako.lookup import TemplateLookup from mako.lookup import TemplateLookup
from mako import exceptions from mako import exceptions
import os import os
import time
import plexpy import plexpy
import common import common
@ -257,7 +256,7 @@ def serve_template(templatename, **kwargs):
class Newsletter(object): class Newsletter(object):
NAME = '' NAME = ''
_DEFAULT_CONFIG = {} _DEFAULT_CONFIG = {'last_days': 7}
_DEFAULT_EMAIL_CONFIG = EMAIL().return_default_config() _DEFAULT_EMAIL_CONFIG = EMAIL().return_default_config()
_DEFAULT_EMAIL_CONFIG['from_name'] = 'Tautulli Newsletter' _DEFAULT_EMAIL_CONFIG['from_name'] = 'Tautulli Newsletter'
_DEFAULT_EMAIL_CONFIG['notifier'] = 0 _DEFAULT_EMAIL_CONFIG['notifier'] = 0
@ -265,11 +264,44 @@ class Newsletter(object):
_TEMPLATE_MASTER = '' _TEMPLATE_MASTER = ''
_TEMPLATE = '' _TEMPLATE = ''
def __init__(self, config=None, email_config=None): def __init__(self, config=None, email_config=None, start_date=None, end_date=None):
self.config = self.set_config(config=config, default=self._DEFAULT_CONFIG) self.config = self.set_config(config=config, default=self._DEFAULT_CONFIG)
self.email_config = self.set_config(config=email_config, default=self._DEFAULT_EMAIL_CONFIG) self.email_config = self.set_config(config=email_config, default=self._DEFAULT_EMAIL_CONFIG)
self.parameters = {'server_name': plexpy.CONFIG.PMS_NAME} date_format = helpers.momentjs_to_arrow(plexpy.CONFIG.DATE_FORMAT)
self.start_date = None
self.end_date = None
if end_date:
try:
self.end_date = arrow.get(end_date, 'YYYY-MM-DD', tzinfo='local').ceil('day')
except ValueError:
pass
if self.end_date is None:
self.end_date = arrow.now().ceil('day')
if start_date:
try:
self.start_date = arrow.get(start_date, 'YYYY-MM-DD', tzinfo='local').floor('day')
except ValueError:
pass
if self.start_date is None:
self.start_date = self.end_date.shift(days=-self.config['last_days']+1).floor('day')
self.end_time = self.end_date.timestamp
self.start_time = self.start_date.timestamp
self.parameters = {
'start_date': self.start_date.format(date_format),
'end_date': self.end_date.format(date_format),
'server_name': plexpy.CONFIG.PMS_NAME
}
self.subject = self.format_subject(self.email_config['subject'])
self.is_preview = False self.is_preview = False
self.data = {} self.data = {}
@ -324,8 +356,7 @@ class Newsletter(object):
preview=self.is_preview preview=self.is_preview
) )
def send(self, subject='', **kwargs): def send(self):
subject = self.format_subject(subject or self.email_config['subject'])
newsletter = self.generate_newsletter() newsletter = self.generate_newsletter()
if not self._has_data(): if not self._has_data():
@ -335,18 +366,18 @@ class Newsletter(object):
if self.email_config['notifier']: if self.email_config['notifier']:
return send_notification( return send_notification(
notifier_id=self.email_config['notifier'], notifier_id=self.email_config['notifier'],
subject=subject, subject=self.subject,
body=newsletter body=newsletter
) )
else: else:
email = EMAIL(config=self.email_config) email = EMAIL(config=self.email_config)
return email.notify( return email.notify(
subject=subject, subject=self.subject,
body=newsletter body=newsletter
) )
def format_subject(self, subject): def format_subject(self, subject=None):
subject = subject or self._DEFAULT_EMAIL_CONFIG['subject'] subject = subject or self._DEFAULT_EMAIL_CONFIG['subject']
try: try:
@ -381,7 +412,7 @@ class RecentlyAdded(Newsletter):
_TEMPLATE_MASTER = 'recently_added_master.html' _TEMPLATE_MASTER = 'recently_added_master.html'
_TEMPLATE = 'recently_added.html' _TEMPLATE = 'recently_added.html'
def __init__(self, config=None, email_config=None): def __init__(self, config=None, email_config=None, start_date=None, end_date=None):
super(RecentlyAdded, self).__init__(config=config, email_config=email_config) super(RecentlyAdded, self).__init__(config=config, email_config=email_config)
if self.config['incl_libraries'] is None: if self.config['incl_libraries'] is None:
@ -391,15 +422,6 @@ class RecentlyAdded(Newsletter):
self._DEFAULT_EMAIL_CONFIG['subject'] = 'Recently Added to Plex ({server_name})! ({end_date})' self._DEFAULT_EMAIL_CONFIG['subject'] = 'Recently Added to Plex ({server_name})! ({end_date})'
date_format = helpers.momentjs_to_arrow(plexpy.CONFIG.DATE_FORMAT)
self.end_time = int(time.time())
self.start_time = self.end_time - self.config['last_days']*24*60*60
self.end_date = arrow.get(self.end_time).format(date_format)
self.start_date = arrow.get(self.start_time).format(date_format)
self.parameters['start_date'] = self.start_date
self.parameters['end_date'] = self.end_date
self.parameters['pms_identifier'] = plexpy.CONFIG.PMS_IDENTIFIER self.parameters['pms_identifier'] = plexpy.CONFIG.PMS_IDENTIFIER
self.parameters['pms_web_url'] = plexpy.CONFIG.PMS_WEB_URL self.parameters['pms_web_url'] = plexpy.CONFIG.PMS_WEB_URL