'- Set person_id from created_by if not already set
- Handle the case where the page is for a Module home page,
Resource Summary page or Map Layer'

@staticmethod
def cms_post_onaccept(form):

    db = current.db
    s3db = current.s3db
    post_id = form.vars.id
    get_vars = current.request.get_vars
    module = get_vars.get('module', None)
    if module:
        table = db.cms_post_module
        query = (table.module == module)
        resource = get_vars.get('resource', None)
        if resource:
            query &= (table.resource == resource)
            record = get_vars.get('record', None)
            if record:
                query &= (table.record == record)
            else:
                query &= (table.record == None)
        else:
            record = None
            query &= ((table.resource == None) | (table.resource == 'index'))
        result = db(query).update(post_id=post_id)
        if (not result):
            table.insert(post_id=post_id, module=module, resource=resource, record=record)
    layer_id = get_vars.get('layer_id', None)
    if layer_id:
        table = s3db.cms_post_layer
        query = (table.layer_id == layer_id)
        result = db(query).update(post_id=post_id)
        if (not result):
            table.insert(post_id=post_id, layer_id=layer_id)
    table = db.cms_post
    record = db((table.id == post_id)).select(table.person_id, table.created_by, limitby=(0, 1)).first()
    if (record.created_by and (not record.person_id)):
        ptable = s3db.pr_person
        putable = s3db.pr_person_user
        query = ((putable.user_id == record.created_by) & (putable.pe_id == ptable.pe_id))
        person = db(query).select(ptable.id, limitby=(0, 1)).first()
        if person:
            db((table.id == post_id)).update(person_id=person.id)
