Added method for sending mail on creation ,updation of task and corresponding templates.

This commit is contained in:
Anupam Shakya 2012-09-15 20:43:33 +05:30
parent 0e3a3e9f69
commit 83029cf028
5 changed files with 307 additions and 4 deletions

View File

@ -16,14 +16,14 @@ from itertools import groupby, chain
from mimetypes import guess_type
from nereid import (request, abort, render_template, login_required, url_for,
redirect, flash, jsonify, current_app, render_email)
redirect, flash, jsonify, render_email)
from flask import send_file
from nereid.ctx import has_request_context
from nereid.signals import registration
from nereid.contrib.pagination import Pagination
from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool
from trytond.pyson import And, Not, Or, Bool, Equal, Eval
from trytond.pyson import Eval
from trytond.config import CONFIG
from trytond.tools import get_smtp_server
@ -370,6 +370,7 @@ class Project(ModelSQL, ModelView):
POST will create a new task
"""
nereid_user_obj = Pool().get('nereid.user')
project = self.get_project(project_id)
# Check if user is among the participants
self.can_write(project, request.nereid_user)
@ -381,14 +382,20 @@ class Project(ModelSQL, ModelView):
'type': 'task',
'comment': request.form.get('description', False),
})
email_receivers = [p.email for p in project.all_participants]
if request.form.get('assign_to', False):
assignee = nereid_user_obj.browse(
int(request.form.get('assign_to'))
)
self.write(task_id, {
'assigned_to': int(request.form.get('assign_to')),
'assigned_to': assignee.id,
'participants': [
('add', [int(request.form.get('assign_to'))])
('add', [assignee.id])
]
})
email_receivers = [assignee.email]
flash("Task successfully added to project %s" % project.name)
self.send_mail(task_id, email_receivers)
return redirect(
url_for('project.work.render_task',
project_id=project_id, task_id=task_id
@ -398,6 +405,36 @@ class Project(ModelSQL, ModelView):
flash("Could not create task. Try again.")
return redirect(request.referrer)
def send_mail(self, task_id, receivers=None):
"""Send mail when task created.
:param task_id: ID of task
:param receivers: Receivers of email.
"""
task = self.browse(task_id)
subject = "[%s] - %s" % (task.parent.name, task.name)
if not receivers:
receivers = [s.email for s in task.participants
if s.email]
message = render_email(
from_email=CONFIG['smtp_user'],
to='. '.join(receivers),
subject=subject,
text_template='project/emails/project_text_content.jinja',
html_template='project/emails/project_html_content.jinja',
task=task,
updated_by=request.nereid_user.name
)
#Send mail.
server = get_smtp_server()
server.sendmail(CONFIG['smtp_from'], receivers,
message.as_string())
server.quit()
@login_required
def unwatch(self, task_id):
"""
@ -834,6 +871,7 @@ class Project(ModelSQL, ModelView):
# update. This is to cover to cover cases where two users who
# havent refreshed the web page close the ticket
comment_id = history_obj.create(history_data)
history_obj.send_mail(comment_id)
else:
# Just comment, no update to task
comment_id = history_obj.create(history_data)
@ -1192,6 +1230,47 @@ class ProjectHistory(ModelSQL, ModelView):
})
return redirect(request.referrer)
def send_mail(self, history_id):
"""Send mail to all participants whenever there is any update on
project.
:param history_id: ID of history.
"""
history = self.browse(history_id)
# Get the previous updates than the latest one.
history_ids = self.search([
('id', '<', history_id),
('project.id', '=', history.project.id)
], order=[('create_date', 'DESC')])
last_history = self.browse(history_ids)
# Prepare the content of email.
subject = "[%s] %s" % (
history.project.parent.name, history.project.work.name,
)
receivers = [s.email for s in history.project.participants
if s.email]
message = render_email(
from_email=CONFIG['smtp_user'],
to='.'.join(receivers),
subject=subject,
text_template='project/emails/text_content.jinja',
html_template='project/emails/html_content.jinja',
history=history,
last_history=last_history
)
message.add_header('reply-to', request.nereid_user.email)
# Send mail.
server = get_smtp_server()
server.sendmail(CONFIG['smtp_from'], receivers,
message.as_string())
server.quit()
ProjectHistory()

View File

