diff --git a/rowers/tasks.py b/rowers/tasks.py index b3fed727..d802a565 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -32,35 +32,6 @@ from django_rq import job from django.utils import timezone from django.utils.html import strip_tags -def textify(html): - # Remove html tags and continuous whitespaces - text_only = re.sub('[ \t]+', ' ', strip_tags(html)) - # Strip single spaces in the beginning of each line - return text_only.replace('\n ', '\n').strip() - -def send_template_email(from_email,to_email,subject,template,context,*args,**kwargs): - - htmly = get_template(template) - - html_content = htmly.render(context) - text_content = textify(html_content) - - msg = EmailMultiAlternatives(subject, text_content, from_email, to_email) - msg.attach_alternative(html_content, "text/html") - - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - else: - return 0 - - return res - from utils import deserialize_list from rowers.dataprepnodjango import ( @@ -90,6 +61,51 @@ import arrow from django.contrib.staticfiles import finders +def textify(html): + # Remove html tags and continuous whitespaces + text_only = re.sub('[ \t]+', ' ', strip_tags(html)) + # Strip single spaces in the beginning of each line + return text_only.replace('\n ', '\n').strip() + +def send_template_email(from_email,to_email,subject, + template,context, + *args,**kwargs): + + htmly = get_template(template) + + html_content = htmly.render(context) + text_content = textify(html_content) + + msg = EmailMultiAlternatives(subject, text_content, from_email, to_email) + msg.attach_alternative(html_content, "text/html") + + if 'attach_file' in kwargs: + fileobj = kwargs['attach_file'] + if os.path.isfile(fileobj): + msg.attach_file(fileobj) + else: + try: + fileobj2 = fileobj + with gzip.open(fileobj+'.gz','rb') as f_in, open(fileobj2,'wb') as f_out: + shutil.copyfileobj(f_in,f_out) + msg.attach_file(fileobj2) + os.remove(fileobj2) + except IOError: + pass + + if 'emailbounced' in kwargs: + emailbounced = kwargs['emailbounced'] + else: + emailbounced = False + + if not emailbounced: + res = msg.send() + else: + return 0 + + return res + + @app.task def add(x, y): return x + y @@ -357,8 +373,6 @@ def handle_sendemail_breakthrough(workoutid, useremail, subject = "A breakthrough workout on rowsandall.com" from_email = 'Rowsandall ' - htmly = get_template('breakthroughemail.html') - d = { 'first_name':userfirstname, 'siteurl':siteurl, @@ -366,23 +380,13 @@ def handle_sendemail_breakthrough(workoutid, useremail, 'btvalues':tablevalues, } - html_content = htmly.render(d) - text_content = textify(html_content) - - msg = EmailMultiAlternatives(subject, text_content, from_email, [useremail]) - msg.attach_alternative(html_content, "text/html") - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - if not emailbounced: - res = msg.send() + res = send_template_email(from_email,[useremail], + subject,'breakthroughemail.html', + d,**kwargs) - - # remove tcx file return 1 # send email when a breakthrough workout is uploaded @@ -403,12 +407,20 @@ def handle_sendemail_hard(workoutid, useremail, if debug: siteurl = SITE_URL_DEV + btvalues = pd.read_json(btvalues) + btvalues.sort_values('delta', axis=0, inplace=True) + + tablevalues = [ + {'delta': t.delta, + 'cpvalue': t.cpvalues, + 'pwr': t.pwr + } for t in btvalues.itertuples() + ] + # send email with attachment subject = "That was a pretty hard workout on rowsandall.com" from_email = 'Rowsandall ' - htmly = get_template('hardemail.html') - d = { 'first_name':userfirstname, 'siteurl':siteurl, @@ -416,24 +428,10 @@ def handle_sendemail_hard(workoutid, useremail, 'btvalues':tablevalues, } - html_content = htmly.render(d) - text_content = textify(html_content) - - msg = EmailMultiAlternatives(subject, text_content, from_email, [useremail]) - msg.attach_alternative(html_content, "text/html") + res = send_template_email(from_email,[useremail], + subject,'hardemail.html',d,**kwargs) - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - - - - # remove tcx file return 1 @@ -511,27 +509,15 @@ def handle_sendemail_unrecognizedowner(useremail, userfirstname, fullemail = useremail subject = "Unrecognized file from Rowsandall.com" - plaintext = get_template('csvemail.txt') - htmly = get_template('csvemail.html') + htmly = get_template('unrecognizedemail.html') - d = {'first_name':first_name} + d = {'first_name':userfirstname} from_email = 'Rowsandall ' - html_content = htmly.render(d) - text_content = textify(html_content) - - msg = EmailMultiAlternatives(subject, text_content, from_email, [fullemail]) - msg.attach_alternative(html_content, "text/html") - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - + res = send_template_email(from_email,[fullemail], + subject,'csvemail.html',d, + **kwargs) return 1 @@ -550,24 +536,11 @@ def handle_sendemailtcx(first_name, last_name, email, tcxfile,**kwargs): from_email = 'Rowsandall ' - html_content = htmly.render(d) - text_content = textify(html_content) - msg = EmailMultiAlternatives(subject, text_content, from_email, [fullemail]) - msg.attach_alternative(html_content, "text/html") + res = send_template_email(from_email,[fullemail], + subject,'tcxemail.html',d, + attach_file=tcxfile,**kwargs) - msg.attach_file(tcxfile) - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - - - # remove tcx file os.remove(tcxfile) return 1 @@ -612,31 +585,11 @@ def handle_sendemailsummary(first_name, last_name, email, csvfile, **kwargs): from_email = 'Rowsandall ' - html_content = htmly.render(d) - text_content = textify(html_content) - - msg = EmailMultiAlternatives(subject, text_content, from_email, [fullemail]) - msg.attach_alternative(html_content, "text/html") - - - if os.path.isfile(csvfile): - msg.attach_file(csvfile) - else: - csvfile2 = csvfile - with gzip.open(csvfile + '.gz', 'rb') as f_in, open(csvfile2, 'wb') as f_out: - shutil.copyfileobj(f_in, f_out) - - msg.attach_file(csvfile2) - os.remove(csvfile2) - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - + res = send_template_email(from_email,[fullemail], + subject,'summarymail.html',d, + attach_file=csvfile, + **kwargs) + try: os.remove(csvfile) except: @@ -654,37 +607,15 @@ def handle_sendemailcsv(first_name, last_name, email, csvfile,**kwargs): fullemail = first_name + " " + last_name + " " + "<" + email + ">" subject = "File from Rowsandall.com" - htmly = get_template('csvemail.html') - d = {'first_name':first_name} from_email = 'Rowsandall ' - html_content = htmly.render(d) - text_content = textify(html_content) - msg = EmailMultiAlternatives(subject, text_content, from_email, [fullemail]) - msg.attach_alternative(html_content, "text/html") - - if os.path.isfile(csvfile): - msg.attach_file(csvfile) - else: - csvfile2 = csvfile - with gzip.open(csvfile + '.gz', 'rb') as f_in, open(csvfile2, 'wb') as f_out: - shutil.copyfileobj(f_in, f_out) - - msg.attach_file(csvfile2) - os.remove(csvfile2) - - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - + res = send_template_email(from_email,[fullemail], + subject,'csvemail.html',d, + attach_file=csvfile,**kwargs) + return 1 @@ -819,22 +750,9 @@ def handle_otwsetpower(self,f1, boattype, weightvalue, 'workoutid':workoutid, } - html_content = htmly.render(d) - text_content = textify(html_content) - - msg = EmailMultiAlternatives(subject, text_content, from_email, [fullemail]) - msg.attach_alternative(html_content, "text/html") - - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - - + res = handle_template_email(from_email,[fullemail], + subject,'otwpoweremail.html',d, + **kwargs) return 1 @@ -1062,30 +980,20 @@ def handle_sendemail_invite(email, name, code, teamname, manager, if debug: siteurl = SITE_URL_DEV - htmly = get_template('teaminviteemail.html') - d = { 'name':name, - 'manage':manager, + 'manager':manager, 'code':code, 'teamname':teamname, + 'siteurl':siteurl } - html_content = htmly.render(d) - text_content = textify(html_content) + from_email = 'Rowsandall ' + + res = send_template_email(from_email,[fullemail], + subject,'teaminviteemail.html',d, + **kwargs) - msg = EmailMultiAlternatives(subject, text_content, from_email, [fullemail]) - msg.attach_alternative(html_content, "text/html") - - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = msg.send() - - return 1 @@ -1106,8 +1014,6 @@ def handle_sendemailnewresponse(first_name, last_name, siteurl = SITE_URL_DEV - htmly = get_template('teamresponseemail.html') - d = { 'first_name':first_name, 'commenter_first_name':commenter_first_name, @@ -1133,30 +1039,32 @@ def handle_sendemailnewcomment(first_name, comment, workoutname, workoutid, debug=False,**kwargs): + + + fullemail = first_name + ' ' + last_name + ' <' + email + '>' + from_email = 'Rowsandall ' subject = 'New comment on workout ' + workoutname - message = 'Dear ' + first_name + ',\n\n' - message += commenter_first_name + ' ' + commenter_last_name - message += ' has written a new comment on your workout ' - message += workoutname + '\n\n' - message += comment - message += '\n\n' - message += 'You can read the comment here:\n' - message += 'https://rowsandall.com/rowers/workout/' + \ - str(workoutid) + '/comment' - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + + d = { + 'first_name':first_name, + 'commenter_first_name':commenter_first_name, + 'commenter_last_name':commenter_last_name, + 'comment':comment, + 'workoutname':workoutname, + 'siteurl':siteurl, + 'workoutid':workoutid, + 'commentid':commentid + } - if not emailbounced: - res = email.send() - + res = send_template_email(from_email,[fullemail],subject, + 'teamresponseemail.html',d,**kwargs) + return 1 @@ -1166,29 +1074,23 @@ def handle_sendemail_request(email, name, code, teamname, requestor, id, debug=False,**kwargs): fullemail = name + ' <' + email + '>' subject = 'Request to join team ' + teamname - message = 'Dear ' + name + ',\n\n' - message += requestor + ' is requesting admission to your team ' + teamname - message += ' on rowsandall.com\n\n' - message += 'Click the direct link to accept: \n' - message += 'https://rowsandall.com/rowers/me/request/' + code + ' \n\n' - message += 'Click the following link to reject the request: \n' - message += 'https://rowsandall.com/rowers/me/request/' + str(id) + ' \n\n' - message += 'You can find all pending requests on your team management page:\n' - message += 'https://rowsandall.com/rowers/me/teams\n\n' - message += "Best Regards, the Rowsandall Team" + from_email = 'Rowsandall ' - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + d = { + 'requestor':requestor, + 'teamname':teamname, + 'siteurl':siteurl, + 'id':id, + 'first_name':name, + } + + res = send_template_email(from_email,[fullemail],subject, + 'teamrequestemail.html',d,**kwargs) - if not emailbounced: - res = email.send() - return 1 @@ -1198,25 +1100,20 @@ def handle_sendemail_request_accept(email, name, teamname, managername, debug=False,**kwargs): fullemail = name + ' <' + email + '>' subject = 'Welcome to ' + teamname - message = 'Dear ' + name + ',\n\n' - message += managername - message += ' has accepted your request to be part of the team ' - message += teamname - message += '\n\n' - message += "Best Regards, the Rowsandall Team" + from_email = 'Rowsandall ' - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + d = { + 'first_name':name, + 'managername':managername, + 'teamname':teamname, + } + res = send_template_email(from_email,[fullemail],subject, + 'teamwelcomeemail.html',d,**kwargs) - if not emailbounced: - res = email.send() - return 1 @@ -1226,26 +1123,19 @@ def handle_sendemail_request_reject(email, name, teamname, managername, debug=False,**kwargs): fullemail = name + ' <' + email + '>' subject = 'Your application to ' + teamname + ' was rejected' - message = 'Dear ' + name + ',\n\n' - message += 'Unfortunately, ' - message += managername - message += ' has rejected your request to be part of the team ' - message += teamname - message += '\n\n' - message += "Best Regards, the Rowsandall Team" + from_email = 'Rowsandall ' - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False - - if not emailbounced: - res = email.send() - + d = { + 'first_name':name, + 'managername':managername, + 'teamname':teamname, + } + res = send_template_email(from_email,[fullemail],subject, + 'teamrejectemail.html',d,**kwargs) return 1 @@ -1255,26 +1145,20 @@ def handle_sendemail_member_dropped(email, name, teamname, managername, debug=False,**kwargs): fullemail = name + ' <' + email + '>' subject = 'You were removed from ' + teamname - message = 'Dear ' + name + ',\n\n' - message += 'Unfortunately, ' - message += managername - message += ' has removed you from the team ' - message += teamname - message += '\n\n' - message += "Best Regards, the Rowsandall Team" + from_email = 'Rowsandall ' - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + d = { + 'first_name':name, + 'managername':managername, + 'teamname':teamname, + } + res = send_template_email(from_email,[fullemail],subject, + 'teamdropemail.html',d,**kwargs) - if not emailbounced: - res = email.send() - return 1 @@ -1282,29 +1166,23 @@ def handle_sendemail_member_dropped(email, name, teamname, managername, @app.task def handle_sendemail_team_removed(email, name, teamname, managername, debug=False,**kwargs): + fullemail = name + ' <' + email + '>' - subject = 'Team ' + teamname + ' was deleted' - message = 'Dear ' + name + ',\n\n' - message += managername - message += ' has decided to delete the team ' - message += teamname - message += '\n\n' - message += 'The ' + teamname + ' tag has been removed from all your ' - message += 'workouts on rowsandall.com.\n\n' - message += "Best Regards, the Rowsandall Team" + subject = 'You were removed from ' + teamname + from_email = 'Rowsandall ' - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + d = { + 'first_name':name, + 'managername':managername, + 'teamname':teamname, + } + res = send_template_email(from_email,[fullemail],subject, + 'teamremoveemail.html',d,**kwargs) - if not emailbounced: - res = email.send() - return 1 @@ -1314,26 +1192,20 @@ def handle_sendemail_invite_reject(email, name, teamname, managername, debug=False,**kwargs): fullemail = managername + ' <' + email + '>' subject = 'Your invitation to ' + name + ' was rejected' - message = 'Dear ' + managername + ',\n\n' - message += 'Unfortunately, ' - message += name - message += ' has rejected your invitation to be part of the team ' - message += teamname - message += '\n\n' - message += "Best Regards, the Rowsandall Team" - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + from_email = 'Rowsandall ' - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV - if not emailbounced: - res = email.send() - + d = { + 'first_name':name, + 'managername':managername, + 'teamname':teamname, + } + res = send_template_email(from_email,[fullemail],subject, + 'teaminviterejectemail.html',d,**kwargs) return 1 @@ -1343,22 +1215,21 @@ def handle_sendemail_invite_accept(email, name, teamname, managername, debug=False,**kwargs): fullemail = managername + ' <' + email + '>' subject = 'Your invitation to ' + name + ' was accepted' - message = 'Dear ' + managername + ',\n\n' - message += name + ' has accepted your invitation to be part of the team ' + teamname + '\n\n' - message += "Best Regards, the Rowsandall Team" - email = EmailMessage(subject, message, - 'Rowsandall ', - [fullemail]) + from_email = 'Rowsandall ' - if 'emailbounced' in kwargs: - emailbounced = kwargs['emailbounced'] - else: - emailbounced = False + siteurl = SITE_URL + if debug: + siteurl = SITE_URL_DEV + + d = { + 'first_name':name, + 'managername':managername, + 'teamname':teamname, + } + res = send_template_email(from_email,[fullemail],subject, + 'teaminviteacceptemail.html',d,**kwargs) - if not emailbounced: - res = email.send() - return 1 diff --git a/rowers/teams.py b/rowers/teams.py index ba802fd2..7ddbfcad 100644 --- a/rowers/teams.py +++ b/rowers/teams.py @@ -393,6 +393,7 @@ def send_request_email(rekwest): code = rekwest.code teamname = rekwest.team.name requestor = rekwest.user.first_name+' '+rekwest.user.last_name + print requestor,'aap' res = myqueue(queuehigh, handle_sendemail_request, diff --git a/rowers/templates/teamdropemail.html b/rowers/templates/teamdropemail.html new file mode 100644 index 00000000..d8255b15 --- /dev/null +++ b/rowers/templates/teamdropemail.html @@ -0,0 +1,16 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ first_name }},

