Skip to content

Commit

Permalink
improve sync_rando view
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed Oct 6, 2022
1 parent 38962c1 commit a0bca7b
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 149 deletions.
257 changes: 133 additions & 124 deletions geotrek/common/templates/common/sync_rando.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,134 +4,143 @@
{% load i18n %}

{% block extrahead %}
<script type="text/javascript">
function enable_sync_button() {
$('#button-id-sync-web').attr('disabled', false);
$('#button-id-sync-web').val('{% trans "Launch Sync" %}');
}

function disable_sync_button(init) {
$('#button-id-sync-web').attr('disabled', true);
if (init==true){
$('#button-id-sync-web').val('{% trans "Sync verifications" %}');
} else {
$('#button-id-sync-web').val('{% trans "Sync in progress" %}');
}
}

function get_sync_infos() {
$.get("{% url 'common:sync_randos_state' %}",
function(response) {

// test if sync_rando run
var has_progress = false;

// test in each result / must be one
$.each(response, function(index) {
if (this.status == 'PROGRESS' || this.status == 'PENDING') {
has_progress = true;
disable_sync_button(false);

$('#progress-value').show();
$("#progress-value").removeClass('bar-danger');
$("#progress-value").parent().addClass("active");

if (this.result.current) {
$("#progress-value").css("width", this.result.current+'%');

if (this.result.current == 100){
$("#progress-value").parent().removeClass("active");
$("#progress-value").addClass('bar-success');
}
}
if (this.result.infos) {
$("#progress-text").text(this.result.infos);
}
}
else {
if (this.status == 'FAILURE'){
// case of exception in task
$("#progress-text").text("{% trans 'An error occured' %}");
$('#exception-message').text(this.result.exc_type + ' : ' + this.result.exc_message)
$("#progress-value").addClass('bar-danger');
$("#progress-value").parent().removeClass("active");
}
}
});

if (!has_progress) {
enable_sync_button();
}
});
}

$(function(){
disable_sync_button(true);
get_sync_infos();

$('#btn-confirm')[0].addEventListener('click', function(evt) {
$("#progress-value").css("width", '0%');
$("#progress-text").text('');
$("#progress-value").parent().addClass("active");
$("#progress-value").removeClass('bar-success');
$("#progress-value").removeClass('bar-danger');

$.post(
$('#form-sync').attr('action'),
$('#form-sync').serialize()
);

$('#confirm-submit').modal('toggle');
disable_sync_button(true);
});

window.setInterval(function(){
get_sync_infos();
}, 500);
});
</script>
<link rel="stylesheet" href="{% static 'trekking/css/sync_trek.css' %}" />
<script type="text/javascript">
function enable_sync_button() {
$('#button-id-sync-web').attr('disabled', false);
$('#button-id-sync-web').val('{% trans "Launch Sync" %}');
}

function disable_sync_button(init) {
$('#button-id-sync-web').attr('disabled', true);
if (init == true) {
$('#button-id-sync-web').val('{% trans "Sync verifications" %}');
} else {
$('#button-id-sync-web').val('{% trans "Sync in progress" %}');
}
}

function get_sync_infos() {
$.get("{% url 'common:sync_randos_state' %}",
function (response) {

// test if sync_rando run
var has_progress = false;

var result = response[0];
// test in each result / must be one
$.each(response, function (index) {
console.log(index);
if (this.status == 'PROGRESS' || this.status == 'PENDING') {
has_progress = true;
disable_sync_button(false);

$("#progress-bar").removeClass('bg-danger');
$("#progress-bar").parent().addClass("active");

if (this.result.current) {
$("#progress-bar").css("width", this.result.current + '%');

if (this.result.current == 100) {
$("#progress-bar").parent().removeClass("active");
$("#progress-bar").addClass('bg-success');
}
}
if (this.result.infos) {
$("#progress-bar").text(this.result.infos);
}
} else {
if (this.status == 'FAILURE') {
// case of exception in task
$("#progress-bar").text("{% trans 'An error occured' %}");
$('#exception-message').text(this.result.exc_type + ' : ' + this.result.exc_message)
$('#exception-message').show();
$("#progress-bar").addClass('bg-danger');
$("#progress-value").parent().removeClass("active");
}
}
});

if (!has_progress) {
enable_sync_button();
}
});
}

$(function () {
disable_sync_button(true);
get_sync_infos();

$('#btn-confirm')[0].addEventListener('click', function (evt) {
$('#exception-message').hide();
$("#progress-bar").css("width", '0%');
$("#progress-bar").text('');
$("#progress-bar").parent().addClass("active");
$("#progress-bar").removeClass('bg-success');
$("#progress-bar").removeClass('bg-danger');

$.post(
$('#form-sync').attr('action'),
$('#form-sync').serialize()
);

$('#confirm-submit').modal('toggle');
disable_sync_button(true);
});

window.setInterval(function () {
get_sync_infos();
}, 500);
});
</script>
<link rel="stylesheet" href="{% static 'trekking/css/sync_trek.css' %}"/>
{% endblock extrahead %}

{% block toolbar %}
{% endblock toolbar %}

{% block mainpanel %}

