<commit_before>from ckan.lib import base
from ckan import model
from ckan.logic import NotFound, NotAuthorized, check_access, tuplize_dict, clean_dict, parse_params, ValidationError
from ckan.lib import helpers
from ckan.plugins import toolkit
from ckan.lib.base import c, request, render, abort
from ckan.common import _
import ckan.lib.navl.dictization_functions as dict_fns
from ckanext.ytp.request.tools import get_user_member


class YtpRequestController(base.BaseController):
    """ View controller for membership requests """

    not_auth_message = _('Unauthorized')

    def _basic_context(self, user=None):
        return {'model': model, 'session': model.Session, 'user': user or c.user}

    def _get_available_roles(self, user=None, context=None):
        roles = []
        for role in toolkit.get_action('member_roles_list')(context or self._basic_context(user), {}):
            if role['value'] != 'member':
                roles.append(role)
        return roles

    def new(self, data=None, errors=None, error_summary=None):
        """ Controller for new member request """
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'save': 'save' in request.params,
                   'parent': request.params.get('parent', None)}
        try:
            check_access('member_request_create', context)
        except NotAuthorized:
            abort(401, self.not_auth_message)

        if context['save'] and not data:
            return self._save_new(context)

        data = data or {}

        extra_vars = {'data': data, 'errors': errors or {}, 'error_summary': error_summary or {}, 'action': 'new',
                      'selected_organization': request.params.get('selected_organization', None)}

        c.roles = self._get_available_roles()
        c.user_role = 'editor'

        c.form = render("request/new_request_form.html", extra_vars=extra_vars)
        return render("request/new.html")

    def _save_new(self, context):
        try:
            data_dict = clean_dict(dict_fns.unflatten(tuplize_dict(parse_params(request.params))))
            data_dict['group'] = data_dict['organization']
            member = toolkit.get_action('member_request_create')(context, data_dict)
            helpers.redirect_to('member_request_show', member_id=member['id'])
        except NotAuthorized:
            abort(401, self.not_auth_message)
        except NotFound:
            abort(404, _('Item not found'))
        except dict_fns.DataError:
            abort(400, _(u'Integrity Error'))
        except ValidationError, e:
            errors = e.error_dict
            error_summary = e.error_summary
            return self.new(data_dict, errors, error_summary)

    def list(self):
        """ Controller for listing member requests """
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author}
        try:
            member_requests = toolkit.get_action('member_request_list')(context, {})
            extra_vars = {'member_requests': member_requests}
            return render('request/list.html', extra_vars=extra_vars)
        except toolkit.NotAuthorized:
            abort(401, self.not_auth_message)

    def show(self, member_id):
        """ Controller for viewing member request """
        try:
            member = model.Session.query(model.Member).get(member_id)
            if not member or member.state != 'pending':
                abort(404, _('Request not found'))

            role = request.params.get('role', None)
            if role:
                check_access('member_request_process', {'member': member_id})
                member.capacity = role
                revision = model.repo.new_revision()<commit_msg>YH-663: Check access to member request show<commit_after>from ckan.lib import base
from ckan import model
from ckan.logic import NotFound, NotAuthorized, check_access, tuplize_dict, clean_dict, parse_params, ValidationError
from ckan.lib import helpers
from ckan.plugins import toolkit
from ckan.lib.base import c, request, render, abort
from ckan.common import _
import ckan.lib.navl.dictization_functions as dict_fns
from ckanext.ytp.request.tools import get_user_member

class YtpRequestController(base.BaseController):
    """ View controller for membership requests """

    not_auth_message = _('Unauthorized')

    def _basic_context(self, user=None):
        return {'model': model, 'session': model.Session, 'user': user or c.user}

    def _get_available_roles(self, user=None, context=None):
        roles = []
        for role in toolkit.get_action('member_roles_list')(context or self._basic_context(user), {}):
            if role['value'] != 'member':
                roles.append(role)
        return roles

    def new(self, data=None, errors=None, error_summary=None):
        """ Controller for new member request """
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'save': 'save' in request.params,
                   'parent': request.params.get('parent', None)}
        try:
            check_access('member_request_create', context)
        except NotAuthorized:
            abort(401, self.not_auth_message)

        if context['save'] and not data:
            return self._save_new(context)

        data = data or {}

        extra_vars = {'data': data, 'errors': errors or {}, 'error_summary': error_summary or {}, 'action': 'new',
                      'selected_organization': request.params.get('selected_organization', None)}

        c.roles = self._get_available_roles()
        c.user_role = 'editor'

        c.form = render("request/new_request_form.html", extra_vars=extra_vars)
        return render("request/new.html")

    def _save_new(self, context):
        try:
            data_dict = clean_dict(dict_fns.unflatten(tuplize_dict(parse_params(request.params))))
            data_dict['group'] = data_dict['organization']
            member = toolkit.get_action('member_request_create')(context, data_dict)
            helpers.redirect_to('member_request_show', member_id=member['id'])
        except NotAuthorized:
            abort(401, self.not_auth_message)
        except NotFound:
            abort(404, _('Item not found'))
        except dict_fns.DataError:
            abort(400, _(u'Integrity Error'))
        except ValidationError, e:
            errors = e.error_dict
            error_summary = e.error_summary
            return self.new(data_dict, errors, error_summary)

    def list(self):
        """ Controller for listing member requests """
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author}
        try:
            member_requests = toolkit.get_action('member_request_list')(context, {})
            extra_vars = {'member_requests': member_requests}
            return render('request/list.html', extra_vars=extra_vars)
        except toolkit.NotAuthorized:
            abort(401, self.not_auth_message)

    def show(self, member_id):
        """ Controller for viewing member request """
        try:
            check_access('member_request_show', {'member': member_id})
            member = model.Session.query(model.Member).get(member_id)
            if not member or member.state != 'pending':
                abort(404, _('Request not found'))

            role = request.params.get('role', None)
            if role:
                check_access('member_request_process', {'member': member_id})
                member.capacity = role
                revision = model.repo.new_revision()
{'fraction_non_alphanumeric': 0.11151546659734858, 'fraction_numerical': 0.005068884845334027, 'mean_word_length': 2.640018921475875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '225762', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2121, 'n_words': 506}