Exemple #1
0
def chooser(request):
    Image = get_image_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm(user=request.user)
    else:
        uploadform = None

    images = Image.objects.order_by('-created_at')

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
        images = hook(images, request)

    if (
        'q' in request.GET or 'p' in request.GET or 'tag' in request.GET or
        'collection_id' in request.GET
    ):
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment
        collection_id = request.GET.get('collection_id')
        if collection_id:
            images = images.filter(collection=collection_id)

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

            images = images.search(q)
            is_searching = True
        else:
            is_searching = False
            q = None

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

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(request, "wagtailimages/chooser/results.html", {
            'images': images,
            'is_searching': is_searching,
            'query_string': q,
            'will_select_format': request.GET.get('select_format')
        })
    else:
        paginator, images = paginate(request, images, per_page=12)

        context = get_chooser_context(request)
        context.update({
            'images': images,
            'uploadform': uploadform,
        })
        return render_modal_workflow(
            request, 'wagtailimages/chooser/chooser.html', None, context,
            json_data=get_chooser_js_data()
        )
Exemple #2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

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

        Image = get_image_model()

        context.update({
            "search_form":
            self.form,
            "popular_tags":
            popular_tags_for_model(get_image_model()),
            "current_tag":
            self.current_tag,
            "collections":
            collections,
            "current_collection":
            self.current_collection,
            "user_can_add":
            permission_policy.user_has_permission(self.request.user, "add"),
            "app_label":
            Image._meta.app_label,
            "model_name":
            Image._meta.model_name,
        })
        return context
Exemple #3
0
def chooser(request):
    Image = get_image_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm(user=request.user)
    else:
        uploadform = None

    images = Image.objects.order_by('-created_at')

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
        images = hook(images, request)

    if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET
            or 'collection_id' in request.GET):
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment
        collection_id = request.GET.get('collection_id')
        if collection_id:
            images = images.filter(collection=collection_id)

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

            images = images.search(q)
            is_searching = True
        else:
            is_searching = False
            q = None

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

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(
            request, "wagtailimages/chooser/results.html", {
                'images': images,
                'is_searching': is_searching,
                'query_string': q,
                'will_select_format': request.GET.get('select_format')
            })
    else:
        paginator, images = paginate(request, images, per_page=12)

        context = get_chooser_context(request)
        context.update({
            'images': images,
            'uploadform': uploadform,
        })
        return render_modal_workflow(request,
                                     'wagtailimages/chooser/chooser.html',
                                     None,
                                     context,
                                     json_data=get_chooser_js_data())
Exemple #4
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

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

        Image = get_image_model()

        context.update({
            'search_form':
            self.form,
            'popular_tags':
            popular_tags_for_model(get_image_model()),
            'current_tag':
            self.current_tag,
            'collections':
            collections,
            'current_collection':
            self.current_collection,
            'user_can_add':
            permission_policy.user_has_permission(self.request.user, 'add'),
            'app_label':
            Image._meta.app_label,
            'model_name':
            Image._meta.model_name,
        })
        return context
Exemple #5
0
def index(request):
    Image = get_image_model()

    # Get images (filtered by user permission)
    images = 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']

            images = images.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)
            images = images.filter(collection=current_collection)
        except (ValueError, Collection.DoesNotExist):
            pass

    paginator = Paginator(images, per_page=20)
    images = paginator.get_page(request.GET.get('p'))

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

    # Create response
    if request.is_ajax():
        return render(request, 'wagtailimages/images/results.html', {
            'images': images,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtailimages/images/index.html', {
            'images': images,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': popular_tags_for_model(Image),
            'collections': collections,
            'current_collection': current_collection,
            'user_can_add': permission_policy.user_has_permission(request.user, 'add'),
        })
    def post(self, request, *args, **kwargs):
        """
        Create a new Image
        """
        # Ensure that the user is allowed to add images
        if not permission_policy.user_has_permission(request.user, 'add'):
            return HttpResponseForbidden()

        return self.create(request, *args, **kwargs)
Exemple #7
0
    def get_context_data(self):
        context = super().get_context_data()

        if permission_policy.user_has_permission(self.request.user, "add"):
            ImageForm = get_image_form(self.image_model)
            uploadform = ImageForm(user=self.request.user,
                                   prefix="image-chooser-upload")
        else:
            uploadform = None

        collections = permission_policy.collections_user_has_permission_for(
            self.request.user, "choose")
        if len(collections) < 2:
            collections = None

        context.update({
            "searchform": self.search_form,
            "popular_tags": popular_tags_for_model(self.image_model),
            "collections": collections,
            "uploadform": uploadform,
        })
        return context
