Beispiel #1
0
def sessions_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'user': '',
        'remote_origin': '',
        'touched_before': '',
        'touched_after': '',
        'closed_before': '',
        'closed_after': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
        'touched_before',
        'touched_after',
        'closed_before',
        'closed_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'user' == field:
            filter['user_ids'] = value
        elif 'remote_origin' == field:
            filter['with_remote_origins'] = value

    pagination = pagination_from_request('touch_time', 'desc', 0, 32)

    total_results = g.accounts.count_sessions(filter=filter)
    results = g.accounts.search_sessions(filter=filter, **pagination)

    populate_sessions_useragents(results)

    return render_template(
        'sessions_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
    )
Beispiel #2
0
def clients_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'access_token_expired_before': '',
        'access_token_expired_after': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    escape = lambda value: (value.replace('\\', '\\\\').replace('_', '\_').
                            replace('%', '\%').replace('-', '\-'))
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
        'access_token_expired_before',
        'access_token_expired_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'client_id' == field:
            filter['client_ids'] = value
        elif 'creation_name' == field:
            filter['creation_names'] = '%' + escape(value) + '%'

    pagination = pagination_from_request('creation_time', 'desc', 0, 32)

    total_results = g.patreon.count_clients(filter=filter)
    results = g.patreon.search_clients(filter=filter, **pagination)

    for client in results.values():
        g.patreon.populate_client_tiers(client)
        g.patreon.populate_client_benefits(client)
        client.total_members = g.patreon.count_members(
            filter={'client_ids': client.id_bytes}, )

    return render_template(
        'patreon_clients_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
    )
Beispiel #3
0
def users_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'name': '',
        'display': '',
        'status': '',
        'protection': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    escape = lambda value: (value.replace('\\', '\\\\').replace('_', '\_').
                            replace('%', '\%').replace('-', '\-'))
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'name' == field:
            filter['names'] = '%' + escape(value) + '%'
        elif 'display' == field:
            filter['displays'] = '%' + escape(value) + '%'
        elif 'status' == field:
            filter['statuses'] = value
        elif 'protection' == field:
            filter['protection'] = ('protected' == value)

    pagination = pagination_from_request('last_seen_time', 'desc', 0, 32)

    total_results = g.accounts.count_users(filter=filter)
    results = g.accounts.search_users(filter=filter, **pagination)

    return render_template(
        'users_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
    )
