'Get current subscription settings'

def _get_subscription(self):

    db = current.db
    s3db = current.s3db
    pe_id = current.auth.user.pe_id
    stable = s3db.pr_subscription
    ftable = s3db.pr_filter
    query = ((stable.pe_id == pe_id) & (stable.deleted != True))
    left = ftable.on((ftable.id == stable.filter_id))
    row = db(query).select(stable.id, stable.notify_on, stable.frequency, ftable.id, ftable.query, left=left, limitby=(0, 1)).first()
    output = {'pe_id': pe_id}
    get_vars = {}
    if row:
        s = getattr(row, 'pr_subscription')
        f = getattr(row, 'pr_filter')
        rtable = s3db.pr_subscription_resource
        query = ((rtable.subscription_id == s.id) & (rtable.deleted != True))
        rows = db(query).select(rtable.id, rtable.resource, rtable.url, rtable.last_check_time, rtable.next_check_time)
        if f.query:
            import json
            filters = json.loads(f.query)
            for (k, v) in filters:
                if (v is None):
                    continue
                if (k in get_vars):
                    if (type(get_vars[k]) is list):
                        get_vars[k].append(v)
                    else:
                        get_vars[k] = [get_vars[k], v]
                else:
                    get_vars[k] = v
        output.update({'id': s.id, 'filter_id': f.id, 'get_vars': get_vars, 'resources': rows, 'notify_on': s.notify_on, 'frequency': s.frequency, 'method': ['EMAIL']})
    else:
        output.update({'id': None, 'filter_id': None, 'get_vars': get_vars, 'resources': None, 'notify_on': stable.notify_on.default, 'frequency': stable.frequency.default, 'method': ['EMAIL']})
    return output
