def send_notification(user, email_dict):
    '''Email `email_dict` to `user`.'''
    import ckan.lib.mailer

    if not user.get('email'):
        # FIXME: Raise an exception.
        return

    try:
        custom_mailer.mail_recipient(user['display_name'], user['email'],
                                     email_dict['subject'], email_dict['body'])
    except ckan.lib.mailer.MailerException:
        raise
示例#2
0
    def _submit(context):
        try:
            data_dict = logic.clean_dict(
                unflatten(
                    logic.tuplize_dict(logic.parse_params(request.params))))
            context['message'] = data_dict.get('log_message', '')
            c.form = data_dict['name']
            captcha.check_recaptcha(request)
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))
        except captcha.CaptchaError:
            error_msg = _(u'Bad Captcha. Please try again.')
            h.flash_error(error_msg)

        errors = {}
        error_summary = {}

        if data_dict["email"] == '':
            errors['email'] = [_(u'Missing value')]
            error_summary[_(u'email')] = _(u'Missing value')

        if data_dict["name"] == '':
            errors['name'] = [_(u'Missing Value')]
            error_summary[_(u'name')] = _(u'Missing value')

        if data_dict["content"] == '':
            errors['content'] = [_(u'Missing value')]
            error_summary[_(u'request')] = _(u'Missing value')

        if len(errors) == 0:
            global author_email_address
            global report_referrer
            global report_resource_name
            global report_organization_name
            global report_dataset_name

            if report_organization_name is None:
                report_organization_name = ""
            if report_dataset_name is None:
                report_dataset_name = ""
            if report_dataset_name is None:
                report_dataset_name = ""

            if "form" in request.GET:
                if "link" in request.GET.getone('form'):
                    report_mail_title = _(
                        u'Report Broken Link - Government Data')
                    report_mail_secondary_title = _(
                        u'A broken link report is recieved')
            else:
                report_mail_title = _(u'Report - Government Data')
                report_mail_secondary_title = _(u'A report is recieved')
            body = _('Hello') + ","
            body += '\n' + report_mail_secondary_title
            body += '\n' + _(
                u'First name and surname') + ": " + data_dict["name"]
            body += '\n' + _(u'Email') + ": " + data_dict["email"]
            body += '\n' + _(u'Data ID') + ': ' + data_dict["id"]
            body += '\n' + _(u'Link to data') + ': ' + report_referrer
            body += '\n' + _(u'Message') + ': ' + data_dict["content"]
            body += '\n' + _(u'Organization') + ': ' + report_organization_name
            body += '\n' + _(u'Dataset') + ': ' + report_dataset_name
            body += '\n' + _(u'Resource') + ': ' + report_resource_name
            body += '\n\n' + _(u'Best Regards')
            body += '\n' + _(u'Government Data Site')

            mail_dict = {
                #added it to send it to the maintainer of the data
                'recipient_email':
                config.get('email_to'),
                'recipient_name':
                config.get("ckanext.report.recipient_name",
                           config.get('ckan.site_title')),
                'subject':
                config.get("ckanext.report.subject", report_mail_title),
                'body':
                body,
                'headers': {
                    'reply-to': data_dict["email"]
                }
            }

            mail_dict_author = {
                #added it to send it to the author of the data
                'recipient_email':
                author_email_address,
                'recipient_name':
                config.get("ckanext.report.recipient_name",
                           config.get('ckan.site_title')),
                'subject':
                config.get("ckanext.report.subject", report_mail_title),
                'body':
                body,
                'headers': {
                    'reply-to': data_dict["email"]
                }
            }

            # Allow other plugins to modify the mail_dict
            for plugin in p.PluginImplementations(Ireport):
                plugin.mail_alter(mail_dict, data_dict)
                plugin.mail_alter(mail_dict_author, data_dict)
            try:
                #sending email
                custom_mailer.mail_recipient(**mail_dict)
                if author_email_address:
                    custom_mailer.mail_recipient(**mail_dict_author)
            except (mailer.MailerException, socket.error):
                h.flash_error(
                    _(u'Sorry, there was an error sending the email. Please try again later'
                      ))
            else:
                data_dict['success'] = True

        return data_dict, errors, error_summary
