'Custom Method to send an Alert'

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

    alert_id = r.id
    if ((r.representation != 'html') or (not alert_id) or r.component):
        raise HTTP(501, BADMETHOD)
    auth = current.auth
    authorised = auth.s3_has_permission('update', 'deploy_alert', record_id=alert_id)
    if (not authorised):
        r.unauthorised()
    record = r.record
    mission_id = record.mission_id
    next_url = URL(f='mission', args=[mission_id, 'profile'])
    if record.message_id:
        current.session.error = current.T('This    Alert    has    already    been    sent!')
        redirect(next_url)
    db = current.db
    s3db = current.s3db
    table = s3db.deploy_alert
    contact_method = record.contact_method
    ltable = db.deploy_alert_recipient
    query = ((ltable.alert_id == alert_id) & (ltable.deleted == False))
    if (contact_method == 9):
        recipients = db(query).select(ltable.human_resource_id)
    else:
        recipients = db(query).select(ltable.id, limitby=(0, 1)).first()
    if (not recipients):
        current.session.error = current.T('This    Alert    has    no    Recipients    yet!')
        redirect(next_url)
    settings = current.deployment_settings
    if record.cc:
        cc_groups = settings.get_deploy_cc_groups()
        if cc_groups:
            gtable = s3db.pr_group
            cc_groups = db(gtable.name.belongs(cc_groups)).select(gtable.pe_id)
            cc_groups = [g.pe_id for g in cc_groups]
    else:
        cc_groups = []
    message = record.body
    msg = current.msg
    if (contact_method == 2):
        message_id = msg.send_by_pe_id(record.pe_id, contact_method='SMS', message=message)
        for g in cc_groups:
            msg.send_by_pe_id(g.pe_id, contact_method='SMS', message=message)
    elif (contact_method == 9):
        id = table.insert(body=message, contact_method=2, mission_id=mission_id, created_by=record.created_by, created_on=record.created_on)
        new_alert = dict(id=id)
        s3db.update_super(table, new_alert)
        for row in recipients:
            ltable.insert(alert_id=id, human_resource_id=row.human_resource_id)
        message_id = msg.send_by_pe_id(new_alert['pe_id'], contact_method='SMS', message=message)
        for g in cc_groups:
            msg.send_by_pe_id(g.pe_id, contact_method='SMS', message=message)
        db((table.id == id)).update(message_id=message_id)
    if (contact_method in (1, 9)):
        message = ('%s\n:mission_id:%s:' % (message, mission_id))
        organisation_id = auth.user.organisation_id
        ctable = s3db.msg_email_channel
        query = ((ctable.deleted == False) & (ctable.enabled == True))
        if organisation_id:
            query &= ((ctable.organisation_id == None) | (ctable.organisation_id == organisation_id))
        channels = db(query).select(ctable.username, ctable.server)
        if (not channels):
            current.session.error = current.T('Need    to    configure    an    Email    Address!')
            redirect(URL(f='email_channel'))
        elif (organisation_id and (len(channels) > 1)):
            _channels = channels.find((lambda row: (row.organisation_id == organisation_id)))
            if (not _channels):
                _channels = channels.find((lambda row: (row.organisation_id == None)))
            channel = _channels.first()
        else:
            channel = channels.first()
        username = channel.username
        if ('@' in username):
            from_address = username
        else:
            from_address = ('%s@%s' % (username, channel.server))
        message_id = msg.send_by_pe_id(record.pe_id, subject=record.subject, message=message, from_address=from_address)
        for g in cc_groups:
            msg.send_by_pe_id(g.pe_id, subject=record.subject, message=message, from_address=from_address)
    if settings.get_deploy_post_to_twitter():
        try:
            import tweepy
        except ImportError:
            current.log.debug('tweepy    module    needed    for    sending    tweets')
        else:
            try:
                msg.send_tweet(text=message)
            except tweepy.error.TweepError as e:
                current.log.debug(('Sending    tweets    failed:    %s' % e))
    data = dict(message_id=message_id)
    if (contact_method == 2):
        data['subject'] = None
    elif (contact_method == 9):
        data['contact_method'] = 1
    db((table.id == alert_id)).update(**data)
    current.session.confirmation = current.T('Alert    Sent')
    redirect(next_url)
