'Update subscription settings'

def update_subscription(self):

    db = current.db
    s3db = current.s3db
    subscription = self.subscription
    pe_id = subscription['pe_id']
    filter_id = subscription['filter_id']
    filters = subscription.get('filters')
    if filters:
        ftable = s3db.pr_filter
        if (not filter_id):
            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
    stable = s3db.pr_subscription
    subscription_id = subscription['id']
    frequency = subscription['frequency']
    email_format = subscription['email_format']
    if (not subscription_id):
        success = stable.insert(pe_id=pe_id, filter_id=filter_id, notify_on=subscription['notify_on'], frequency=frequency, email_format=email_format, method=subscription['method'])
        subscription_id = success
    else:
        success = db((stable.id == subscription_id)).update(pe_id=pe_id, filter_id=filter_id, notify_on=subscription['notify_on'], frequency=frequency, email_format=email_format, method=subscription['method'])
    if (not success):
        return None
    rtable = s3db.pr_subscription_resource
    subscribe = subscription.get('subscribe')
    if subscribe:
        from datetime import datetime, timedelta
        now = datetime.utcnow()
        resources = subscription['resources']
        subscribed = {}
        timestamps = {}
        if resources:
            for r in resources:
                subscribed[(r.resource, r.url)] = r.id
                timestamps[r.id] = (r.last_check_time, r.next_check_time)
        intervals = s3db.pr_subscription_check_intervals
        interval = timedelta(minutes=intervals.get(frequency, 0))
        keep = set()
        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)
                keep.add(record_id)
        unsubscribe = (set(subscribed.values()) - keep)
        db(rtable.id.belongs(unsubscribe)).update(deleted=True, deleted_fk=fk, subscription_id=None)
    subscription['id'] = subscription_id
    subscription['filter_id'] = filter_id
    return subscription