@ -0,0 +1,141 @@
<div class='email' style='font-size: 14px; color: rgb(50,50,50); font-family: Helvetica, Arial; margin: 20px 0'>
<div class='header'>
<div class='sub_header' >
<h1>
<a href="http://my.openlabs.co.in/en_US/" style='text-decoration:none; color:#a01717' ><img alt="Project Management System" /></a>
</h1>
</div>
<div style="width:100%; height:auto; padding:10px 0px 20px 0px ;border-bottom:solid 1px #C0C0C0">
<div style=" float:left; width:auto;">
<b>Project </b><a href="{{ url_for('project.work.render_project', project_id=history.project.parent.id, _external=True) }}"
style=' text-decoration:none; color:#2E9AFE'> {{ history.project.parent.name }} </a>
<b>Task </b><a href="{{ url_for('project.work.render_task', task_id=history.project.work.id, project_id=history.project.parent.id, _external=True) }}"
style=' text-decoration:none; color:#2E9AFE'> {{ history.project.work.name }} </a>
</div>
<div style=" float:right; width:auto;">
<span style="text-align:right">{{ history.date|dateformat }}</span>
</div>
</div>
{% if history.new_state == 'done' %}
<div class='box' style="background:#81F79E; padding:1px 10px 1px 10px; margin:10px 0px 0px 0px; -moz-border-radius: 10px;
border-radius: 10px; border-bottom:solid 1px #999999">
<p>
<span style='margin-right: 15px'>Task <b><a href="{{ url_for('project.work.render_task', task_id=history.project.work.id,
project_id=history.project.parent.id, _external=True) }}" style=' text-decoration:none; color:#2E9AFE'>
{{ history.project.name}} </a></b><span>
</p>
<p>
{% if history.new_assigned_to.name %}
<span style='margin-right: 15px'>Assigned to <b>{{ history.new_assigned_to.name }}</b><span>
{% endif %}
<span style='margin-right: 15px'>Status <b>{{ history.new_state }}</b></span>
<span style='margin-right: 15px'>Due On <b>{{ history.date|dateformat }}</b></span>
{% if history.comment %}
<p>
{{ history.comment }}
</p>
{% endif %}
</p>
</div>
{% elif history.new_state == 'opened' %}
<div class='box' style="background: #8FE7FA; padding:1px 10px 1px 10px; margin:10px 0px 0px 0px; -moz-border-radius: 10px;
border-radius: 10px; border-bottom:solid 1px #999999">
<p>
<span style='margin-right: 15px'>Task <b><a href="{{ url_for('project.work.render_task', task_id=history.project.work.id,
project_id=history.project.parent.id, _external=True) }}" style=' text-decoration:none; color:#2E9AFE'>
{{ history.project.name}}</a></b><span>
</p>
<p>
{% if history.new_assigned_to.name %}
<span style='margin-right: 15px'>Assigned to <b>{{ history.new_assigned_to.name }}</b><span>
{% endif %}
<span style='margin-right: 15px'>Status <b>{{ history.new_state }}</b></span>
<span style='margin-right: 15px'>Due On <b>{{ history.date|dateformat }}</b></span>
{% if history.comment %}
<p>
{{ history.comment }}
</p>
{% endif %}
</p>
</div>
{% else %}
<div class='box' style="background:#F2F5A9; padding:1px 10px 1px 10px; margin:10px 0px 0px 0px; -moz-border-radius: 10px;
border-radius: 10px; border-bottom:solid 1px #CCCC5A">
<p>
<span style='margin-right: 15px'>Task <b><a href="{{ url_for('project.work.render_task', task_id=history.project.work.id,
project_id=history.project.parent.id, _external=True) }}" style=' text-decoration:none; color:#2E9AFE'>
{{ history.project.name}}</a></b><span>
</p>
<p>
{% if history.new_assigned_to.name %}
<span style='margin-right: 15px'>Assigned to <b>{{ history.new_assigned_to.name }}</b><span>
{% endif %}
{% if history.new_state %}
<span style='margin-right: 15px'>Status <b>{{ history.new_state }}</b></span>
{% endif %}
<p>
{% if history.comment %}
{{ history.comment }}
{% endif %}
</p>
</p>
</div>
{% endif %}
{% for h_line in last_history%}
{% if loop.index == 1 %}
<div class='latest_comment' style="background:#D8D8D8 ; padding:0px 10px 1px 10px; margin:10px 0px 0px 0px; -moz-border-radius: 10px;
border-radius: 10px; border-bottom:dotted 1px #999999">
{% else %}
<div class='comment' style="background:#f5f5f5; padding:0px 10px 1px 10px; margin:10px 0px 0px 0px; -moz-border-radius: 10px;
border-radius: 10px; border-bottom:dotted 1px #999999">
{% endif %}
<div class='body' style=' padding: 5px; font-size: 12px'>
<p>
<span style='margin-right: 15px'><font color="#373435">{{ h_line.updated_by.name }}, </font><span>
<span style='color: rgb(150,150,150);'><font color="gray">{{ h_line.date|dateformat }}</font><span>
{% if h_line.new_assigned_to.party.name %}
<span style='margin-left: 10px; font-weight: bold; color: white; background-color: #333; padding: 1px 3px;'>
Assigned to {{ h_line.new_assigned_to.party.name }}
</span>
{% endif %}
<p>
{% if h_line.comment %}
{{ h_line.comment }}
{% endif %}
</p>
</p>
</div>
</div>
{% endfor %}
<div class="info" style="color:#666666; font-size:13px; font-weight:bold">
<p>You can <a href="{{ url_for('project.work.render_task', task_id=history.project.work.id, project_id=history.project.parent.id, _external=True) }}">
view this task online</a> to comment and attach files.
You can also reply to this email to post a reply in the conversation.</p>
</div>
<div class="more-info" style="color:#666666; font-size:11px; font-weight:normal; line-height:5px; padding:30px 0px 0px0px;">
{% for participant in history.project.participants %}
{% if participant.name %}
<b>{{ participant.name }}</b>
{% endif %}
{% endfor %} is/are subscribed to this task, and will be notified of future replies.
<p>Stop watching this thread to stop receiving email notifications. You can change your notification settings for further emails.</p>
</div>
</div>
</div>

