Skip to content

Commit

Permalink
Send project progress email to contributors (hotosm#5262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aadesh-Baral authored Jul 27, 2022
1 parent 2938f95 commit 55526be
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 194 deletions.
3 changes: 3 additions & 0 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class EnvironmentConfig:
MAIL_DEFAULT_SENDER = os.getenv("TM_EMAIL_FROM_ADDRESS", None)
MAIL_DEBUG = True if LOG_LEVEL == "DEBUG" else False

# If disabled project update emails will not be sent.
SEND_PROJECT_EMAIL_UPDATES = os.getenv("TM_SEND_PROJECT_EMAIL_UPDATES", True)

# Languages offered by the Tasking Manager
# Please note that there must be exactly the same number of Codes as languages.
SUPPORTED_LANGUAGES = {
Expand Down
9 changes: 9 additions & 0 deletions backend/models/postgis/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class Project(db.Model):
extra_id_params = db.Column(db.String)
rapid_power_user = db.Column(db.Boolean, default=False)
last_updated = db.Column(db.DateTime, default=timestamp)
progress_email_sent = db.Column(db.Boolean, default=False)
license_id = db.Column(db.Integer, db.ForeignKey("licenses.id", name="fk_licenses"))
geometry = db.Column(Geometry("MULTIPOLYGON", srid=4326), nullable=False)
centroid = db.Column(Geometry("POINT", srid=4326), nullable=False)
Expand Down Expand Up @@ -1164,6 +1165,14 @@ def calculate_tasks_percent(
return int(tasks_validated / (total_tasks - tasks_bad_imagery) * 100)
elif target == "bad_imagery":
return int((tasks_bad_imagery / total_tasks) * 100)
elif target == "project_completion":
# To calculate project completion we assign 2 points to each task
# one for mapping and one for validation
return int(
(tasks_mapped + (tasks_validated * 2))
/ ((total_tasks - tasks_bad_imagery) * 2)
* 100
)
except ZeroDivisionError:
return 0

Expand Down
8 changes: 8 additions & 0 deletions backend/models/postgis/statuses.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,11 @@ class OrganisationType(Enum):
FREE = 1
DISCOUNTED = 2
FULL_FEE = 3


class EncouragingEmailType(Enum):
""" Describes the type of encouraging email sent to users """

PROJECT_PROGRESS = 1 # Send encouraging email to mappers when a project they have contributed to make progress
PROJECT_COMPLETE = 2 # Send encouraging email to mappers when a project they have contributed to is complete
BEEN_SOME_TIME = 3 # Send encouraging email to mappers who haven't been active for some time on the site
2 changes: 1 addition & 1 deletion backend/services/mapping_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def unlock_task_after_mapping(mapped_task: MappedTaskDTO) -> TaskDTO:
)

task.unlock_task(mapped_task.user_id, new_state, mapped_task.comment)

ProjectService.send_email_on_project_progress(mapped_task.project_id)
return task.as_dto_with_instructions(mapped_task.preferred_locale)

@staticmethod
Expand Down
63 changes: 62 additions & 1 deletion backend/services/messaging/smtp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from flask import current_app
from flask_mail import Message

from backend import mail
from backend import mail, create_app
from backend.models.postgis.message import Message as PostgisMessage
from backend.models.postgis.statuses import EncouragingEmailType
from backend.services.messaging.template_service import (
get_template,
format_username_link,
Expand Down Expand Up @@ -57,6 +59,65 @@ def send_contact_admin_email(data):
subject = "New contact from {name}".format(name=data.get("name"))
SMTPService._send_message(email_to, subject, message, message)

@staticmethod
def send_email_to_contributors_on_project_progress(
email_type: str,
project_id: int = None,
project_name: str = None,
project_completion: int = None,
):
""" Sends an encouraging email to a users when a project they have contributed to make progress"""
from backend.services.users.user_service import UserService

app = (
create_app()
) # Because message-all run on background thread it needs it's own app context
with app.app_context():
if email_type == EncouragingEmailType.PROJECT_PROGRESS.value:
subject = "The project you have contributed to has made progress."
elif email_type == EncouragingEmailType.PROJECT_COMPLETE.value:
subject = "The project you have contributed to has been completed."
values = {
"EMAIL_TYPE": email_type,
"PROJECT_ID": project_id,
"PROJECT_NAME": project_name,
"PROJECT_COMPLETION": project_completion,
}
contributor_ids = PostgisMessage.get_all_contributors(project_id)
for contributor_id in contributor_ids:
contributor = UserService.get_user_by_id(contributor_id[0])
values["USERNAME"] = contributor.username
if email_type == EncouragingEmailType.PROJECT_COMPLETE.value:
recommended_projects = UserService.get_recommended_projects(
contributor.username, "en"
).results
projects = []
for recommended_project in recommended_projects:
projects.append(
{
"org_logo": recommended_project.organisation_logo,
"priority": recommended_project.priority,
"name": recommended_project.name,
"id": recommended_project.project_id,
"description": recommended_project.short_description,
"total_contributors": recommended_project.total_contributors,
"difficulty": recommended_project.mapper_level,
"progress": recommended_project.percent_mapped,
"due_date": recommended_project.due_date,
}
)

values["PROJECTS"] = projects
html_template = get_template("encourage_mapper_en.html", values)
if (
contributor.email_address
and contributor.is_email_verified
and contributor.projects_notifications
):
SMTPService._send_message(
contributor.email_address, subject, html_template
)

@staticmethod
def send_email_alert(
to_address: str,
Expand Down
189 changes: 0 additions & 189 deletions backend/services/messaging/templates/been_some_time.html

This file was deleted.

Loading

0 comments on commit 55526be

Please sign in to comment.