Beispiel #4
0
def members_list():
    search = {
        'id': '',
        'client_id': '',
        'campaign_id': '',
        'tier_id': '',
        'name': '',
        'amount_cents_more_than': '',
        'amount_cents_less_than': '',
        'last_charged_before': '',
        'last_fulfilled_before': '',
        'last_fulfilled_after': '',
        'last_charged_after': '',
        'last_charge_status': '',
        'lifetime_support_cents_more_than': '',
        'lifetime_support_cents_less_than': '',
        'pledged_before': '',
        'pledged_after': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    escape = lambda value: (value.replace('\\', '\\\\').replace('_', '\_').
                            replace('%', '\%').replace('-', '\-'))
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'last_fulfilled_before',
        'last_fulfilled_after',
        'last_charged_before',
        'last_charged_after',
        'pledged_before',
        'pledged_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'client_id' == field:
            filter['client_ids'] = value
        elif 'campaign_id' == field:
            filter['campaign_ids'] = value
        elif 'tier_id' == field:
            filter['tier_ids'] = value
        elif 'name' == field:
            filter['names'] = '%' + escape(value) + '%'
        elif 'last_charge_status' == field:
            filter['last_charge_status'] = value

    pagination = pagination_from_request('pledge_relationship_start_time',
                                         'desc', 0, 32)

    total_results = g.patreon.count_members(filter=filter)
    results = g.patreon.search_members(filter=filter, **pagination)

    client_ids = []
    patron_ids = []
    for result in results.values():
        if result.client_id not in client_ids:
            client_ids.append(result.client_id)
        if result.user_id not in patron_ids:
            patron_ids.append(result.user_id)
    clients = g.patreon.search_clients(filter={'ids': client_ids}, )
    for client in clients.values():
        g.patreon.populate_client_tiers(client)
    authentications = g.patreon.accounts.search_authentications(
        filter={'values': patron_ids}, )
    patron_ids_to_users = {}
    for authentication in authentications.values():
        patron_ids_to_users[int(authentication.value)] = authentication.user
    return render_template(
        'patreon_members_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
        clients=clients,
        patron_ids_to_users=patron_ids_to_users,
    )
Beispiel #5
0
def auto_permissions_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'created_by_user': '',
        'duration_shorter_than': '',
        'duration_longer_than': '',
        'scope': '',
        'user': '',
        'valid_from': '',
        'valid_until': '',
    }
    for group_name in g.accounts.available_groups:
        search.update({'group_' + group_name: ''})
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    group_bits = 0
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
        'valid_from',
        'valid_until',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'created_by_user' == field:
            if 'system' == value:
                value = ''
            filter['created_by_user_ids'] = value
        elif 'duration_longer_than' == field:
            filter['duration_longer_than'] = value
        elif 'duration_shorter_than' == field:
            filter['duration_shorter_than'] = value
        elif 'scope' == field:
            if 'global' == value:
                value = ''
            filter['scopes'] = value
        elif 'user' == field:
            filter['user_ids'] = value
        elif 'group_' == field[:6]:
            group_bits = g.accounts.combine_groups(bits=[group_bits],
                                                   names=[field[6:]])
    if group_bits:
        filter['with_group_bits'] = group_bits

    pagination = pagination_from_request('creation_time', 'desc', 0, 32)

    total_results = g.accounts.count_auto_permissions(filter=filter)
    results = g.accounts.search_auto_permissions(filter=filter, **pagination)

    return render_template(
        'auto_permissions_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
    )
Beispiel #6
0
def manage_likes():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'user_id': '',
        'medium_id': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'user_id' == field:
            filter['user_ids'] = value
        elif 'medium_id' == field:
            filter['medium_ids'] = value

    pagination = pagination_from_request('creation_time', 'desc', 0, 32)

    total_results = g.media.count_likes(filter=filter)
    results = g.media.search_likes(filter=filter, **pagination)

    user_ids = []
    medium_ids = []
    for result in results.values():
        user_ids.append(result.user_id)
        medium_ids.append(result.medium_id)

    users = g.accounts.search_users(filter={'ids': user_ids})
    media = g.media.search_media(filter={'ids': medium_ids})

    for result in results.values():
        if result.user_id in users:
            result.user = users.get(result.user_id)
        if result.medium_id in media:
            result.medium = media.get(result.medium_id)

    return render_template(
        'likes_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
    )
