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 { 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 = { 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>', '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>', '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._$nb_opes = $('#nb_opes');
this.list = new OperationList(); 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.display_options = display_options || {};
this._init_selection(); this._init_selection();
this._init_events(); 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() { display() {
this.list.display(this._$container, this.templates, this.display_options); this.list.display(this._$container, this.templates, this.display_options);
var nb_opes = this._$container.find('.ope[canceled="false"]').length; var nb_opes = this._$container.find('.ope[canceled="false"]').length;
$('#nb_opes').text(nb_opes); $('#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() { _init_selection() {
this._$container.selectable({ this._$container.selectable({
filter: 'div.opegroup, div.ope', filter: 'div.opegroup, div.ope',

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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