Пример #1
0
def ajax_get_tree_debug(request: HttpRequest):
    def human_format(number):
        units = ['', 'K', 'M', 'G', 'T', 'P']
        k = 1000.0
        magnitude = int(floor(log(number, k)))
        if magnitude > 0:
            return '{0:.2}{1:s}'.format(number / k**magnitude, units[magnitude])
        else:
            return '{0}'.format(number)

    def visit(node):
        if isinstance(node, SubscriptionFolder):
            unwatched = node.get_unwatched_count()
            return {
                "id": __tree_folder_id(node.id),
                "text": node.name,
                "type": "folder",
                "state": {"opened": True},
                "parent": __tree_folder_id(node.parent_id),
                "li_attr": {"data-unwatched-count": unwatched}
            }
        elif isinstance(node, Subscription):
            unwatched = node.get_unwatched_count()
            return {
                "id": __tree_sub_id(node.id),
                "type": "sub",
                "text": node.name,
                "icon": node.thumb.url if node.thumb else None,
                "parent": __tree_folder_id(node.parent_folder_id),
                "li_attr": {"data-unwatched-count": unwatched}
            }

    result = SubscriptionFolder.traverse(None, request.user, visit)
    return HttpResponse(json.dumps(result))
Пример #2
0
def get_videos(
    user: User,
    sort_order: Optional[str],
    query: Optional[str] = None,
    subscription_id: Optional[int] = None,
    folder_id: Optional[int] = None,
    only_watched: Optional[bool] = None,
    only_downloaded: Optional[bool] = None,
):

    filter_args = []
    filter_kwargs = {'subscription__user': user}

    # Process query string - basically, we break it down into words,
    # and then search for the given text in the name, description, uploader name and subscription name
    if query is not None:
        for match in re.finditer(r'\w+', query):
            word = match[0]
            filter_args.append(
                Q(name__icontains=word)
                | Q(description__icontains=word)
                | Q(uploader_name__icontains=word)
                | Q(subscription__name__icontains=word))

    # Subscription id
    if subscription_id is not None:
        filter_kwargs['subscription_id'] = subscription_id

    # Folder id
    if folder_id is not None:
        # Visit function - returns only the subscription IDs
        def visit(node):
            if isinstance(node, Subscription):
                return node.id
            return None

        filter_kwargs['subscription_id__in'] = SubscriptionFolder.traverse(
            folder_id, user, visit)

    # Only watched
    if only_watched is not None:
        filter_kwargs['watched'] = only_watched

    # Only downloaded
    # - not downloaded (False) -> is null (True)
    # - downloaded (True) -> is not null (False)
    if only_downloaded is not None:
        filter_kwargs['downloaded_path__isnull'] = not only_downloaded

    return Video.objects.filter(*filter_args,
                                **filter_kwargs).order_by(sort_order)
Пример #3
0
def ajax_get_tree(request: HttpRequest):
    def visit(node):
        if isinstance(node, SubscriptionFolder):
            return {
                "id": __tree_folder_id(node.id),
                "text": node.name,
                "type": "folder",
                "state": {
                    "opened": True
                },
                "parent": __tree_folder_id(node.parent_id)
            }
        elif isinstance(node, Subscription):
            return {
                "id": __tree_sub_id(node.id),
                "type": "sub",
                "text": node.name,
                "icon": node.icon_default,
                "parent": __tree_folder_id(node.parent_folder_id)
            }

    result = SubscriptionFolder.traverse(None, request.user, visit)
    return JsonResponse(result, safe=False)