Beispiel #7
0
def logs_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'scope': '',
        'remote_origin': '',
        'subject_id': '',
        'object_id': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
    ]
    for field, value in search.items():
        if value:
            if 'id' == field:
                filter['ids'] = value
            elif field in time_fields:
                try:
                    parsed = dateutil.parser.parse(value)
                except ValueError:
                    filter[field] = 'bad_query'
                else:
                    search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                    filter[field] = parsed.timestamp()
            elif 'scope' == field:
                filter['scopes'] = value
            elif 'remote_origin' == field:
                filter['with_remote_origins'] = value
            elif 'subject_id' == field:
                if 'system' == value:
                    value = ''
                filter['subject_ids'] = value
            elif 'object_id' == field:
                filter['object_ids'] = value

    pagination = pagination_from_request('creation_time', 'desc', 0, 32)

    total_results = g.access_log.count_logs(filter=filter)
    results = g.access_log.search_logs(filter=filter, **pagination)

    # enhanced search for other modules
    ids = []
    int_ids = []
    for log in results.values():
        if log.subject_id:
            ids.append(log.subject_id)
        if log.object_id:
            ids.append(log.object_id)
    collections = {
        'user': g.accounts.search_users(filter={'ids': ids}),
        'authentication':
        g.accounts.search_authentications(filter={'ids': ids}, ),
        'session': g.accounts.search_sessions(filter={'ids': ids}),
        'invite': g.accounts.search_invites(filter={'ids': ids}),
        'auto_permission':
        g.accounts.search_auto_permissions(filter={'ids': ids}, ),
        'ban': g.bans.search_bans(filter={'ids': ids}),
        'medium': g.media.search_media(filter={'ids': ids}),
        'medium_like': g.media.search_likes(filter={'ids': ids}),
    }
    if g.persephone_config['optional_packages']['comments']:
        collections['comment'] = g.comments.search_comments(
            filter={'ids': ids})
    if g.persephone_config['optional_packages']['stickers']:
        collections['sticker'] = g.stickers.search_stickers(
            filter={'ids': ids})
        collections[
            'collected_sticker'] = g.stickers.search_collected_stickers(
                filter={'ids': ids}, )
        collections[
            'sticker_placement'] = g.stickers.search_sticker_placements(
                filter={'ids': ids}, )
    if g.persephone_config['optional_packages']['patreon']:
        collections['patreon_client'] = g.patreon.search_clients(
            filter={'ids': ids})

    for log in results.values():
        for category, collection in collections.items():
            if log.subject_id in collection:
                log.subject = collection.get(log.subject_id)
                log.subject_category = category
            if log.object_id in collection:
                log.object = collection.get(log.object_id)
                log.object_category = category

    return render_template(
        'logs_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
        unique_scopes=g.access_log.get_unique_scopes(),
    )
Beispiel #8
0
def stickers_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'name': '',
        'display': '',
        'category': '',
        #'category_order_more_than': '',
        #'category_order_less_than': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    escape = lambda value: (value.replace('\\', '\\\\').replace('_', '\_').
                            replace('%', '\%').replace('-', '\-'))
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'name' == field:
            filter['name'] = '%' + escape(value) + '%'
        elif 'display' == field:
            filter['display'] = '%' + escape(value) + '%'
        elif 'category' == field:
            filter['category'] = escape(value)

    pagination = pagination_from_request('creation_time', 'desc', 0, 32)

    total_results = g.stickers.count_stickers(filter=filter)
    results = g.stickers.search_stickers(filter=filter, **pagination)

    #TODO this is probably fine for now since the total number of stickers
    #TODO will probably remain small, and the admin panels will probably be
    #TODO accessed infrequently
    #TODO but eventually add another count method for multiple sticker_ids
    #TODO to stickers module so that this can be done in just two queries total
    #TODO instead of two queries per row
    for result in results.values():
        result.total_collected = g.stickers.count_collected_stickers(
            filter={'sticker_ids': result.id_bytes}, )
        result.total_placed = 0
        result.total_placed = g.stickers.count_sticker_placements(
            filter={'sticker_ids': result.id_bytes}, )

    categories = g.stickers.config['categories']
    unique_categories = g.stickers.get_unique_categories()
    for category in unique_categories:
        if category not in categories:
            categories.append(category)
    return render_template(
        'stickers_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
        unique_categories=categories,
    )
