Skip to content

Commit

Permalink
Merge pull request #130 from neon-jungle/generic-index
Browse files Browse the repository at this point in the history
Generic index
  • Loading branch information
seb-b authored Aug 12, 2024
2 parents 2ba9372 + 1204cf4 commit 2486d58
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 180 deletions.
12 changes: 6 additions & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ head:
before_script:
- pip install .


lts_52:
image: python:3.11
extends: .python_test
before_script:
- pip install . wagtail~=5.2 django~=4.2
# Bring back with next LTS (6.3)
# lts_52:
# image: python:3.11
# extends: .python_test
# before_script:
# - pip install . wagtail~=5.2 django~=4.2

flake8:
stage: lint
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ html5 compliant codec using ffmpeg.
Requirements
------------

- Wagtail >= 5.2 (for older wagtail version see the tags)
- Wagtail >= 6.1 (for older wagtail version see the tags)
- `ffmpeg <https://ffmpeg.org/>`__ (optional, for transcoding)

Installing
Expand Down Expand Up @@ -187,3 +187,4 @@ Future features
- Some docs
- Richtext embed
- Transcoding via external service rather than ffmpeg
- Remove wagtail-modeladmin dependency
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
description-file = README.rst
description_file = README.rst

[bdist_wheel]
universal = 1
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