示例#3
0
    def _submit(context):

        errors = {}
        error_summary = {}

        try:
            data_dict = logic.clean_dict(
                unflatten(
                    logic.tuplize_dict(logic.parse_params(request.params))))
            context['message'] = data_dict.get('log_message', '')
            c.form = data_dict['name']
            if (config.get('ckan.recaptcha.is_enabled')) == "True":
                if not (captcha.check_recaptcha(request)):
                    raise captcha.CaptchaError
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))
        except captcha.CaptchaError:
            errors['captcha'] = [_(u'Bad Captcha. Please try again.')]
            error_summary[_('captcha')] = _(u'Bad Captcha. Please try again.')

        if data_dict["email"] == '':
            errors['email'] = [_(u'Missing value')]
            error_summary[_('email')] = _(u'Missing value')

        if data_dict["name"] == '':
            errors['name'] = [_(u'Missing value')]
            error_summary[_('Contact Name')] = _(u'Missing value')

        if data_dict["content"] == '':
            errors['content'] = [_(u'Missing value')]
            error_summary[_('request')] = _(u'Missing value')

        if c.contact_title == _('Data Request'):
            body_title = _('A data request form is recieved')
            mail_subject = _('Data Request - Government Data')
        else:
            body_title = _('A contact form is recieved')
            mail_subject = _('Contact - Government Data')

        if len(errors) == 0:

            body = _('Hello') + ","
            body += '\n' + body_title
            body += '\n' + _(
                u'First name and surname') + ":" + data_dict["name"]
            body += '\n' + _(u'Email') + ":" + data_dict["email"]
            if c.contact_title == _('Data Request'):
                body += '\n' + _(
                    u'organization_name') + ':' + data_dict["organizations"]
            body += '\n' + _(u'Message') + ':' + data_dict["content"]
            body += '\n\n' + _(u'Best Regards')
            body += '\n' + _(u'Government Data Site')

            mail_dict = {
                'recipient_email':
                config.get('email_to'),
                'recipient_name':
                config.get("ckanext.contact.recipient_name",
                           config.get('ckan.site_title')),
                'subject':
                config.get("ckanext.contact.subject", mail_subject),
                'body':
                body,
                'headers': {
                    'reply-to': data_dict["email"]
                }
            }

            # Allow other plugins to modify the mail_dict
            for plugin in p.PluginImplementations(IContact):
                plugin.mail_alter(mail_dict, data_dict)

            try:
                custom_mailer.mail_recipient(**mail_dict)
            except (mailer.MailerException, socket.error):
                h.flash_error(
                    _(u'Sorry, there was an error sending the email. Please try again later'
                      ))
            else:
                data_dict['success'] = True

        return data_dict, errors, error_summary
