'Entry point for REST API
@param r: the S3Request
@param attr: controller arguments'

def apply_method(self, r, **attr):

    if ((r.name == 'person') and r.id and (not r.component) and (r.representation in ('html', 'aadata'))):
        T = current.T
        s3db = current.s3db
        get_config = s3db.get_config
        settings = current.deployment_settings
        tablename = r.tablename
        if (r.controller == 'vol'):
            controller = 'vol'
            vol = True
        elif (r.controller == 'deploy'):
            controller = 'deploy'
            vol = False
        elif (r.controller == 'member'):
            controller = 'member'
            vol = False
        else:
            controller = 'hrm'
            vol = False
        def dt_row_actions(component, tablename):
            def row_actions(r, list_id):
                editable = get_config(tablename, 'editable')
                if (editable is None):
                    editable = True
                deletable = get_config(tablename, 'deletable')
                if (deletable is None):
                    deletable = True
                if editable:
                    actions = [{'label': T('Open'), 'url': r.url(component=component, component_id='[id]', method='update.popup', vars={'refresh': list_id}), '_class': 'action-btn    edit    s3_modal'}]
                else:
                    actions = [{'label': T('Open'), 'url': r.url(component=component, component_id='[id]', method='read.popup', vars={'refresh': list_id}), '_class': 'action-btn    edit    s3_modal'}]
                if deletable:
                    actions.append({'label': T('Delete'), '_ajaxurl': r.url(component=component, component_id='[id]', method='delete.json'), '_class': 'action-btn    delete-btn-ajax    dt-ajax-delete'})
                return actions
            return row_actions
        profile_widgets = []
        form = self.form
        if form:
            if callable(form):
                form = form(r)
            if (form is not None):
                profile_widgets.append(form)
        if (vol and settings.get_hrm_use_awards()):
            tablename = 'vol_volunteer_award'
            r.customise_resource(tablename)
            widget = dict(type='datatable', actions=dt_row_actions('award', tablename), tablename=tablename, context='person', create_controller='vol', create_function='person', create_component='award', pagesize=None)
            profile_widgets.append(widget)
        if settings.get_hrm_use_education():
            tablename = 'pr_education'
            widget = dict(label='Education', label_create='Add    Education', type='datatable', actions=dt_row_actions('education', tablename), tablename=tablename, context='person', create_controller=controller, create_function='person', create_component='education', pagesize=None)
            profile_widgets.append(widget)
        if vol:
            vol_experience = settings.get_hrm_vol_experience()
            experience = (vol_experience in ('both', 'experience'))
            missions = None
        else:
            staff_experience = settings.get_hrm_staff_experience()
            experience = (staff_experience in ('both', 'experience'))
            missions = (staff_experience in ('both', 'missions'))
        if experience:
            tablename = 'hrm_experience'
            r.customise_resource(tablename)
            widget = dict(type='datatable', actions=dt_row_actions('experience', tablename), tablename=tablename, context='person', filter=(FS('assignment__link.assignment_id') == None), create_controller=controller, create_function='person', create_component='experience', pagesize=None, list_fields=['start_date', 'end_date', 'employment_type', 'organisation', 'job_title'])
            profile_widgets.append(widget)
        if missions:
            tablename = 'hrm_experience'
            widget = dict(label='Missions', type='datatable', actions=dt_row_actions('experience', tablename), tablename=tablename, context='person', filter=(FS('assignment__link.assignment_id') != None), insert=False, pagesize=None, list_fields=['start_date', 'end_date', 'location_id', 'job_title_id', 'job_title'])
            profile_widgets.append(widget)
        if settings.get_hrm_use_trainings():
            tablename = 'hrm_training'
            if settings.get_hrm_trainings_external():
                widget = dict(label='Internal    Training', label_create='Add    Internal    Training', type='datatable', actions=dt_row_actions('training', tablename), tablename=tablename, context='person', filter=(FS('course_id$external') == False), create_controller=controller, create_function='person', create_component='training', pagesize=None)
                profile_widgets.append(widget)
                widget = dict(label='External    Training', label_create='Add    External    Training', type='datatable', actions=dt_row_actions('training', tablename), tablename=tablename, context='person', filter=(FS('course_id$external') == True), create_controller=controller, create_function='person', create_component='training', pagesize=None)
                profile_widgets.append(widget)
            else:
                widget = dict(label='Training', label_create='Add    Training', type='datatable', actions=dt_row_actions('training', tablename), tablename=tablename, context='person', create_controller=controller, create_function='person', create_component='training', pagesize=None)
                profile_widgets.append(widget)
        if settings.get_hrm_use_skills():
            tablename = 'hrm_competency'
            r.customise_resource(tablename)
            widget = dict(type='datatable', actions=dt_row_actions('competency', tablename), tablename=tablename, context='person', create_controller=controller, create_function='person', create_component='competency', pagesize=None)
            profile_widgets.append(widget)
        if settings.get_hrm_use_certificates():
            tablename = 'hrm_certification'
            widget = dict(label='Certificates', label_create='Add    Certificate', type='datatable', actions=dt_row_actions('certification', tablename), tablename=tablename, context='person', create_controller=controller, create_function='person', create_component='certification', pagesize=None)
            profile_widgets.append(widget)
        if (r.representation == 'html'):
            response = current.response
            profile_header = TAG[''](H2(response.s3.crud_strings['pr_person'].title_display), DIV(hrm_rheader(r), _id='rheader'))
        else:
            profile_header = None
        s3db.configure(tablename, profile_cols=1, profile_header=profile_header, profile_widgets=profile_widgets)
        profile = S3Profile()
        profile.tablename = tablename
        profile.request = r
        output = profile.profile(r, **attr)
        if (r.representation == 'html'):
            output['title'] = response.title = T('CV')
        return output
    else:
        raise HTTP(405, current.ERROR.BAD_METHOD)