<div id="sync-div" class="col-12 col-sm-3">
<h3>{% trans "Public web site sync" %}</h3>
<div class="progress progress-striped active">
<div id="progress-value" class="bar" style="width: 0%;"></div>
<span id="progress-text"></span>
</div>
{% block mainform %}
{% crispy form form.helper %}
{% endblock mainform %}
</div>

<div id="exception-message" style="display: none;">
</div>

<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<!-- boostrap 2 modal confirmation -->
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
{% trans "Confirmation" %}
</div>
<div class="modal-body">
{% trans "Are you sure you want to sync ?" %}
</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
{% trans "Cancel" %}
</button>
<a href="#" id="btn-confirm" class="btn btn-success success">
{% trans "Sync" %}
</a>
</div>
</div>
</div>
</div>
<div id="sync-div" class="col-12 col-md-6 offset-md-3">
<div class="card">
<div class="card-header">
<h3>{% trans "Public web site sync" %}</h3>
</div>
<div class="card-body">
<div class="progress progress-striped" role="progressbar">
<div class="progress-bar progress-bar-striped" style="width: 0;" id="progress-bar"></div>
</div>
<div id="exception-message" style="display: none;" class="alert alert-danger">
</div>
</div>
<div class="card-footer">
{% block mainform %}
{% crispy form form.helper %}
{% endblock mainform %}
</div>
</div>
</div>


<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<!-- boostrap 2 modal confirmation -->
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
{% trans "Confirmation" %}
</div>
<div class="modal-body">
{% trans "Are you sure you want to sync ?" %}
</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
{% trans "Cancel" %}
</button>
<a href="#" id="btn-confirm" class="btn btn-success success">
{% trans "Sync" %}
</a>
</div>
</div>
</div>
</div>
{% endblock mainpanel %}
26 changes: 12 additions & 14 deletions geotrek/common/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from django.urls import path, converters, register_converter
from mapentity.registry import MapEntityOptions

from .views import (JSSettings, admin_check_extents, DocumentPublic, DocumentBookletPublic, import_view, import_update_json,
ThemeViewSet, MarkupPublic, sync_view, sync_update_json, SyncRandoRedirect)

from . import views

class LangConverter(converters.StringConverter):
regex = '[a-z]{2}'
Expand All @@ -13,21 +11,21 @@ class LangConverter(converters.StringConverter):

app_name = 'common'
urlpatterns = [
path('api/settings.json', JSSettings.as_view(), name='settings_json'),
path('tools/extents/', admin_check_extents, name='check_extents'),
path('commands/import-update.json', import_update_json, name='import_update_json'),
path('commands/import', import_view, name='import_dataset'),
path('commands/sync', SyncRandoRedirect.as_view(), name='sync_randos'),
path('commands/syncview', sync_view, name='sync_randos_view'),
path('commands/statesync/', sync_update_json, name='sync_randos_state'),
path('api/<lang:lang>/themes.json', ThemeViewSet.as_view({'get': 'list'}), name="themes_json"),
path('api/settings.json', views.JSSettings.as_view(), name='settings_json'),
path('tools/extents/', views.admin_check_extents, name='check_extents'),
path('commands/import-update.json', views.import_update_json, name='import_update_json'),
path('commands/import', views.import_view, name='import_dataset'),
path('commands/sync', views.SyncRandoRedirect.as_view(), name='sync_randos'),
path('commands/syncview', views.SyncRandoView.as_view(), name='sync_randos_view'),
path('commands/statesync/', views.sync_update_json, name='sync_randos_state'),
path('api/<lang:lang>/themes.json', views.ThemeViewSet.as_view({'get': 'list'}), name="themes_json"),
]


class PublishableEntityOptions(MapEntityOptions):
document_public_view = DocumentPublic
document_public_booklet_view = DocumentBookletPublic
markup_public_view = MarkupPublic
document_public_view = views.DocumentPublic
document_public_booklet_view = views.DocumentBookletPublic
markup_public_view =views.MarkupPublic

def scan_views(self, *args, **kwargs):
""" Adds the URLs of all views provided by ``PublishableMixin`` models.
Expand Down
18 changes: 7 additions & 11 deletions geotrek/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.http import JsonResponse, Http404, HttpResponse, HttpResponseRedirect
Expand All @@ -24,7 +25,7 @@
from django.utils.translation import gettext as _
from django.views import static
from django.views.decorators.http import require_POST, require_http_methods
from django.views.generic import RedirectView, View
from django.views.generic import RedirectView, View, FormView
from django.views.generic import TemplateView
from django_celery_results.models import TaskResult
from mapentity import views as mapentity_views
Expand Down Expand Up @@ -327,17 +328,12 @@ def last_list(request):
return redirect('trekking:trek_list')


@login_required
@user_passes_test(lambda u: u.is_superuser)
def sync_view(request):
"""
Custom views to view / track / launch a sync rando
"""
class SyncRandoView(LoginRequiredMixin, UserPassesTestMixin, FormView):
form_class = SyncRandoForm
template_name = 'common/sync_rando.html'

return render(request,
'common/sync_rando.html',
{'form': SyncRandoForm(), },
)
def test_func(self):
return self.request.user.is_superuser


@login_required
Expand Down

0 comments on commit a0bca7b

Please sign in to comment.