示例#4
0
文件: report.py 项目: CIOIL/DataGovIL
    def _submit(context):
        try:
            data_dict = logic.clean_dict(unflatten(logic.tuplize_dict(logic.parse_params(request.params))))
            context['message'] = data_dict.get('log_message', '')
            c.form = data_dict['name']
            captcha.check_recaptcha(request)
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))
        except captcha.CaptchaError:
            error_msg = _(u'Bad Captcha. Please try again.')
            h.flash_error(error_msg)

        errors = {}
        error_summary = {}

        if data_dict["email"] == '':
            errors['email'] = [_(u'Missing value')]
            error_summary[_(u'email')] = _(u'Missing value')

        if data_dict["name"] == '':
            errors['name'] = [_(u'Missing Value')]
            error_summary[_(u'name')] = _(u'Missing value')

        if data_dict["content"] == '':
            errors['content'] = [_(u'Missing value')]
            error_summary[_(u'request')] = _(u'Missing value')

        if len(errors) == 0:
            global author_email_address
            #global report_referrer
            global report_resource_name
            global report_organization_name
            global report_dataset_name

            if report_organization_name is None:
                report_organization_name = ""
            if report_dataset_name is None:
                report_dataset_name = ""
            if report_dataset_name is None:
                report_dataset_name = ""

            if "form" in request.GET:
                if "link" in request.GET.getone('form'):
                    report_mail_title = _(u'Report Broken Link - Government Data')
                    report_mail_secondary_title = _(u'A broken link report is recieved')
            else:
                report_mail_title = _(u'Report - Government Data')
                report_mail_secondary_title = _(u'A report is recieved')
            body = _('Hello')+","
            body += '\n'+report_mail_secondary_title
            body += '\n'+_(u'First name and surname')+": "+data_dict["name"]
            body += '\n'+_(u'Email')+": "+data_dict["email"]
            body += '\n'+_(u'Data ID')+': '+data_dict["id"]
            body += '\n'+_(u'Link to data')+': '+report_referrer
            body += '\n'+_(u'Message')+': '+data_dict["content"]
            body += '\n'+_(u'Organization')+': '+report_organization_name
            body += '\n'+_(u'Dataset')+': '+report_dataset_name
            body += '\n'+_(u'Resource')+': '+report_resource_name
            body += '\n\n'+_(u'Best Regards')
            body += '\n'+_(u'Government Data Site')

            mail_dict = {
                #added it to send it to the maintainer of the data
                'recipient_email': config.get('email_to'),
                'recipient_name': config.get("ckanext.report.recipient_name", config.get('ckan.site_title')),
                'subject': config.get("ckanext.report.subject", report_mail_title),
                'body': body,
                'headers': {'reply-to': data_dict["email"]}
            }

            mail_dict_author = {
                #added it to send it to the author of the data
                'recipient_email': author_email_address,
                'recipient_name': config.get("ckanext.report.recipient_name", config.get('ckan.site_title')),
                'subject': config.get("ckanext.report.subject", report_mail_title),
                'body': body,
                'headers': {'reply-to': data_dict["email"]}
            }

            # Allow other plugins to modify the mail_dict
            for plugin in p.PluginImplementations(IReport):
                plugin.mail_alter(mail_dict, data_dict)
                plugin.mail_alter(mail_dict_author, data_dict)
            try:
                #sending email
                custom_mailer.mail_recipient(**mail_dict)
                if author_email_address:
                    custom_mailer.mail_recipient(**mail_dict_author)
            except (mailer.MailerException, socket.error):
                h.flash_error(_(u'Sorry, there was an error sending the email. Please try again later'))
            else:
                data_dict['success'] = True

        return data_dict, errors, error_summary
示例#5
0
    def _submit(context):

        errors = {}
        error_summary = {}

        try:
            data_dict = logic.clean_dict(unflatten(logic.tuplize_dict(logic.parse_params(request.params))))
            context['message'] = data_dict.get('log_message', '')
            c.form = data_dict['name']
            if not (captcha.check_recaptcha(request)):
                raise captcha.CaptchaError
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))
        except captcha.CaptchaError:
            errors['captcha'] = [_(u'Bad Captcha. Please try again.')]
            error_summary[_('captcha')] = _(u'Bad Captcha. Please try again.')



        if data_dict["email"] == '':
            errors['email'] = [_(u'Missing value')]
            error_summary[_('email')] = _(u'Missing value')

        if data_dict["name"] == '':
            errors['name'] = [_(u'Missing value')]
            error_summary[_('Contact Name')] = _(u'Missing value')

        if data_dict["subject"] == '':
            errors['subject'] = [_(u'Missing value')]
            error_summary[_('Subject')] = _(u'Missing value')

        if data_dict["content"] == '':
            errors['content'] = [_(u'Missing value')]
            error_summary[_('request')] = _(u'Missing value')

        if c.contact_title == _('Data Request'):
            body_title = _('A data request form is recieved')
            mail_subject = _('Data Request - Government Data')
        else:
           body_title = _('A contact form is recieved')
           mail_subject = _('Contact - Government Data')

        if len(errors) == 0:

            body = _('Hello')+","
            body += '\n'+body_title
            body += '\n'+_(u'First name and surname')+":"+data_dict["name"]
	    body += '\n' + _(u'Subject') + ":" + data_dict["subject"]
            body += '\n'+_(u'Email')+":"+data_dict["email"]
            if c.contact_title == _('Data Request'):
                body += '\n' + _(u'organization_name') + ':' + data_dict["organizations"]
            body += '\n'+_(u'Message')+':'+data_dict["content"]
            body += '\n\n'+_(u'Best Regards')
            body += '\n'+_(u'Government Data Site')

            mail_dict = {
                'recipient_email': config.get('email_to'),
                'recipient_name': config.get("ckanext.contact.recipient_name", config.get('ckan.site_title')),
                'subject': config.get("ckanext.contact.subject", mail_subject),
                'body': body,
                'headers': {'reply-to': data_dict["email"]}
            }

            # Allow other plugins to modify the mail_dict
            for plugin in p.PluginImplementations(IContact):
                plugin.mail_alter(mail_dict, data_dict)

            try:
                custom_mailer.mail_recipient(**mail_dict)
            except (mailer.MailerException, socket.error):
                h.flash_error(_(u'Sorry, there was an error sending the email. Please try again later'))
            else:
                data_dict['success'] = True

        return data_dict, errors, error_summary
