<commit_before>import ckan.model as model
import ckan.logic as logic
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.captcha as captcha
import ckan.lib.mailer as mailer

import ckan.plugins.toolkit as toolkit
from ckan.common import _, request, c, g
from ckan.lib.base import render_jinja2

from logging import getLogger
from pylons import config
from email.header import Header

import simplejson as json

import ckan.lib.navl.dictization_functions as dictization_functions
DataError = dictization_functions.DataError
unflatten = dictization_functions.unflatten

render = base.render

log = getLogger(__name__)

class RestrictedController(toolkit.BaseController):

    def __before__(self, action, **env):
        base.BaseController.__before__(self, action, **env)
        try:
            context = {'model': base.model, 'user': base.c.user or base.c.author,
                       'auth_user_obj': base.c.userobj}
            logic.check_access('site_read', context)
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))

    def _send_request_mail(self, data):
        success = False
        try:

            resource_link = toolkit.url_for(controller='package', action='resource_read', 
                                    id=data.get('package_name'), resource_id=data.get('resource_id'))
            
            resource_edit_link = toolkit.url_for(controller='package', action='resource_edit', 
                                              id=data.get('package_name') , resource_id=data.get('resource_id'))

            extra_vars = {
                'site_title': config.get('ckan.site_title'),
                'site_url': config.get('ckan.site_url'),
                'maintainer_name': data.get('maintainer_name', 'Maintainer'),
                'user_id': data.get('user_id','the user id'),
                'user_name': data.get('user_name', ''),
                'user_email': data.get('user_email', ''),
                'resource_name': data.get('resource_name',''),
                'resource_link': config.get('ckan.site_url') + resource_link,
                'resource_edit_link': config.get('ckan.site_url') + resource_edit_link,
                'package_name': data.get('resource_name',''),
                'message': data.get('message',''),
                'admin_email_to': config.get('email_to', 'email_to_undefined')
                }

            body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars)
            subject = 'Access Request to resource ' +  data.get('resource_name','') + ' (' +  data.get('package_name','')  + ') from ' + data.get('user_name','')
            headers = {'CC': ",".join(email_dict.keys()),  'reply-to': data.get('user_email')}

            email_dict = {data.get('maintainer_email'): extra_vars.get('maintainer_name'), extra_vars.get('admin_email_to'): extra_vars.get('site_title') + ' Admin'}
            ## CC doesn't work and mailer cannot send to multiple addresses
            for email, name in email_dict.iteritems():
                mailer.mail_recipient(name, email, subject, body, headers)
            
            ## Special copy for the user (no links)
            email = data.get('user_email')
            name = data.get('user_name','User')
            
            extra_vars['resource_link'] = '[...]'
            extra_vars['resource_edit_link'] = '[...]'
            body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars)
            body_user = "Please find below a copy of the access request mail sent. \n\n >> {0}".format( body.replace("\n", "\n >> "))
            mailer.mail_recipient(name, email, 'Fwd: ' + subject, body_user, headers)
            success=True

        except mailer.MailerException as mailer_exception:
            log.error("Cannot access request mail after registration ")
            log.error(mailer_exception)
            pass

        return success

    def _send_request(self, context):
<commit_msg>fixed minor bug<commit_after>import ckan.model as model
import ckan.logic as logic
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.captcha as captcha
import ckan.lib.mailer as mailer

import ckan.plugins.toolkit as toolkit
from ckan.common import _, request, c, g
from ckan.lib.base import render_jinja2

from logging import getLogger
from pylons import config
from email.header import Header

import simplejson as json

import ckan.lib.navl.dictization_functions as dictization_functions
DataError = dictization_functions.DataError
unflatten = dictization_functions.unflatten

render = base.render

log = getLogger(__name__)

class RestrictedController(toolkit.BaseController):

    def __before__(self, action, **env):
        base.BaseController.__before__(self, action, **env)
        try:
            context = {'model': base.model, 'user': base.c.user or base.c.author,
                       'auth_user_obj': base.c.userobj}
            logic.check_access('site_read', context)
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))

    def _send_request_mail(self, data):
        success = False
        try:

            resource_link = toolkit.url_for(controller='package', action='resource_read', 
                                    id=data.get('package_name'), resource_id=data.get('resource_id'))
            
            resource_edit_link = toolkit.url_for(controller='package', action='resource_edit', 
                                              id=data.get('package_name') , resource_id=data.get('resource_id'))

            extra_vars = {
                'site_title': config.get('ckan.site_title'),
                'site_url': config.get('ckan.site_url'),
                'maintainer_name': data.get('maintainer_name', 'Maintainer'),
                'user_id': data.get('user_id','the user id'),
                'user_name': data.get('user_name', ''),
                'user_email': data.get('user_email', ''),
                'resource_name': data.get('resource_name',''),
                'resource_link': config.get('ckan.site_url') + resource_link,
                'resource_edit_link': config.get('ckan.site_url') + resource_edit_link,
                'package_name': data.get('resource_name',''),
                'message': data.get('message',''),
                'admin_email_to': config.get('email_to', 'email_to_undefined')
                }

            body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars)
            subject = 'Access Request to resource ' +  data.get('resource_name','') + ' (' +  data.get('package_name','')  + ') from ' + data.get('user_name','')

            email_dict = {data.get('maintainer_email'): extra_vars.get('maintainer_name'), extra_vars.get('admin_email_to'): extra_vars.get('site_title') + ' Admin'}
            headers = {'CC': ",".join(email_dict.keys()),  'reply-to': data.get('user_email')}

            ## CC doesn't work and mailer cannot send to multiple addresses
            for email, name in email_dict.iteritems():
                mailer.mail_recipient(name, email, subject, body, headers)
            
            ## Special copy for the user (no links)
            email = data.get('user_email')
            name = data.get('user_name','User')
            
            extra_vars['resource_link'] = '[...]'
            extra_vars['resource_edit_link'] = '[...]'
            body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars)
            body_user = "Please find below a copy of the access request mail sent. \n\n >> {0}".format( body.replace("\n", "\n >> "))
            mailer.mail_recipient(name, email, 'Fwd: ' + subject, body_user, headers)
            success=True

        except mailer.MailerException as mailer_exception:
            log.error("Cannot access request mail after registration ")
            log.error(mailer_exception)
            pass

        return success

    def _send_request(self, context):

{'fraction_non_alphanumeric': 0.12526302760242605, 'fraction_numerical': 0.0017328877336303997, 'mean_word_length': 2.562610229276896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6179094', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2264, 'n_words': 488}