Add fetch method

This commit is contained in:
Ludovic Stephan 2017-04-05 10:59:59 -03:00
parent 290d4ecb6e
commit 8d13c0a4bb
7 changed files with 31 additions and 26 deletions

View file

@ -1,6 +1,10 @@
class KHistory {
static get default_options() {
return { from: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'), };
}
constructor(api_options, display_options) {
constructor(display_options) {
this.templates = {
'purchase': '<div class="ope"><span class="amount"></span><span class="infos1"></span><span class="infos2"></span><span class="addcost"></span><span class="canceled"></span></div>',
'specialope': '<div class="ope"><span class="amount"></span><span class="infos1"></span><span class="infos2"></span><span class="addcost"></span><span class="canceled"></span></div>',
@ -14,26 +18,33 @@ class KHistory {
this._$nb_opes = $('#nb_opes');
this.list = new OperationList();
this.api_options =
api_options || { from: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'), };
this.display_options = display_options || {};
this._init_selection();
this._init_events();
}
fetch(api_options) {
this._$container.html('');
this.list.clear();
if (api_options)
this.api_options = api_options;
else if (!this.api_options)
this.api_options = this.constructor.default_options;
this.list.fromAPI(this.api_options)
.done( () => this.display() );
}
display() {
this.list.display(this._$container, this.templates, this.display_options);
var nb_opes = this._$container.find('.ope[canceled="false"]').length;
$('#nb_opes').text(nb_opes);
}
reset() {
this._$container.html('');
this.list.clear();
this.list.fromAPI(this.api_options, this.display.bind(this), $.noop);
}
_init_selection() {
this._$container.selectable({
filter: 'div.opegroup, div.ope',

View file

@ -24,6 +24,7 @@ class KPsulManager {
if (!soft) {
this.checkout_manager.reset();
this.article_manager.reset_data();
this.history.fetch();
}
}

View file

@ -105,10 +105,7 @@ $(document).ready(function() {
// Lock to avoid multiple requests
window.lock = 0;
var history = new KHistory();
history.api_options = {
'accounts': [{{ account.pk }}],
};
var history = new KHistory({'remove_props': ['trigramme']});
// -----
// Synchronization
@ -116,7 +113,7 @@ $(document).ready(function() {
OperationWebSocket.add_handler((data) => history.update_data(data));
Config.reset(history.reset.bind(history));
Config.reset(() => history.fetch({'accounts': [{{account.pk}}]}));
});
</script>

View file

@ -93,10 +93,9 @@ $(document).ready(function() {
data['checkouts'] = checkouts;
var accounts = getSelectedMultiple($accounts);
data['accounts'] = accounts;
history.api_options = data ;
// Update history
history.reset();
history.fetch(data);
}
$('#from_date').datetimepicker({

View file

@ -668,7 +668,7 @@ $(document).ready(function() {
Config.reset(function() {
kpsul.article_manager.reset_data();
displayAddcost();
kpsul.history.reset();
kpsul.history.fetch();
});
}

View file

@ -55,9 +55,6 @@ $(document).ready(function() {
window.lock = 0;
var history = new KHistory();
history.api_options = {
'transfersonly': true,
};
// -----
// Synchronization
@ -65,7 +62,7 @@ $(document).ready(function() {
OperationWebSocket.add_handler((data) => history.update_data(data));
Config.reset(history.reset.bind(history));
Config.reset(() => history.fetch({'transfersonly': true}));
});
</script>

View file

@ -1441,11 +1441,11 @@ def kpsul_cancel_operations(request):
@login_required
def history_json(request):
# Récupération des paramètres
from_date = request.POST.get('from', None)
to_date = request.POST.get('to', None)
checkouts = request.POST.getlist('checkouts[]', None)
accounts = request.POST.getlist('accounts[]', None)
transfers_only = request.POST.get('transfersonly', None)
from_date = request.GET.get('from', None)
to_date = request.GET.get('to', None)
checkouts = request.GET.getlist('checkouts[]', None)
accounts = request.GET.getlist('accounts[]', None)
transfers_only = request.GET.get('transfersonly', None)
# Construction de la requête (sur les opérations) pour le prefetch
ope_queryset_prefetch = Operation.objects.select_related(