restify album view

This commit is contained in:
thomas girod 2024-08-05 23:40:11 +02:00
parent 0b9ccf6a57
commit da6bd84cdf
7 changed files with 75 additions and 61 deletions

View File

@ -65,3 +65,21 @@ function display_notif() {
function getCSRFToken() { function getCSRFToken() {
return $("[name=csrfmiddlewaretoken]").val(); return $("[name=csrfmiddlewaretoken]").val();
} }
const initialUrlParams = new URLSearchParams(window.location.search);
function update_query_string(key, value) {
const url = new URL(window.location.href);
if (!value) {
// If the value is null, undefined or empty => delete it
url.searchParams.delete(key)
} else if (Array.isArray(value)) {
url.searchParams.delete(key)
value.forEach((v) => url.searchParams.append(key, v))
} else {
url.searchParams.set(key, value);
}
history.pushState(null, document.title, url.toString());
}

View File

@ -3,6 +3,7 @@
.pagination { .pagination {
text-align: center; text-align: center;
gap: 10px; gap: 10px;
margin: 30px;
button { button {
background-color: $secondary-neutral-light-color; background-color: $secondary-neutral-light-color;

View File

@ -161,17 +161,13 @@ main {
> .album { > .album {
box-sizing: border-box; box-sizing: border-box;
background-color: #333333; background-color: #333333;
background-size: cover; background-size: contain;
background-repeat: no-repeat; background-repeat: no-repeat;
background-position: center center; background-position: center center;
width: calc(16 / 9 * 128px); width: calc(16 / 9 * 128px);
height: 128px; height: 128px;
&.vertical {
background-size: contain;
}
margin: 0; margin: 0;
padding: 0; padding: 0;
box-shadow: none; box-shadow: none;

View File

@ -126,22 +126,6 @@
</nav> </nav>
</div> </div>
<script> <script>
const initialUrlParams = new URLSearchParams(window.location.search);
function update_query_string(key, value) {
const url = new URL(window.location.href);
if (!value) {
{# If the value is null, undefined or empty => delete it #}
url.searchParams.delete(key)
} else if (Array.isArray(value)) {
url.searchParams.delete(key)
value.forEach((v) => url.searchParams.append(key, v))
} else {
url.searchParams.set(key, value);
}
history.pushState(null, document.title, url.toString());
}
{# {#
How does this work : How does this work :

View File

@ -50,7 +50,7 @@ class PicturesController(ControllerBase):
Picture.objects.filter(is_moderated=True, asked_for_removal=False) Picture.objects.filter(is_moderated=True, asked_for_removal=False)
) )
.distinct() .distinct()
.order_by("-date") .order_by("-parent__date", "date")
.annotate(album=F("parent__name")) .annotate(album=F("parent__name"))
) )

View File

@ -3,6 +3,7 @@
{%- block additional_css -%} {%- block additional_css -%}
<link rel="stylesheet" href="{{ scss('sas/album.scss') }}"> <link rel="stylesheet" href="{{ scss('sas/album.scss') }}">
<link rel="stylesheet" href="{{ scss('core/pagination.scss') }}">
{%- endblock -%} {%- endblock -%}
{% block title %} {% block title %}
@ -81,39 +82,38 @@
<br> <br>
{% endif %} {% endif %}
<h4>{% trans %}Pictures{% endtrans %}</h4> <div x-data="pictures">
{% if pictures | length != 0 %} <h4>{% trans %}Pictures{% endtrans %}</h4>
<div class="photos"> <div class="photos">
{% for p in pictures %} <template x-for="picture in pictures.results">
{% if p.can_be_viewed_by(user) %} <a :href="`/sas/picture/${picture.id}#pict`">
<a href="{{ url('sas:picture', picture_id=p.id) }}#pict"> <div class="photo" :style="`background-image: url(${picture.thumb_url})`">
<div <template x-if="!picture.is_moderated">
class="photo {% if p.is_vertical %}vertical{% endif %}" <div class="overlay">&nbsp;</div>
style="background-image: url('{{ p.get_download_thumb_url() }}')" <div class="text">{% trans %}To be moderated{% endtrans %}</div>
> </template>
{% if not p.is_moderated %} <template x-if="picture.is_moderated">
<div class="overlay">&nbsp;</div> <div class="text">&nbsp;</div>
<div class="text">{% trans %}To be moderated{% endtrans %}</div> </template>
{% else %} </div>
<div class="text">&nbsp;</div> {% if edit_mode %}
{% endif %} <input type="checkbox" name="file_list" :value="picture.id">
</div> {% endif %}
{% if edit_mode %} </a>
<input type="checkbox" name="file_list" value="{{ p.id }}"> </template>
{% endif %}
</a>
{% endif %}
{% endfor %}
</div> </div>
{% else %} <nav class="pagination" x-show="nb_pages() > 1">
{% trans %}This album does not contain any photos.{% endtrans %} <button @click="page--" :disabled="page <= 1">
{% endif %} <i class="fa fa-caret-left"></i>
</button>
{% if pictures.has_previous() or pictures.has_next() %} <template x-for="i in nb_pages()">
<div class="paginator"> <button x-text="i" @click="page = i":class="{active: page === i}"></button>
{{ paginate(pictures, paginator) }} </template>
</div> <button @click="page++" :disabled="page >= nb_pages()">
{% endif %} <i class="fa fa-caret-right"></i>
</button>
</nav>
</div>
{% if edit_mode %} {% if edit_mode %}
</form> </form>
@ -140,6 +140,29 @@
{% block script %} {% block script %}
{{ super() }} {{ super() }}
<script> <script>
document.addEventListener("alpine:init", () => {
Alpine.data("pictures", () => ({
pictures: {},
page: parseInt(initialUrlParams.get("page")) || 1,
async init() {
await this.fetch_pictures();
this.$watch("page", () => this.fetch_pictures());
},
async fetch_pictures() {
update_query_string("page", this.page === 1 ? null : this.page);
const url = "{{ url("api:pictures") }}"
+"?album_id={{ album.id }}"
+`&page=${this.page}`
+"&page_size={{ settings.SITH_SAS_IMAGES_PER_PAGE }}";
this.pictures = await (await fetch(url)).json();
},
nb_pages() {
return Math.ceil(this.pictures.count / {{ settings.SITH_SAS_IMAGES_PER_PAGE }});
}
}))
})
$("form#upload_form").submit(function (event) { $("form#upload_form").submit(function (event) {
let formData = new FormData($(this)[0]); let formData = new FormData($(this)[0]);

View File

@ -236,7 +236,6 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
form_class = SASForm form_class = SASForm
pk_url_kwarg = "album_id" pk_url_kwarg = "album_id"
template_name = "sas/album.jinja" template_name = "sas/album.jinja"
paginate_by = settings.SITH_SAS_IMAGES_PER_PAGE
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
try: try:
@ -283,13 +282,6 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs = super().get_context_data(**kwargs) kwargs = super().get_context_data(**kwargs)
kwargs["paginator"] = Paginator(
self.object.children_pictures.order_by("id"), self.paginate_by
)
try:
kwargs["pictures"] = kwargs["paginator"].page(self.asked_page)
except InvalidPage as e:
raise Http404 from e
kwargs["form"] = self.form kwargs["form"] = self.form
kwargs["clipboard"] = SithFile.objects.filter( kwargs["clipboard"] = SithFile.objects.filter(
id__in=self.request.session["clipboard"] id__in=self.request.session["clipboard"]