* intial commit * changes * added validation * bux fixes * confirmed_unsubscribe code raw mysql query removed * moved everything to single file for optimisation * UI fixes * fixed csrf token not found * removed the older function(method) * code refracted or added some comment * changes * ui enhancement * codacy fixes * Ui fixes * Applied Requested changes * code optimisation * fixed unit test * more validation * Ui fixes * removed print statement which was used for tracing * change * done changes requested * done chnages requested * Make code more self-explanatory * Fix some code formatting - Add more descriptive name/id for buttons - Rearrange a comment - Remove some unwanted line breaks - change status values fixes #15961 issue
44 lines
No EOL
1.4 KiB
Python
44 lines
No EOL
1.4 KiB
Python
from __future__ import unicode_literals
|
|
import frappe
|
|
from frappe.utils.verified_command import verify_request
|
|
from frappe.email.doctype.newsletter.newsletter import confirmed_unsubscribe
|
|
|
|
no_cache = True
|
|
|
|
def get_context(context):
|
|
frappe.flags.ignore_permissions = True
|
|
# Called for confirmation.
|
|
if "email" in frappe.form_dict:
|
|
if verify_request():
|
|
user_email = frappe.form_dict["email"]
|
|
context.email = user_email
|
|
title = frappe.form_dict["name"]
|
|
context.email_groups = get_email_groups(user_email)
|
|
context.current_group = get_current_groups(title)
|
|
context.status = "waiting_for_confirmation"
|
|
|
|
# Called when form is submitted.
|
|
elif "user_email" in frappe.form_dict:
|
|
context.status = "unsubscribed"
|
|
email = frappe.form_dict['user_email']
|
|
email_group = get_email_groups(email)
|
|
for group in email_group:
|
|
if group.email_group in frappe.form_dict:
|
|
confirmed_unsubscribe(email, group.email_group)
|
|
|
|
# Called on Invalid or unsigned request.
|
|
else:
|
|
context.status = "invalid"
|
|
|
|
def get_email_groups(user_email):
|
|
# Return the all email_groups in which the email has been registered.
|
|
return frappe.get_all("Email Group Member",
|
|
fields=["email_group"],
|
|
filters={"email": user_email, "unsubscribed": 0})
|
|
|
|
|
|
def get_current_groups(name):
|
|
# Return current group by which the mail has been sent.
|
|
return frappe.db.get_all("Newsletter Email Group",
|
|
fields=["email_group"],
|
|
filters={"parent":name, "parenttype":"Newsletter"}) |