'Update Admin subscription'

def _update_admin_subscription(self, subscription, pe_id, alert_editor_id, user_id=None, group_id=None, filter_id=None, subscription_id=None):

    db = current.db
    s3db = current.s3db
    stable = s3db.pr_subscription
    filters = subscription.get('filters')
    if (user_id is not None):
        comment_filter = {'auth_user.id': str(user_id)}
    elif (group_id is not None):
        comment_filter = {'pr_group.id': str(group_id)}
    else:
        comment_filter = None
    ftable = s3db.pr_filter
    filters = subscription.get('filters')
    filters = json.loads(filters)
    for filter in filters:
        if (filter[0] == 'language__belongs'):
            filter[0] = 'info.language__belongs'
        if (filter[0] == 'priority__belongs'):
            filter[0] = 'info.priority__belongs'
    scope_filter = ['scope__belongs', 'Public,Restricted']
    system_alert_filter = ['external__ne', 'True']
    filters.append(scope_filter)
    filters.append(system_alert_filter)
    filters_ = [filter for filter in filters if (filter[0] != 'id__belongs')]
    filters_ = json.dumps(filters_)
    if (filter_id is None):
        success = ftable.insert(pe_id=pe_id, query=filters_)
        filter_id = success
    else:
        success = db((ftable.id == filter_id)).update(query=filters_)
    if (not success):
        return None
    frequency = subscription['frequency']
    if (subscription_id is None):
        if (comment_filter is not None):
            comment_filter = json.dumps(comment_filter)
            success = stable.insert(pe_id=pe_id, filter_id=filter_id, notify_on=subscription['notify_on'], frequency=frequency, method=subscription['method'], comments=comment_filter, owned_by_group=alert_editor_id)
        else:
            success = stable.insert(pe_id=pe_id, filter_id=filter_id, notify_on=subscription['notify_on'], frequency=frequency, method=subscription['method'], owned_by_group=alert_editor_id)
        subscription_id = success
    elif (comment_filter is not None):
        comment_filter = json.dumps(comment_filter)
        success = db((stable.id == subscription_id)).update(pe_id=pe_id, filter_id=filter_id, notify_on=subscription['notify_on'], frequency=frequency, method=subscription['method'], comments=comment_filter, owned_by_group=alert_editor_id)
    else:
        success = db((stable.id == subscription_id)).update(pe_id=pe_id, filter_id=filter_id, notify_on=subscription['notify_on'], frequency=frequency, method=subscription['method'], owned_by_group=alert_editor_id)
    if (not success):
        return None
    rtable = s3db.pr_subscription_resource
    subscribe = subscription.get('subscribe')
    if subscribe:
        subscribed = {}
        timestamps = {}
        intervals = s3db.pr_subscription_check_intervals
        interval = timedelta(minutes=intervals.get(frequency, 0))
        now = datetime.utcnow()
        row = db((rtable.subscription_id == subscription_id)).select(limitby=(0, 1)).first()
        if row:
            subscribed[(row.resource, row.url)] = row.id
            timestamps[row.id] = (row.last_check_time, row.next_check_time)
        fk = ('{"subscription_id":    %s}' % subscription_id)
        for new in subscribe:
            (resource, url) = (new['resource'], new['url'])
            if ((resource, url) not in subscribed):
                unsubscribed = {'deleted': True, 'deleted_fk': fk, 'resource': resource, 'url': url}
                rtable.update_or_insert(_key=unsubscribed, deleted=False, deleted_fk=None, subscription_id=subscription_id, resource=resource, url=url, last_check_time=now, next_check_time=None)
            else:
                record_id = subscribed[(resource, url)]
                (last_check_time, next_check_time) = timestamps[record_id]
                data = {}
                if (not last_check_time):
                    last_check_time = now
                    data['last_check_time'] = last_check_time
                due = (last_check_time + interval)
                if (next_check_time != due):
                    data['next_check_time'] = due
                if data:
                    db((rtable.id == record_id)).update(**data)
    subscription['id'] = subscription_id
    subscription['filter_id'] = filter_id
    return subscription