View File

@ -0,0 +1,45 @@
<div class='email' style='font-size: 14px; color: rgb(50,50,50); font-family: Helvetica, Arial; margin: 20px 0'>
<div class='header'>
<h2>
<a href="http://my.openlabs.co.in/en_US/" style='text-decoration:none; color:#a01717' ><img alt="Project Management System" /></a>
</h2>
<div style="width:100%; height:auto; padding:10px 0px 20px 0px ;border-bottom:solid 1px #C0C0C0">
<div style=" float:left; width:auto;">
<b>Project </b><a href="{{ url_for('project.work.render_project', project_id=task.parent.id, _external=True) }}"
style=' text-decoration:none; color:#2E9AFE'> {{ task.parent.name }} </a>
<b>Task </b><a href="{{ url_for('project.work.render_task', task_id=task.id, project_id=task.parent.id, _external=True) }}"
style=' text-decoration:none; color:#2E9AFE'> {{ task.name }} </a>
</div>
<div style=" float:right; width:auto;">
<span style="text-align:right">{{ task.create_date|dateformat }}</span>
</div>
</div>
<p> A new task is created by <b>{{ updated_by }}</b> </p>
<div class='desc' style='background:#F2F5A9; padding:1px 10px 1px 10px; margin:10px 0px 0px 0px; -moz-border-radius: 10px;
border-radius: 10px; border-bottom:solid 1px #CCCC5A'>
<p style=" text-align:justify">
{{ task.comment }}
</p>
</div>
<div class="info" style="color:#666666; font-size:13px; font-weight:bold">
<p>You can <a href="{{ url_for('project.work.render_task', task_id=task.id, project_id=task.parent.id, _external=True) }}">
view this task online</a> to comment and attach files.
You can also reply to this email to post a reply in the conversation.</p>
</div>
<div class="more-info" style="color:#666666; font-size:11px; font-weight:normal; line-height:5px; padding:30px 0px 0px0px;">
{% for participant in task.participants %}
{% if participant.name %}
<b>{{ participant.name }}</b>
{% endif %}
{% endfor %} are subscribed to this task, and will be notified of future replies.
<p>Stop watching this thread to stop receiving email notifications. You can change your notification settings for further emails.</p>
</div>
</div>
</div>

View File

@ -0,0 +1,17 @@
Project: {{ task.parent.name }}
Task: {{ task.name }} {{ task.create_date.ctime() }}
A new task is created by {{ updated_by }}
{{ task.comment }}
<p>You can <a href="{{ url_for('project.work.render_task', task_id=task.id, project_id=task.parent.id, _external=True) }}">
view this task online</a> to comment and attach files.
You can also reply to this email to post a reply in the conversation.
</p>
{% for participant in task.participants %}
{% if participant.name %}
<b>{{ participant.name }}</b>
{% endif %}
{% endfor %} are subscribed to this task, and will be notified of future replies.
<p>Stop watching this thread to stop receiving email notifications. You can change your notification settings for further emails.</p>

View File

@ -0,0 +1,21 @@
Task: {{ history.project.name }}
{% if history.new_assigned_to.name %}
Assigned To: {{ history.new_assigned_to.name }}
{% endif %}
{% if history.new_state %}
Status: {{ history.new_state }}
{% endif %}
{{ history.updated_by.name }}, {{ history.date|dateformat }}
{{ history.comment }}
-----------------------------------------------------------------------------------------
{% for h_line in last_history %}
{{ h_line.updated_by.name }}
{{ h_line.date }}
{{ h_line.comment }}
---------------------------------------------------------------------------------------
{% endfor %}