'Helper method to find all subscriptions which need to be
notified now.
@param now: current datetime (UTC)
@return: joined Rows pr_subscription/pr_subscription_resource,
or None if no due subscriptions could be found
@todo: take notify_on into account when checking'

@classmethod
def _subscriptions(cls, now):

    db = current.db
    s3db = current.s3db
    stable = s3db.pr_subscription
    rtable = db.pr_subscription_resource
    query = ((((rtable.next_check_time == None) | (rtable.next_check_time <= now)) & (rtable.locked != True)) & (rtable.deleted != True))
    tname = rtable.resource
    mtime = rtable.last_check_time.min()
    rows = db(query).select(tname, mtime, groupby=tname)
    resources = set()
    radd = resources.add
    for row in rows:
        tablename = row[tname]
        table = s3db.table(tablename)
        if ((not table) or (not ('modified_on' in table.fields))):
            continue
        else:
            modified_on = table.modified_on
        msince = row[mtime]
        if (msince is None):
            query = (table.id > 0)
        else:
            query = (modified_on >= msince)
        update = db(query).select(modified_on, orderby=(~ modified_on), limitby=(0, 1)).first()
        if update:
            radd((tablename, update.modified_on))
    if resources:
        join = rtable.on((((rtable.subscription_id == stable.id) & (rtable.locked != True)) & (rtable.deleted != True)))
        query = None
        for (rname, modified_on) in resources:
            q = ((rtable.resource == rname) & ((rtable.last_check_time == None) | (rtable.last_check_time <= modified_on)))
            if (query is None):
                query = q
            else:
                query |= q
        query = ((((stable.frequency != 'never') & (stable.deleted != True)) & ((rtable.next_check_time == None) | (rtable.next_check_time <= now))) & query)
        return db(query).select(rtable.id, join=join)
    else:
        return None