Beispiel #9
0
def bans_list():
    search = {
        'id': '',
        'created_before': '',
        'created_after': '',
        'remote_origin': '',
        'scope': '',
        'reason': '',
        'note': '',
        'expired_before': '',
        'expired_after': '',
        'viewed_after': '',
        'viewed_before': '',
        'created_by_user_id': '',
        'user_id': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    escape = lambda value: (value.replace('\\', '\\\\').replace('_', '\_').
                            replace('%', '\%').replace('-', '\-'))
    # for parsing datetime and timestamp from submitted form
    # filter fields are named the same as search fields
    time_fields = [
        'created_before',
        'created_after',
        'expired_before',
        'expired_after',
        'viewed_before',
        'viewed_after',
    ]
    for field, value in search.items():
        if not value:
            continue
        if 'id' == field:
            filter['ids'] = value
        elif field in time_fields:
            try:
                parsed = dateutil.parser.parse(value)
            except ValueError:
                filter[field] = 'bad_query'
            else:
                search[field] = parsed.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
                filter[field] = parsed.timestamp()
        elif 'remote_origin' == field:
            filter['with_remote_origins'] = value
        elif 'scope' == field:
            if 'global' == value:
                value = ''
            filter['scopes'] = value
        elif 'reason' == field:
            filter['reasons'] = '%' + escape(value) + '%'
        elif 'note' == field:
            filter['notes'] = '%' + escape(value) + '%'
        elif 'created_by_user_id' == field:
            if 'system' == value:
                value = ''
            filter['created_by_user_ids'] = value
        elif 'user_id' == field:
            filter['user_ids'] = value

    pagination = pagination_from_request('creation_time', 'desc', 0, 32)

    total_results = g.bans.count_bans(filter=filter)
    results = g.bans.search_bans(filter=filter, **pagination)

    # enhanced search for users
    ids = []
    for ban in results.values():
        if ban.created_by_user_id:
            ids.append(ban.created_by_user_id)
        if ban.user_id:
            ids.append(ban.user_id)
    users = g.bans.accounts.search_users(filter={'ids': ids})
    for ban in results.values():
        if ban.created_by_user_id in users:
            ban.created_by_user = users.get(ban.created_by_user_id)
        if ban.user_id in users:
            ban.user = users.get(ban.user_id)

    return render_template(
        'bans_list.html',
        results=results,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
        unique_scopes=g.bans.get_unique_scopes(),
    )
Beispiel #10
0
def tags_list(search_endpoint=''):
    if 'mode' in request.args:
        if 'redirect_uri' in request.args:
            redirect_uri = request.args['redirect_uri']
        else:
            redirect_uri = url_for(request.endpoint)
        if 'generate_suggestions' == request.args['mode']:
            g.media.build_tag_suggestions(manual=True)
        elif ('tag' in request.args and request.args['tag']):
            tag = request.args['tag']
            if 'remove' == request.args['mode']:
                if 'confirm' not in request.args:
                    return render_template(
                        'confirm_remove_tag.html',
                        tag=tag,
                        redirect_uri=redirect_uri,
                    )
                g.media.delete_tags(tag)
            else:
                media = g.media.search_media(filter={'with_tags': tag})
                medium_ids = list(media.keys())
                if 'replace' == request.args['mode']:
                    if ('replacement' not in request.args
                            or not request.args['replacement']):
                        return render_template(
                            'replace_tag.html',
                            tag=request.args['tag'],
                            redirect_uri=redirect_uri,
                        )
                    g.media.delete_tags(tag)
                    g.media.add_tags(medium_ids, request.args['replacement'])
                elif 'accompany' == request.args['mode']:
                    if ('accompaniment' not in request.args
                            or not request.args['accompaniment']):
                        return render_template(
                            'accompany_tag.html',
                            tag=request.args['tag'],
                            redirect_uri=redirect_uri,
                        )
                    g.media.add_tags(medium_ids, request.args['accompaniment'])
        return redirect(
            redirect_uri,
            code=303,
        )

    search = {
        'tag': '',
    }
    for field in search:
        if field in request.args:
            search[field] = request.args[field]

    filter = {}
    escape = lambda value: (value.replace('\\', '\\\\').replace('_', '\_').
                            replace('%', '\%').replace('-', '\-'))
    for field, value in search.items():
        if not value:
            continue
        if 'tag' == field:
            filter['tags'] = '%' + escape(value) + '%'

    pagination = pagination_from_request('count', 'desc', 0, 32)

    total_results = g.media.count_unique_tags(filter=filter)
    results = g.media.search_tag_counts(filter=filter, **pagination)

    results_dict = {}
    i = 0
    for result in results:
        results_dict.update({i: result})
        i += 1
    return render_template(
        'tags_list.html',
        results=results_dict,
        search=search,
        pagination=pagination,
        total_results=total_results,
        total_pages=math.ceil(total_results / pagination['perpage']),
        search_endpoint=search_endpoint,
    )