Sith/sas/templates/sas/album.jinja

162 lines
5.0 KiB
Plaintext
Raw Normal View History

2016-10-26 17:21:19 +00:00
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}SAS{% endtrans %}
{% endblock %}
2016-11-20 09:40:49 +00:00
{% macro print_path(file) %}
2016-11-20 13:08:10 +00:00
{% if file and file.parent %}
2016-11-20 09:40:49 +00:00
{{ print_path(file.parent) }}
<a href="{{ url('sas:album', album_id=file.id) }}">{{ file.get_display_name() }}</a> >
{% endif %}
{% endmacro %}
2016-10-26 17:21:19 +00:00
{% block content %}
2016-11-20 13:08:10 +00:00
<a href="{{ url('sas:main') }}">SAS</a> > {{ print_path(album.parent) }} {{ album.get_display_name() }}
2016-11-08 14:12:37 +00:00
<h3>{{ album.get_display_name() }}</h3>
2016-11-25 12:47:09 +00:00
<a href="{{ url('sas:album_edit', album_id=album.id) }}">{% trans %}Edit{% endtrans %}</a><br>
2016-11-20 22:53:41 +00:00
<hr>
2016-10-26 17:21:19 +00:00
<div>
{% for a in album.children.filter(is_folder=True).order_by('-id') %}
{% if a.as_album.can_be_viewed_by(user) %}
2016-11-20 22:53:41 +00:00
<a href="{{ url("sas:album", album_id=a.id) }}" style="display: inline-block">
<div class="album{% if not a.is_moderated %} not_moderated{% endif %}">
2016-11-20 22:53:41 +00:00
<div>
2016-11-30 08:28:22 +00:00
{% if a.file %}
<img src="{{ a.as_picture.get_download_url() }}" alt="{% trans %}preview{% endtrans %}">
{% elif a.children.filter(is_folder=False, is_moderated=True).exists() %}
2016-11-20 22:53:41 +00:00
<img src="{{ a.children.filter(is_folder=False).first().as_picture.get_download_thumb_url() }}" alt="{% trans %}preview{% endtrans %}">
{% else %}
<img src="{{ static('core/img/sas.jpg') }}" alt="{% trans %}preview{% endtrans %}">
{% endif %}
</div>
{{ a.name }}
2016-10-26 17:21:19 +00:00
</div>
2016-11-20 22:53:41 +00:00
</a>
{% endif %}
2016-10-26 17:21:19 +00:00
{% endfor %}
</div>
<div>
{% for p in album.children.filter(is_folder=False).order_by('id') %}
2016-11-08 14:12:37 +00:00
{% if p.as_picture.can_be_viewed_by(user) %}
<div class="picture{% if not p.is_moderated %} not_moderated{% endif %}">
<a href="{{ url("sas:picture", picture_id=p.id) }}#pict">
2016-11-20 22:53:41 +00:00
<img src="{{ p.as_picture.get_download_thumb_url() }}" alt="{{ p.get_display_name() }}" />
2016-10-26 17:21:19 +00:00
</a>
</div>
{% endif %}
{% endfor %}
</div>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Upload{% endtrans %}" /></p>
</form>
2016-10-26 17:21:19 +00:00
{% endblock %}
2016-11-20 23:35:07 +00:00
{% block script %}
{{ super() }}
2016-11-22 15:58:55 +00:00
<script>
$("form").submit(function (event) {
var formData = new FormData($(this)[0]);
if(formData.get('album_name') === '' && formData.get('images').name === '')
return false;
if(formData.get('images').name === '') {
return true;
}
event.preventDefault();
var errorlist;
if((errorlist = this.querySelector('ul.errorlist.nonfield')) === null) {
errorlist = document.createElement('ul');
errorlist.classList.add('errorlist', 'nonfield');
this.insertBefore(errorlist, this.firstElementChild);
}
2016-11-22 17:10:24 +00:00
while(errorlist.childElementCount > 0)
2016-11-22 15:58:55 +00:00
errorlist.removeChild(errorlist.firstElementChild);
var progress;
if((progress = this.querySelector('progress')) === null) {
progress = document.createElement('progress');
progress.value = 0;
var p = document.createElement('p');
p.appendChild(progress);
this.insertBefore(p, this.lastElementChild);
}
var dataHolder;
if(formData.get('album_name') !== '') {
dataHolder = new FormData();
dataHolder.set('csrfmiddlewaretoken', '{{ csrf_token }}');
dataHolder.set('album_name', formData.get('album_name'));
$.ajax({
method: 'POST',
data: dataHolder,
processData: false,
contentType: false,
success: onSuccess
2016-11-22 17:10:24 +00:00
});
2016-11-22 15:58:55 +00:00
}
var images = formData.getAll('images');
var imagesCount = images.length;
var completeCount = 0;
var poolSize = 5;
var imagePool = [];
while(images.length > 0 && imagePool.length < poolSize) {
var image = images.shift();
imagePool.push(image);
sendImage(image);
}
function sendImage(image) {
dataHolder = new FormData();
dataHolder.set('csrfmiddlewaretoken', '{{ csrf_token }}');
dataHolder.set('images', image);
$.ajax({
method: 'POST',
data: dataHolder,
processData: false,
contentType: false,
})
.done(onSuccess.bind(undefined, image))
.always(next.bind(undefined, image));
}
function next(image, status, jqXHR) {
var index;
if(index = imagePool.indexOf(image) !== -1) {
imagePool.splice(index, 1);
}
var nextImage;
if(nextImage = images.shift()) {
imagePool.push(nextImage);
sendImage(nextImage);
}
}
function onSuccess(image, data, status, jqXHR) {
2016-11-22 17:10:24 +00:00
if ($(data).find('.errorlist.nonfield')[0])
var errors = Array.from($(data).find('.errorlist.nonfield')[0].children);
else
var errors = []
2016-11-22 15:58:55 +00:00
while(errors.length > 0)
errorlist.appendChild(errors.shift());
progress.value = ++completeCount / imagesCount;
if(progress.value === 1 && errorlist.children.length === 0)
2016-12-08 14:16:42 +00:00
document.location.reload(true)
2016-11-22 15:58:55 +00:00
}
});
</script>
2016-11-20 23:35:07 +00:00
{% endblock %}