'Entry point for REST API
@param r: the request (S3Request)
@param attr: REST controller parameters'

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

    if (r.record and (r.representation in ('html', 'aadata'))):
        T = current.T
        db = current.db
        s3db = current.s3db
        auth = current.auth
        is_admin = auth.s3_has_role('ADMIN')
        accessible = auth.s3_accessible_query
        profile_widgets = []
        add_widget = profile_widgets.append
        dt_row_actions = self.dt_row_actions
        from s3 import FS
        widget = {'label': T('My    Organizations'), 'icon': 'organisation', 'insert': False, 'tablename': 'org_organisation', 'type': 'datatable', 'actions': dt_row_actions('org', 'organisation'), 'list_fields': ['name', (T('Type'), 'organisation_organisation_type.organisation_type_id'), 'phone', (T('Email'), 'email.value'), 'website']}
        if (not is_admin):
            otable = s3db.org_organisation
            rows = db(accessible('update', 'org_organisation')).select(otable.id)
            organisation_ids = [row.id for row in rows]
            widget['filter'] = FS('id').belongs(organisation_ids)
        add_widget(widget)
        widget = {'label': T('My    Facilities'), 'icon': 'facility', 'insert': False, 'tablename': 'org_facility', 'type': 'datatable', 'actions': dt_row_actions('org', 'facility'), 'list_fields': ['name', 'code', 'site_facility_type.facility_type_id', 'organisation_id', 'location_id']}
        if (not is_admin):
            ftable = s3db.org_facility
            rows = db(accessible('update', 'org_facility')).select(ftable.id)
            facility_ids = [row.id for row in rows]
            widget['filter'] = FS('id').belongs(facility_ids)
        add_widget(widget)
        widget_filter = None
        if (not is_admin):
            gtable = s3db.org_group
            rows = db(accessible('update', 'org_group')).select(gtable.id)
            group_ids = [row.id for row in rows]
            if group_ids:
                widget_filter = FS('id').belongs(group_ids)
        if (is_admin or widget_filter):
            widget = {'label': T('My    Networks'), 'icon': 'org-network', 'insert': False, 'tablename': 'org_group', 'filter': widget_filter, 'type': 'datatable', 'actions': dt_row_actions('org', 'group')}
            add_widget(widget)
        widget_filter = None
        if (not is_admin):
            gtable = s3db.pr_group
            rows = db(accessible('update', 'pr_group')).select(gtable.id)
            group_ids = [row.id for row in rows]
            if group_ids:
                widget_filter = FS('id').belongs(group_ids)
        if (is_admin or widget_filter):
            widget = {'label': T('My    Groups'), 'icon': 'group', 'insert': False, 'tablename': 'pr_group', 'filter': widget_filter, 'type': 'datatable', 'actions': dt_row_actions('hrm', 'group'), 'list_fields': [(T('Network'), 'group_team.org_group_id'), 'name', 'description', (T('Chairperson'), 'chairperson')]}
            add_widget(widget)
        from gluon.html import A, DIV, H2, TAG
        item = None
        title = T('Dashboard')
        if current.deployment_settings.has_module('cms'):
            name = 'Dashboard'
            ctable = s3db.cms_post
            query = ((ctable.name == name) & (ctable.deleted != True))
            row = db(query).select(ctable.id, ctable.title, ctable.body, limitby=(0, 1)).first()
            get_vars = {'page': name, 'url': URL(args='dashboard', vars={})}
            if row:
                title = row.title
                if is_admin:
                    item = DIV(XML(row.body), DIV(A(T('Edit'), _href=URL(c='cms', f='post', args=[row.id, 'update'], vars=get_vars), _class='action-btn'), _class='cms-edit'))
                else:
                    item = DIV(XML(row.body))
            elif is_admin:
                item = DIV(DIV(A(T('Edit'), _href=URL(c='cms', f='post', args='create', vars=get_vars), _class='action-btn'), _class='cms-edit'))
        if (r.representation == 'html'):
            profile_header = TAG[''](DIV(DIV(H2(title), _class='medium-6    columns    end'), _class='row'))
            if item:
                profile_header.append(DIV(DIV(item, _class='medium-12    columns'), _class='row'))
            dashboard_links = DIV(A(T('Personal    Profile'), _href=URL(c='default', f='person'), _class='action-btn'), _class='dashboard-links', _style='padding:0.5rem    0;')
            profile_header.append(DIV(DIV(dashboard_links, _class='medium-12    columns'), _class='row'))
        else:
            profile_header = None
        tablename = r.tablename
        s3db.configure(tablename, profile_cols=2, profile_header=profile_header, profile_widgets=profile_widgets)
        from s3 import S3Profile
        profile = S3Profile()
        profile.tablename = tablename
        profile.request = r
        output = profile.profile(r, **attr)
        if (r.representation == 'html'):
            output['title'] = current.response.title = T('Personal    Dashboard')
        return output
    else:
        raise HTTP(405, current.ERROR.BAD_METHOD)
