'Remove a Tag from a Post
S3Method for interactive requests
- designed to be called as an afterTagRemoved callback to tag-it.js'

@staticmethod
def cms_remove_tag(r, **attr):

    post_id = r.id
    if ((not post_id) or (len(r.args) < 3)):
        raise HTTP(405, current.ERROR.BAD_METHOD)
    tag = r.args[2]
    db = current.db
    ttable = db.cms_tag
    exists = db((ttable.name == tag)).select(ttable.id, ttable.deleted, limitby=(0, 1)).first()
    if exists:
        tag_id = exists.id
        ltable = db.cms_tag_post
        query = ((ltable.tag_id == tag_id) & (ltable.post_id == post_id))
        exists = db(query).select(ltable.id, ltable.deleted, limitby=(0, 1)).first()
        if (exists and (not exists.deleted)):
            resource = current.s3db.resource('cms_tag_post', id=exists.id)
            resource.delete()
    output = current.xml.json_message(True, 200, current.T('Tag    Removed'))
    current.response.headers['Content-Type'] = 'application/json'
    return output