+ +

+ Unfortunately, + {{ managername }} has removed you from + the team {{ teamname }}. +

+

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/templates/teaminviteacceptemail.html b/rowers/templates/teaminviteacceptemail.html new file mode 100644 index 00000000..86754ee1 --- /dev/null +++ b/rowers/templates/teaminviteacceptemail.html @@ -0,0 +1,16 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ managername }},

+ +

+ {{ first_name }} has accepted your invitation to be + part of the team {{ teamname }}. +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/templates/teaminviterejectemail.html b/rowers/templates/teaminviterejectemail.html new file mode 100644 index 00000000..30ab689a --- /dev/null +++ b/rowers/templates/teaminviterejectemail.html @@ -0,0 +1,17 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ managername }},

+ +

+ Unfortunately, + {{ first_name }} has rejected your invitation to be + part of the team {{ teamname }}. +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/templates/teamrejectemail.html b/rowers/templates/teamrejectemail.html new file mode 100644 index 00000000..34422898 --- /dev/null +++ b/rowers/templates/teamrejectemail.html @@ -0,0 +1,16 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ first_name }},

+ +

+ Unfortunately, + {{ managername }} has rejected your request to be part + of the team {{ teamname }}. +

+

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/templates/teamremoveemail.html b/rowers/templates/teamremoveemail.html new file mode 100644 index 00000000..64c92d62 --- /dev/null +++ b/rowers/templates/teamremoveemail.html @@ -0,0 +1,19 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ first_name }},

