def confirm_registration(email): from frappe.utils.verified_command import verify_request if not verify_request(): return # Default user message message = frappe._dict({ "title": _("registration:dialog:title:registration_subscription"), "html": _('registration:dialog:body:error_confirming_your_email.').format( email), "primary_label": _("dialog:body:go_to_homepage"), }) fields = ["name", "email_confirmed"] registration = frappe.get_value("Web Registration", {"email": email}, fields, as_dict=True) if registration.name and not registration.email_confirmed: doc = frappe.get_doc("Web Registration", registration.name) doc.email_confirmed = 1 doc.save(ignore_permissions=True) frappe.db.commit() message.html = _( 'registration:dialog:body:email_"{0}"_confirmed_ok').format(email) elif registration.name and registration.email_confirmed: message.html = _( 'registration:dialog:body:email_"{0}"_confirmed_previously' ).format(email) frappe.respond_as_web_page(**message)
def unsubscribe(email, name): if not verify_request(): return primary_action = frappe.utils.get_url() + "/api/method/frappe.email.doctype.newsletter.newsletter.confirmed_unsubscribe"+\ "?" + get_signed_params({"email": email, "name":name}) return_confirmation_page(email, name, primary_action)
def confirm_deletion(email, name, host_name): if not verify_request(): return doc = frappe.get_doc("Personal Data Deletion Request", name) host_name = frappe.local.site if doc.status == "Pending Verification": doc.status = "Pending Approval" doc.save(ignore_permissions=True) doc.notify_system_managers() frappe.db.commit() frappe.respond_as_web_page( _("Confirmed"), _( "The process for deletion of {0} data associated with {1} has been initiated." ).format(host_name, email), indicator_color="green", ) else: frappe.respond_as_web_page( _("Link Expired"), _("This link has already been activated for verification."), indicator_color="red", )
def apply_action(action, doctype, docname, current_state, user=None, last_modified=None): if not verify_request(): return doc = frappe.get_doc(doctype, docname) doc_workflow_state = get_doc_workflow_state(doc) if doc_workflow_state == current_state: action_link = get_confirm_workflow_action_url(doc, action, user) if not last_modified or get_datetime( doc.modified) == get_datetime(last_modified): return_action_confirmation_page(doc, action, action_link) else: return_action_confirmation_page(doc, action, action_link, alert_doc_change=True) else: return_link_expired_page(doc, doc_workflow_state)
def test_expose(self): from frappe.utils.verified_command import verify_request frappe.sendmail(recipients=['*****@*****.**'], cc=['*****@*****.**'], sender="*****@*****.**", reference_doctype='User', reference_name="Administrator", subject='Testing Email Queue', message='This is mail is queued!', unsubscribe_message="Unsubscribe", now=True) email_queue = frappe.db.sql("""select name from `tabEmail Queue` where status='Sent'""", as_dict=1) self.assertEqual(len(email_queue), 1) queue_recipients = [r.recipient for r in frappe.db.sql("""select recipient from `tabEmail Queue Recipient` where status='Sent'""", as_dict=1)] self.assertTrue('*****@*****.**' in queue_recipients) self.assertTrue('*****@*****.**' in queue_recipients) message = frappe.db.sql("""select message from `tabEmail Queue` where status='Sent'""", as_dict=1)[0].message self.assertTrue('<!--recipient-->' in message) email_obj = email.message_from_string(frappe.safe_decode(frappe.flags.sent_mail)) for part in email_obj.walk(): content = part.get_payload(decode=True) if content: frappe.local.flags.signed_query_string = re.search(r'(?<=/api/method/frappe.email.queue.unsubscribe\?).*(?=\n)', content.decode()).group(0) self.assertTrue(verify_request()) break
def test_expose(self): from frappe.utils.verified_command import verify_request frappe.sendmail(recipients=['*****@*****.**'], cc=['*****@*****.**'], sender="*****@*****.**", reference_doctype='User', reference_name="Administrator", subject='Testing Email Queue', message='This is mail is queued!', unsubscribe_message="Unsubscribe", now=True) email_queue = frappe.db.sql("""select name from `tabEmail Queue` where status='Sent'""", as_dict=1) self.assertEqual(len(email_queue), 1) queue_recipients = [r.recipient for r in frappe.db.sql("""select recipient from `tabEmail Queue Recipient` where status='Sent'""", as_dict=1)] self.assertTrue('*****@*****.**' in queue_recipients) self.assertTrue('*****@*****.**' in queue_recipients) message = frappe.db.sql("""select message from `tabEmail Queue` where status='Sent'""", as_dict=1)[0].message self.assertTrue('<!--recipient-->' in message) email_obj = email.message_from_string(frappe.safe_decode(frappe.flags.sent_mail)) for part in email_obj.walk(): content = part.get_payload(decode=True) if content: frappe.local.flags.signed_query_string = re.search('(?<=/api/method/frappe.email.queue.unsubscribe\?).*(?=\n)', content.decode()).group(0) self.assertTrue(verify_request()) break
def confirmed_unsubscribe(email, name): if not verify_request(): return for email_group in get_email_groups(name): frappe.db.sql('''update `tabEmail Group Member` set unsubscribed=1 where email=%s and email_group=%s''',(email, email_group.email_group)) frappe.db.commit() return_unsubscribed_page(email, name)
def confirm_subscription(email): from frappe.utils.verified_command import verify_request if not verify_request(): return # Default user message message = frappe._dict({ "title": _("newsletter:dialog:title:newsletter_subscription"), "html": _('newsletter:dialog:body:error_adding_email_"{0}".').format(email), "primary_label": _("dialog:body:go_to_homepage"), }) group_name = "EGD Subscriptions" if not frappe.db.exists("Email Group", group_name): frappe.get_doc({ "doctype": "Email Group", "title": group_name, }).insert(ignore_permissions=True) from frappe.sessions import get_geo_from_ip country_code = "" if frappe.local.request_ip: geo = get_geo_from_ip(frappe.local.request_ip) if geo and "country" in geo: country_code = geo["country"]["iso_code"] from frappe.utils import validate_email_address email = email.strip() email_valid = validate_email_address(email, False) if email_valid: if not frappe.db.get_value("Email Group Member", { "email_group": group_name, "email": email_valid }): frappe.get_doc({ "doctype": "Email Group Member", "email_group": group_name, "email": email_valid, "country": country_code, "ip": frappe.local.request_ip, }).insert(ignore_permissions=True) frappe.get_doc("Email Group", group_name).update_total_subscribers() frappe.db.commit() message.html = _('newsletter:dialog:body:email_"{0}"_subscribed_ok' ).format(email) else: message.html = _( 'newsletter:dialog:body:email_"{0}"_subscribed_previously' ).format(email) frappe.respond_as_web_page(**message)
def confirmed_unsubscribe(email, name): if not verify_request(): return for email_group in get_email_groups(name): frappe.db.sql( '''update `tabEmail Group Member` set unsubscribed=1 where email=%s and email_group=%s''', (email, email_group.email_group)) frappe.db.commit() return_unsubscribed_page(email, name)
def unsubscribe(email, name): if not verify_request(): return subs_id = frappe.db.get_value("Email Group Member", {"email": email, "email_group": name}) if subs_id: subscriber = frappe.get_doc("Email Group Member", subs_id) subscriber.unsubscribed = 1 subscriber.save(ignore_permissions=True) frappe.db.commit() return_unsubscribed_page(email)
def unsubscribe(email, name): if not verify_request(): return subs_id = frappe.db.get_value("Newsletter List Subscriber", {"email": email, "newsletter_list": name}) if subs_id: subscriber = frappe.get_doc("Newsletter List Subscriber", subs_id) subscriber.unsubscribed = 1 subscriber.save(ignore_permissions=True) frappe.db.commit() return_unsubscribed_page(email)
def unsubscribe(doctype, name, email): # unsubsribe from comments and communications if not verify_request(): return frappe.get_doc({ "doctype": "Email Unsubscribe", "email": email, "reference_doctype": doctype, "reference_name": name }).insert(ignore_permissions=True) frappe.db.commit() return_unsubscribed_page(email, doctype, name)
def get_context(context): if not verify_request(): context.success = False return context email = frappe.form_dict['email'] appointment_name = frappe.form_dict['appointment'] if email and appointment_name: appointment = frappe.get_doc('Appointment', appointment_name) appointment.set_verified(email) context.success = True return context else: context.success = False return context
def confirm_action(doctype, docname, user, action): if not verify_request(): return logged_in_user = frappe.session.user if logged_in_user == 'Guest' and user: # to allow user to apply action without login frappe.set_user(user) doc = frappe.get_doc(doctype, docname) newdoc = apply_workflow(doc, action) frappe.db.commit() return_success_page(newdoc) # reset session user frappe.set_user(logged_in_user)
def confirm_subscription(email): if not verify_request(): return if not frappe.db.exists("Email Group", _("Website")): frappe.get_doc({ "doctype": "Email Group", "title": _("Website") }).insert(ignore_permissions=True) frappe.flags.ignore_permissions = True add_subscribers(_("Website"), email) frappe.db.commit() frappe.respond_as_web_page(_("Confirmed"), _("{0} has been successfully added to our Email Group.").format(email))
def confirm_subscription(email): if not verify_request(): return if not frappe.db.exists("Newsletter List", _("Website")): frappe.get_doc({ "doctype": "Newsletter List", "title": _("Website") }).insert(ignore_permissions=True) frappe.flags.ignore_permissions = True add_subscribers(_("Website"), email) frappe.db.commit() frappe.respond_as_web_page(_("Confirmed"), _("{0} has been successfully added to our Newsletter list.").format(email))
def apply_action(action, doctype, docname, current_state, user=None, last_modified=None): if not verify_request(): return doc = frappe.get_doc(doctype, docname) doc_workflow_state = get_doc_workflow_state(doc) if doc_workflow_state == current_state: action_link = get_confirm_workflow_action_url(doc, action, user) if not last_modified or get_datetime(doc.modified) == get_datetime(last_modified): return_action_confirmation_page(doc, action, action_link) else: return_action_confirmation_page(doc, action, action_link, alert_doc_change=True) else: return_link_expired_page(doc, doc_workflow_state)
def confirm_subscription(email, email_group=_('Website')): if not verify_request(): return if not frappe.db.exists("Email Group", email_group): frappe.get_doc({ "doctype": "Email Group", "title": email_group }).insert(ignore_permissions=True) frappe.flags.ignore_permissions = True add_subscribers(email_group, email) frappe.db.commit() frappe.respond_as_web_page(_("Confirmed"), _("{0} has been successfully added to the Email Group.").format(email), indicator_color='green')
def unsubscribe(doctype, name, email): # unsubsribe from comments and communications if not verify_request(): return try: frappe.get_doc({ "doctype": "Email Unsubscribe", "email": email, "reference_doctype": doctype, "reference_name": name, }).insert(ignore_permissions=True) except frappe.DuplicateEntryError: frappe.db.rollback() else: frappe.db.commit() return_unsubscribed_page(email, doctype, name)
def confirm_subscription(email, email_group=_("Website")): """API endpoint to confirm email subscription. This endpoint is called when user clicks on the link sent to their mail. """ if not verify_request(): return if not frappe.db.exists("Email Group", email_group): frappe.get_doc({"doctype": "Email Group", "title": email_group}).insert( ignore_permissions=True ) frappe.flags.ignore_permissions = True add_subscribers(email_group, email) frappe.db.commit() frappe.respond_as_web_page( _("Confirmed"), _("{0} has been successfully added to the Email Group.").format(email), indicator_color="green", )
def get_context(context): frappe.flags.ignore_permissions = True # Called for confirmation. if "email" in frappe.form_dict and frappe.request.method == "GET": 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 and frappe.request.method == "POST": 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"