def index(request):
    EmbedVideo = get_embed_video_model()

    # Get embed videos
    embed_videos = permission_policy.instances_user_has_any_permission_for(
        request.user, ['change', 'delete']
    ).order_by('-created_at')

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search images"))
        if form.is_valid():
            query_string = form.cleaned_data['q']

            embed_videos = embed_videos.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search images"))

    # Filter by collection
    current_collection = None
    collection_id = request.GET.get('collection_id')
    if collection_id:
        try:
            current_collection = Collection.objects.get(id=collection_id)
            embed_videos = embed_videos.filter(collection=current_collection)
        except (ValueError, Collection.DoesNotExist):
            pass

    # Pagination
    paginator, embed_videos = paginate(request, embed_videos)

    collections = permission_policy.collections_user_has_any_permission_for(
        request.user, ['add', 'change']
    )
    if len(collections) < 2:
        collections = None

    # Create response
    if request.is_ajax():
        return render(
            request,
            'wagtail_embed_videos/embed_videos/results.html',
            {
                'embed_videos': embed_videos,
                'query_string': query_string,
                'is_searching': bool(query_string),
            }
        )

    else:
        return render(
            request,
            'wagtail_embed_videos/embed_videos/index.html',
            {
                'embed_videos': embed_videos,
                'query_string': query_string,
                'is_searching': bool(query_string),

                'search_form': form,
                'popular_tags': popular_tags_for_model(EmbedVideo),
                'collections': collections,
                'current_collection': current_collection,
                'user_can_add': permission_policy.user_has_permission(request.user, 'add'),
            }
        )
Ejemplo n.º 2
0
def chooser(request):
    EmbedVideo = get_embed_video_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        EmbedVideoForm = get_embed_video_form(EmbedVideo)
        uploadform = EmbedVideoForm(user=request.user)
    else:
        uploadform = None

    embed_videos = EmbedVideo.objects.order_by('-created_at')

    q = None
    if 'q' in request.GET or 'p' in request.GET or 'tag' in request.GET or 'collection_id' in request.GET:
        collection_id = request.GET.get('collection_id')
        if collection_id:
            embed_videos = embed_videos.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            embed_videos = embed_videos.search(q)
            is_searching = True

        else:
            is_searching = False

            tag_name = request.GET.get('tag')
            if tag_name:
                embed_videos = embed_videos.filter(tags__name=tag_name)

        paginator, embed_videos = paginate(request, embed_videos, per_page=12)

        return render(request, "wagtail_embed_videos/chooser/results.html", {
            'embed_videos': embed_videos,
            'is_searching': is_searching,
            'query_string': q,
        })
    else:
        searchform = SearchForm()

        collections = Collection.objects.all()
        if len(collections) < 2:
            collections = None

        paginator, embed_videos = paginate(request, embed_videos, per_page=12)

    return render_modal_workflow(
        request,
        'wagtail_embed_videos/chooser/chooser.html',
        'wagtail_embed_videos/chooser/chooser.js',
        {
            'embed_videos': embed_videos,
            'uploadform': uploadform,
            'searchform': searchform,
            'is_searching': False,
            'query_string': q,
            'popular_tags': popular_tags_for_model(EmbedVideo),
            'collections': collections,
        }
    )