+ +

+ {{ managername }} has decided to delete the team {{ teamname }}. +

+

+ The {{ teamname }} tag has been removed from all your + workouts on rowsandall.com +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/templates/teamrequestemail.html b/rowers/templates/teamrequestemail.html new file mode 100644 index 00000000..dd436837 --- /dev/null +++ b/rowers/templates/teamrequestemail.html @@ -0,0 +1,29 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ first_name }},

+ +

+ {{ requestor }} is requesting access to your team {{ teamname }} + on rowsandall.com +

+

+ Click the direct link to accept: + + {{ siteurl }}/rowers/me/request/{{ code }} +

+

+ Click the following link to reject the request: + + {{ siteurl }}/rowers/me/request/{{ id }} +

+

+ You can find all pending requests on your team management page: + {{ siteurl }}/rowers/me/teams +

+

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/templates/teamresponseemail.html b/rowers/templates/teamresponseemail.html index 3bb04e19..d0def71a 100644 --- a/rowers/templates/teamresponseemail.html +++ b/rowers/templates/teamresponseemail.html @@ -7,7 +7,7 @@

{{ commenter_first_name }} {{ commenter_last_name }} has written - a new comment on your workout {{ workoutname }} + a new comment on workout {{ workoutname }}

{{ comment }} diff --git a/rowers/templates/teamwelcomeemail.html b/rowers/templates/teamwelcomeemail.html new file mode 100644 index 00000000..d255002c --- /dev/null +++ b/rowers/templates/teamwelcomeemail.html @@ -0,0 +1,15 @@ +{% extends "emailbase.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block body %} +

Dear {{ first_name }},

+ +

+ {{ managername }} has accepted your request to be part + of the team {{ teamname }}. +

+

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/temp.txt b/temp.txt deleted file mode 100644 index 5fdc2a13..00000000 --- a/temp.txt +++ /dev/null @@ -1,5 +0,0 @@ ---- -aap: noot -mies: jet -jet -... \ No newline at end of file