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

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

    event_id = r.id
    if ((not event_id) or (len(r.args) < 3)):
        raise HTTP(405, current.ERROR.BAD_METHOD)
    tag = r.args[2]
    db = current.db
    s3db = current.s3db
    ttable = s3db.cms_tag
    exists = db((ttable.name == tag)).select(ttable.id, ttable.deleted, limitby=(0, 1)).first()
    if exists:
        tag_id = exists.id
        ltable = s3db.event_tag
        query = ((ltable.tag_id == tag_id) & (ltable.event_id == event_id))
        exists = db(query).select(ltable.id, ltable.deleted, limitby=(0, 1)).first()
        if (exists and (not exists.deleted)):
            resource = s3db.resource('event_tag', 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
