WIP: Aureplop/kpsul js refactor #501

Draft
delobell wants to merge 215 commits from aureplop/kpsul_js_refactor into master
2 changed files with 50 additions and 39 deletions
Showing only changes of commit 5e8752632c - Show all commits

View file

@ -565,14 +565,14 @@ class Day extends ModelObject {
* @default <tt>['id', 'date']</tt>
* @see {@link Models.ModelObject.props|ModelObject.props}
*/
static get props() { return ['id', 'date', 'opegroups'] }
static get props() { return ['id', 'at', 'opegroups'] }
/**
* Default values for Day model instances
* @default <tt>{'id': '', 'date': moment()}</tt>
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
*/
static get default_data() { return {'id': '', 'date': moment(), 'opegroups': []}; }
static get default_data() { return {'id': '', 'at': moment(), 'opegroups': []}; }
/**
* Verbose name for Article model
@ -580,6 +580,11 @@ class Day extends ModelObject {
*/
static get verbose_name() { return 'day'; }
from(data) {
super.from(data);
this.id = this.at.format("YYYYMMDD");
}
/**
* @default {@link Formatters.DayFormatter}
*/
@ -593,14 +598,14 @@ class Day extends ModelObject {
*/
static compare (a, b) {
//Days are sorted by most recent first
if (a.date < b.date) return 1;
else if (a.date > b.date) return -1;
if (a.at < b.at) return 1;
else if (a.at > b.at) return -1;
else return 0;
}
//Parse date and round it
get date() { return this._date; }
set date(v) { this._date = dateUTCToParis(v).startOf('date'); }
get at() { return this._at; }
set at(v) { this._at = dateUTCToParis(v).startOf('date'); }
}
/**
@ -955,7 +960,8 @@ class ModelForest {
* @param {number} direction
*/
get_or_create(data, direction) {
var struct_data = this.constructor.structure[data.modelname];
var struct = this.constructor.structure ;
var struct_data = struct[data.modelname];
var model = struct_data.model;
var existing = this.find(data.modelname, data.content.id);
@ -963,19 +969,41 @@ class ModelForest {
return existing;
}
var node = new model(data.content);
var node;
if (data.content instanceof ModelObject)
node = data.content;
else
node = new model(data.content);
if (direction <= 0) {
var parent_name = struct_data.parent;
var parent_data = data.parent;
var parent_struct = this.constructor.structure[parent_name];
if (parent_data) {
var parent_node = this.get_or_create(parent_data, -1);
node[parent_name] = parent_node;
parent_node[parent_struct.children].push(node);
} else {
if (!parent_name) {
this.roots.push(node);
return node;
}
// If index, we create it
if (struct_data.index) {
var new_parent = {}
for (let key of struct_data.index.fields) {
new_parent[key] = data.content[key];
}
// We create model in case there are some special fields
var data_parent = {
'modelname': struct_data.index.modelname,
'content': new struct[struct_data.index.modelname].model(new_parent),
}
console.log(data_parent.content);
} else {
var data_parent = data.parent ;
}
var parent = this.get_or_create(data_parent, -1);
var parent_childname = struct[parent.constructor.verbose_name].children;
node[parent_name] = parent ;
parent[parent_childname].push(node);
}
if (direction >= 0) {
@ -983,7 +1011,7 @@ class ModelForest {
if (data.children && data.children.length) {
for (let child_data of data.children) {
var child = this.get_or_create(child_data, 1);
var child_parent = this.constructor.structure[child.constructor.verbose_name];
var child_parent = struct[child.constructor.verbose_name];
child[child_parent] = node;
node[child_name].push(child);
}
@ -1235,6 +1263,10 @@ class OperationList extends APIModelForest {
'model': OperationGroup,
'parent': 'day',
'children': 'opes',
'index': {
'modelname': 'day',
'fields': ["at"]
},
},
'transfergroup': {
'model': TransferGroup,
@ -1648,14 +1680,14 @@ class DayFormatter extends Formatter {
* @default {@link Models.Day.props}
*/
static get props() {
return Day.props;
return ['date'];
}
/**
* <tt>a.date</tt> formatted as <tt>D MMMM</tt>
*/
static prop_date(a) {
return a.date.format('D MMMM');
return a.at.format('D MMMM');
}
}

View file

@ -1182,13 +1182,6 @@ def kpsul_perform_operations(request):
'account_id': operationgroup.on_acc.pk,
'checkout_id': operationgroup.checkout.pk,
},
'parent': {
'modelname': 'day',
'content': {
'id': operationgroup.at.strftime('%Y%m%d'),
'date': operationgroup.at
},
},
'children': [],
}]
for ope in operations:
@ -1547,13 +1540,6 @@ def history_json(request):
'trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
},
'parent': {
'modelname': 'day',
'content': {
'id': opegroup.at.strftime('%Y%m%d'),
'date': opegroup.at
},
},
'children': [],
}
if request.user.has_perm('kfet.is_team'):
@ -1603,13 +1589,6 @@ def history_json(request):
'at': transfergroup.at,
'comment': transfergroup.comment,
},
'parent': {
'modelname': 'day',
'content': {
'id': transfergroup.at.strftime('%Y%m%d'),
'date': transfergroup.at
},
},
'children': [],
}