Exemple #8
0
    def get_context_data(self):
        context = super().get_context_data()

        if permission_policy.user_has_permission(self.request.user, 'add'):
            ImageForm = get_image_form(self.image_model)
            uploadform = ImageForm(user=self.request.user, prefix='image-chooser-upload')
        else:
            uploadform = None

        collections = permission_policy.collections_user_has_permission_for(
            self.request.user, 'choose'
        )
        if len(collections) < 2:
            collections = None

        context.update({
            'searchform': self.search_form,
            'popular_tags': popular_tags_for_model(self.image_model),
            'collections': collections,
            'uploadform': uploadform,
        })
        return context
Exemple #9
0
def chooser(request):
    Image = get_image_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm(user=request.user)
    else:
        uploadform = None

    will_select_format = request.GET.get('select_format')
    will_select_rendition = request.GET.get('select_rendition')

    images = Image.objects.order_by('-created_at') \
                          .filter(show_in_catalogue=True)

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
        images = hook(images, request)

    q = None
    if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET
            or 'collection_id' in request.GET):
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment
        collection_id = request.GET.get('collection_id')
        if collection_id:
            images = images.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']
            images = images.search(q)
            is_searching = True
        else:
            is_searching = False

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

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(
            request, "wagtailimages/chooser/results.html", {
                'images': images,
                'is_searching': is_searching,
                'query_string': q,
                'will_select_format': will_select_format,
                'will_select_rendition': will_select_rendition,
                'post_processing_spec': request.GET.get('pps'),
                'additional_params': get_cropper_params(request),
            })
    else:
        searchform = SearchForm()

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

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

        return render_modal_workflow(
            request, 'wagtailimages/chooser/chooser.html',
            'wagtailimages/chooser/chooser.js', {
                'images':
                images,
                'uploadform':
                uploadform,
                'searchform':
                searchform,
                'is_searching':
                False,
                'query_string':
                q,
                'will_select_format':
                will_select_format,
                'will_select_rendition':
                will_select_rendition,
                'popular_tags':
                popular_tags_for_model(Image),
                'collections':
                collections,
                'uploadid':
                uuid.uuid4(),
                'post_processing_spec':
                request.GET.get('pps'),
                'additional_params':
                get_cropper_params(request),
                'allowed_extensions':
                ALLOWED_EXTENSIONS,
                'error_max_file_size':
                uploadform.fields['file'].
                error_messages['file_too_large_unknown_size']
                if uploadform else None,
                'error_accepted_file_types':
                uploadform.fields['file'].error_messages['invalid_image']
                if uploadform else None,
            })
Exemple #10
0
def chooser(request):
    Image = get_image_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm(user=request.user)
    else:
        uploadform = None

    will_select_format = request.GET.get('select_format')
    will_select_rendition = request.GET.get('select_rendition')

    images = Image.objects.order_by('-created_at') \
                          .filter(show_in_catalogue=True)

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
        images = hook(images, request)

    q = None
    if (
        'q' in request.GET or 'p' in request.GET or 'tag' in request.GET or
        'collection_id' in request.GET
    ):
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment
        collection_id = request.GET.get('collection_id')
        if collection_id:
            images = images.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']
            images = images.search(q)
            is_searching = True
        else:
            is_searching = False

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

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(request, "wagtailimages/chooser/results.html", {
            'images': images,
            'is_searching': is_searching,
            'query_string': q,
            'will_select_format': will_select_format,
            'will_select_rendition': will_select_rendition,
            'post_processing_spec': request.GET.get('pps'),
            'additional_params': get_cropper_params(request),
        })
    else:
        searchform = SearchForm()

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

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

        return render_modal_workflow(request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', {
            'images': images,
            'uploadform': uploadform,
            'searchform': searchform,
            'is_searching': False,
            'query_string': q,
            'will_select_format': will_select_format,
            'will_select_rendition': will_select_rendition,
            'popular_tags': popular_tags_for_model(Image),
            'collections': collections,
            'uploadid': uuid.uuid4(),
            'post_processing_spec': request.GET.get('pps'),
            'additional_params': get_cropper_params(request),
            'allowed_extensions': ALLOWED_EXTENSIONS,
            'error_max_file_size':
                uploadform.fields['file']
                          .error_messages['file_too_large_unknown_size']
                            if uploadform else None,
            'error_accepted_file_types':
                uploadform.fields['file'].error_messages['invalid_image']
                            if uploadform else None,
        })