'Update the Status for a Channel'

@staticmethod
def update_channel_status(channel_id, status, period=None):

    db = current.db
    stable = current.s3db.msg_channel_status
    query = (stable.channel_id == channel_id)
    old_status = db(query).select(stable.status, limitby=(0, 1)).first()
    if old_status:
        if (status and (status[0] == '+')):
            old_status = old_status.status
            try:
                old_status = int(old_status)
            except:
                new_status = status
            else:
                new_status = (old_status + int(status[1:]))
        else:
            new_status = status
        db(query).update(status=new_status)
    else:
        stable.insert(channel_id=channel_id, status=status)
    if period:
        ttable = db.scheduler_task
        args = ('["msg_rss_channel",    %s]' % channel_id)
        query = (((ttable.function_name == 'msg_poll') & (ttable.args == args)) & ttable.status.belongs(['RUNNING', 'QUEUED', 'ALLOCATED']))
        exists = db(query).select(ttable.id, ttable.period, limitby=(0, 1)).first()
        if (not exists):
            return
        old_period = exists.period
        max_period = period[1]
        if (old_period < max_period):
            new_period = (old_period + period[0])
            new_period = min(new_period, max_period)
            db((ttable.id == exists.id)).update(period=new_period)