示例#6
0
def submit():
    '''
    Take the data in the request params and send an email using them. If the data is invalid or
    a recaptcha is setup and it fails, don't send the email.

    :return: a dict of details
    '''
    # this variable holds the status of sending the email
    email_success = True

    # pull out the data from the request
    data_dict = logic.clean_dict(
        unflatten(logic.tuplize_dict(logic.parse_params(
            toolkit.request.values))))

    # validate the request params
    errors, error_summary, recaptcha_error = validate(data_dict)

    # if there are not errors and no recaptcha error, attempt to send the email
    if len(errors) == 0 and recaptcha_error is None:

        # Mail title by request type
        if data_dict.get(u'report_type', '') == 'general':
            report_mail_title = u'Contact US - General - Government Data'
        elif data_dict.get(u'report_type', '') == 'dataset_req':
            report_mail_title = u'Contact US - Dataset Request - Government Data'
        else:
            report_mail_title = u'Contact US - Report - Government Data'

        # general content
        body = u'{}\n\nSent by:\nName: {}\nEmail: {}\n'.format(data_dict[u'content'],
                                                               data_dict[u'name'],
                                                               data_dict[u'email'])

        # if report page was from resource page -adding resource data
        if data_dict.get('type','') == 'report_mail':
            report_mail_title = 'Report Broken Link - Government Data'
            body += '\n' + ('Dataset ID') + ': ' + data_dict[u'dataset_id']
            body += '\n' + ('Dataset Title') + ': ' + data_dict[u'dataset_title']
            body += '\n' + ('Dataset Author') + ': ' + data_dict[u'dataset_author']
            body += '\n' + ('Resource ID') + ': ' + data_dict[u'resource_id']
            body += '\n' + ('Resource Title') + ': ' + data_dict[u'resource_title']
            body += '\n' + ('Dataset Author') + ': ' + data_dict[u'dataset_author']
            body += '\n' + ('Organization') + ': ' + data_dict[u'organization_name']
            body += '\n' + ('Link to data') + ': ' + "{}/dataset/{}/resource/{}".format(toolkit.config.get('ckan.site_url', '//localhost:5000'),
                                                                                            data_dict[u'dataset_id'],
                                                                                          data_dict[u'resource_id'])
            body += '\n\n' + (u'Best Regards')
            body += '\n' + (u'Government Data Site')

        mail_dict = {
            u'recipient_email': toolkit.config.get(u'ckanext.contact.mail_to',
                                                   toolkit.config.get(u'email_to')),
            u'recipient_name': toolkit.config.get(u'ckanext.contact.recipient_name',
                                                  toolkit.config.get(u'ckan.site_title')),
            u'subject': report_mail_title,
            u'body': body,
            u'headers': {
                u'reply-to': data_dict[u'email']
                }
            }

        # allow other plugins to modify the mail_dict
        for plugin in PluginImplementations(IContact):
            plugin.mail_alter(mail_dict, data_dict)

        try:
            custom_mailer.mail_recipient(**mail_dict)
        except (mailer.MailerException, socket.error):
            email_success = False

    return {
        u'success': recaptcha_error is None and len(errors) == 0 and email_success,
        u'data': data_dict,
        u'errors': errors,
        u'error_summary': error_summary,
        u'recaptcha_error': recaptcha_error,
        }