setup(
name="wagtailvideos",
version="6.1.2",
version="7.0.0",
description="A wagtail module for uploading and displaying videos in various codecs.",
long_description=readme,
author="Neon Jungle",
author_email="[email protected]",
url="https://github.com/neon-jungle/wagtailvideos",
install_requires=[
"wagtail>=5.2",
"wagtail>=6.1",
"Django>=3.2",
"bcp47==0.0.4",
"wagtail-modeladmin>=2.0.0"
Expand Down
16 changes: 6 additions & 10 deletions tests/test_admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ def test_search(self):
self.assertEqual(response.context["query_string"], "Hello")

def test_pagination(self):
pages = ["0", "1", "-1", "9999", "Not a page"]
for page in pages:
response = self.get({"p": page})
self.assertEqual(response.status_code, 200)

def test_ordering(self):
orderings = ["title", "-created_at"]
for ordering in orderings:
response = self.get({"ordering": ordering})
self.assertEqual(response.status_code, 200)
# page numbers in range should be accepted
response = self.get({"p": 1})
self.assertEqual(response.status_code, 200)
# page numbers out of range should return 404
response = self.get({"p": 9999})
self.assertEqual(response.status_code, 404)


class TestVideoAddView(TestCase, WagtailTestUtils):
Expand Down
4 changes: 3 additions & 1 deletion wagtailvideos/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class VideoChooserBlock(ChooserBlock):
@cached_property
def target_model(self):
from wagtailvideos import get_video_model

return get_video_model()

@cached_property
def widget(self):
from wagtailvideos.widgets import AdminVideoChooser

return AdminVideoChooser()

def render_basic(self, value, context=None):
Expand All @@ -20,4 +22,4 @@ def render_basic(self, value, context=None):
return ""

class Meta:
icon = 'media'
icon = "media"
2 changes: 1 addition & 1 deletion wagtailvideos/templates/wagtailvideos/multiple/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<form action="{% url 'wagtailvideos:add_multiple' %}" method="POST" enctype="multipart/form-data">
<div class="replace-file-input">
<button class="button bicolor icon icon-plus">{% trans "Or choose from your computer" %}</button>
<button class="button bicolor button--icon">{% icon name="plus" wrapped=1 %}{% trans "Or choose from your computer" %}</button>
<input id="fileupload" type="file" name="files[]" data-url="{% url 'wagtailvideos:add_multiple' %}" multiple>
</div>
{% csrf_token %}
Expand Down
53 changes: 26 additions & 27 deletions wagtailvideos/templates/wagtailvideos/videos/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@
{% block titletag %}{% trans "Add a video" %}{% endblock %}

{% block extra_js %}
{{ block.super }}

{% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
<script>
$(function() {
$('#id_tags').tagit({
autocomplete: {source: "{{ autocomplete_url|addslashes }}"}
});
});
</script>
{{ block.super }}
{% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
<script>
$(function () {
$("#id_tags").tagit({
autocomplete: { source: "{{ autocomplete_url|addslashes }}" },
});
});
</script>
{% endblock %}

{% block content %}
{% trans "Add video" as add_str %}
{% include "wagtailadmin/shared/header.html" with title=add_str icon="media" %}
{% trans "Add video" as add_str %}
{% include "wagtailadmin/shared/header.html" with title=add_str icon="media" %}

<div class="nice-padding">
<form action="{% url 'wagtailvideos:add' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<ul class="fields">
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
<li>{% include "wagtailadmin/shared/field.html" with field=field %}</li>
{% endif %}
{% endfor %}
<li><input class="button" type="submit" value="{% trans 'Save' %}" /></li>
</ul>
</form>
</div>
<div class="nice-padding">
<form action="{% url 'wagtailvideos:add' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<ul class="fields">
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
<li>{% include "wagtailadmin/shared/field.html" with field=field %}</li>
{% endif %}
{% endfor %}
<li><input class="button" type="submit" value="{% trans 'Save' %}" /></li>
</ul>
</form>
</div>
{% endblock %}
40 changes: 4 additions & 36 deletions wagtailvideos/templates/wagtailvideos/videos/index.html
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
{% extends "wagtailadmin/base.html" %}
{% extends "wagtailadmin/generic/index.html" %}
{% load i18n %}

{% block titletag %}{% trans "Videos" %}{% endblock %}
{% block extra_js %}
{{ block.super }}
<script>
window.headerSearch = {
url: "{% url 'wagtailvideos:index' %}",
termInput: "#id_q",
targetOutput: "#image-results"
}

$(function() {
$('#collection_chooser_collection_id').change(function() {
this.form.submit();
})
});
</script>
{% endblock %}

{% block content %}
{% trans "Videos" as video_str %}

{% trans "Add a video" as add_video_str %}
{% url "wagtailvideos:add_multiple" as add_link %}
{% include "wagtailadmin/shared/header.html" with title=video_str add_link="wagtailvideos:add_multiple" action_url=add_link icon="media" add_text=add_video_str action_text=add_video_str search_url="wagtailvideos:index" %}

<div class="nice-padding">
{% if collections %}
<form class="image-search search-bar" action="{% url 'wagtailvideos:index' %}" method="GET">
<ul class="fields">
{% include "wagtailadmin/shared/collection_chooser.html" %}
</ul>
</form>
{% endif %}

<div id="image-results">
{% include "wagtailvideos/videos/results.html" %}
</div>
</div>
{% block slim_header %}

{% trans "Add a video" as add_video_str %}
{% include "wagtailadmin/shared/headers/slim_header.html" with title=video_str breadcrumbs_items=breadcrumbs_items icon_name="media" search_url=index_results_url search_form=search_form filters=filters buttons=header_buttons only %}
{% endblock %}
28 changes: 9 additions & 19 deletions wagtailvideos/templates/wagtailvideos/videos/results.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@

{% extends "wagtailadmin/generic/index_results.html" %}
{% load wagtailadmin_tags %}
{% load i18n %}
{% if videos %}
{% if is_searching %}
<h2>
{% blocktrans count counter=videos.paginator.count %}
There is one match
{% plural %}
There are {{ counter }} matches
{% endblocktrans %}
</h2>

{% search_other %}
{% else %}
<h2>{% trans "Latest videos" %}</h2>
{% endif %}

{% block results %}
<div class="nice-padding">
<ul class="listing horiz images">
{% for video in videos %}
<li>
Expand All @@ -31,13 +21,13 @@ <h2>{% trans "Latest videos" %}</h2>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}


{% include "wagtailadmin/shared/pagination_nav.html" with items=videos is_searching=is_searching query_string=query_string linkurl="wagtailvideos:index" %}

{% else %}
{% block no_results_message %}
{% if is_searching %}
<h2>{% blocktrans %}Sorry, no videos match "<em>{{ query_string }}</em>"{% endblocktrans %}</h2>

{% search_other %}
{% else %}
{% url 'wagtailvideos:add_multiple' as wagtailvideos_add_video_url %}
Expand All @@ -47,4 +37,4 @@ <h2>{% blocktrans %}Sorry, no videos match "<em>{{ query_string }}</em>"{% endbl
<p>{% blocktrans %}You've not uploaded any videos. Why not <a href="{{ wagtailvideos_add_video_url }}">add one now</a>?{% endblocktrans %}</p>
{% endif %}
{% endif %}
{% endif %}
{% endblock %}
3 changes: 2 additions & 1 deletion wagtailvideos/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
re_path(r'^(\d+)/delete/$', videos.delete, name='delete'),
re_path(r'^(\d+)/create_transcode/$', videos.create_transcode, name='create_transcode'),
re_path(r'^(\d+)/$', videos.edit, name='edit'),
path('', videos.index, name='index'),
path('', videos.IndexView.as_view(), name='index'),
path("results/", videos.IndexView.as_view(results_only=True), name="index_results"),
]
1 change: 1 addition & 0 deletions wagtailvideos/views/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Meta(VideoForm.Meta):

@vary_on_headers('X-Requested-With')
def add(request):
# TODO replace with wagtail.admin.views.generic.multiple_upload.AddView subclass
Video = get_video_model()
VideoForm = get_video_form(Video)

Expand Down
93 changes: 35 additions & 58 deletions wagtailvideos/views/videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
from django.urls import reverse
from django.utils.translation import gettext as _
from django.views.decorators.http import require_POST
from django.views.decorators.vary import vary_on_headers
from wagtail.admin import messages
from wagtail.admin.auth import PermissionPolicyChecker
from wagtail.admin.forms.search import SearchForm
from wagtail.admin.models import popular_tags_for_model
from wagtail.models import Collection
from wagtail.admin.filters import BaseMediaFilterSet
from wagtail.admin.views import generic
from wagtail.search.backends import get_search_backends
from wagtail_modeladmin.helpers import AdminURLHelper

Expand All @@ -21,65 +19,44 @@
permission_checker = PermissionPolicyChecker(permission_policy)


@permission_checker.require_any('add', 'change', 'delete', 'choose')
@vary_on_headers('X-Requested-With')
def index(request):
# Get Videos (filtered by user permission)
Video = get_video_model()
Video = get_video_model()

collections = permission_policy.collections_user_has_any_permission_for(
request.user, ['add', 'change', 'delete', 'choose'])
if len(collections) > 1:
collections_to_choose = collections
else:
# no need to show a collections chooser
collections_to_choose = None

videos = Video.objects.filter(collection__in=collections)
class VideoFilterSet(BaseMediaFilterSet):
permission_policy = permission_policy

# Search
query_string = None
if 'q' in request.GET:
form = SearchForm(request.GET, placeholder=_("Search videos"))
if form.is_valid():
query_string = form.cleaned_data['q']
class Meta:
model = Video
fields = []

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

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

paginator = Paginator(videos, per_page=25)
page = paginator.get_page(request.GET.get('p'))

# Create response
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
response = render(request, 'wagtailvideos/videos/results.html', {
'videos': page,
'query_string': query_string,
'is_searching': bool(query_string),
})
return response
else:
response = render(request, 'wagtailvideos/videos/index.html', {
'videos': page,
'query_string': query_string,
'is_searching': bool(query_string),
'search_form': form,
'popular_tags': popular_tags_for_model(Video),
'current_collection': current_collection,
'collections': collections_to_choose,
})
return response
class IndexView(generic.IndexView):
context_object_name = "videos"
model = Video
filterset_class = VideoFilterSet
permission_policy = permission_policy
any_permission_required = ['add', 'change', 'delete']
header_icon = 'media'
template_name = 'wagtailvideos/videos/index.html'
results_template_name = 'wagtailvideos/videos/results.html'
_show_breadcrumbs = True
add_url_name = "wagtailvideos:add_multiple"
edit_url_name = "wagtailvideos:edit"
index_results_url_name = 'wagtailvideos:index_results'
add_item_label = "Add a video"

def get_breadcrumbs_items(self):
return self.breadcrumbs_items + [
{"url": "", "label": "Videos"},
]

def get_filterset_kwargs(self):
kwargs = super().get_filterset_kwargs()
kwargs["is_searching"] = self.is_searching
return kwargs

def get_paginate_by(self, queryset):
return 32 # 4 x 8


@permission_checker.require('change')
Expand Down
Loading

0 comments on commit 2486d58

Please sign in to comment.