Add related objects

This commit is contained in:
Ludovic Stephan 2017-04-09 23:24:50 -03:00
parent 5e8752632c
commit 47da80f21c
3 changed files with 123 additions and 92 deletions

View file

@ -79,7 +79,7 @@ class KHistory {
}
add_node(data) {
var node = this.list.get_or_create(data, 0);
var node = this.list.get_or_create(data.modelname, data.content, 0);
this.list.add_to_container(this._$container, node, this.templates);
}

View file

@ -927,8 +927,8 @@ class ModelForest {
* Creates empty instance and populates it with data if given
* @param {Object[]} [datalist=[]]
*/
constructor(datalist) {
this.from(datalist || []);
constructor(data) {
this.from(data || {});
}
/**
@ -959,21 +959,21 @@ class ModelForest {
* @param {Object} data
* @param {number} direction
*/
get_or_create(data, direction) {
get_or_create(modelname, data, direction) {
var struct = this.constructor.structure ;
var struct_data = struct[data.modelname];
var struct_data = struct[modelname];
var model = struct_data.model;
var existing = this.find(data.modelname, data.content.id);
var existing = this.find(modelname, data.id);
if (existing) {
return existing;
}
var node;
if (data.content instanceof ModelObject)
node = data.content;
if (data instanceof ModelObject)
node = data;
else
node = new model(data.content);
node = new model(data);
if (direction <= 0) {
var parent_name = struct_data.parent;
@ -982,26 +982,32 @@ class ModelForest {
return node;
}
var parent_modelname, parent_data;
// 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];
new_parent[key] = data[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);
parent_data = new struct[struct_data.index.modelname].model(new_parent);
parent_modelname = struct_data.index.modelname;
} else if (struct_data.related) {
var related = struct_data.related;
var field = data[related.modelname+'__'+related.field];
parent_data = this.related[related.modelname].find( (obj) => obj[related.field] === field);
parent_modelname = related.modelname;
} else {
var data_parent = data.parent ;
parent_data = data.parent.content ;
parent_modelname = data.parent.modelname ;
}
var parent = this.get_or_create(data_parent, -1);
var parent_childname = struct[parent.constructor.verbose_name].children;
var parent = this.get_or_create(parent_modelname, parent_data, -1);
var parent_childname = struct[parent_modelname].children;
node[parent_name] = parent ;
parent[parent_childname].push(node);
}
@ -1010,8 +1016,9 @@ class ModelForest {
var child_name = struct_data.children;
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 = struct[child.constructor.verbose_name];
console.log(child_data);
var child = this.get_or_create(child_data.modelname, child_data.content, 1);
var child_parent = struct[child_data.modelname];
child[child_parent] = node;
node[child_name].push(child);
}
@ -1025,10 +1032,14 @@ class ModelForest {
* Resets then populates the instance with the given data.
* @param {Object[]} datalist
*/
from(datalist) {
from(data) {
this.roots = [];
for (let data of datalist) {
this.get_or_create(data, 0);
if (data.objects) {
this.related = data.related
for (let modelname in data.objects) {
for (let obj_data of data.objects[modelname])
this.get_or_create(modelname, obj_data, 0);
}
}
}
@ -1036,7 +1047,7 @@ class ModelForest {
* Removes all Models.TreeNode from the tree.
*/
clear() {
this.from([]);
this.from({});
}
/**
@ -1223,6 +1234,10 @@ class ArticleList extends APIModelForest {
'article': {
'model': Article,
'parent': 'category',
'related': {
'modelname': 'category',
'field': 'id',
}
},
'category': {
'model': ArticleCategory,
@ -1271,19 +1286,35 @@ class OperationList extends APIModelForest {
'transfergroup': {
'model': TransferGroup,
'parent': 'day',
'children': 'transfers'
'children': 'transfers',
'index': {
'modelname': 'day',
'fields': ["at"]
},
},
'purchase': {
'model': Purchase,
'parent': 'opegroup',
'related': {
'modelname': 'opegroup',
'field': 'id',
},
},
'specialope': {
'model': SpecialOperation,
'parent': 'opegroup',
'related': {
'modelname': 'opegroup',
'field': 'id',
},
},
'transfer': {
'model': Transfer,
'parent': 'transfergroup',
'related': {
'modelname': 'transfergroup',
'field': 'id',
},
},
};
}

View file

@ -1181,8 +1181,8 @@ def kpsul_perform_operations(request):
# Used to filter websocket updates
'account_id': operationgroup.on_acc.pk,
'checkout_id': operationgroup.checkout.pk,
'children': [],
},
'children': [],
}]
for ope in operations:
ope_data = {
@ -1208,7 +1208,7 @@ def kpsul_perform_operations(request):
ope_data['content'].update({
'type': ope.type,
})
websocket_data['opegroups'][0]['children'].append(ope_data)
websocket_data['opegroups'][0]['content']['children'].append(ope_data)
# Need refresh from db cause we used update on queryset
operationgroup.checkout.refresh_from_db()
websocket_data['checkouts'] = [{
@ -1527,97 +1527,91 @@ def history_json(request):
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
# Construction de la réponse
opegroup_list = []
related_data = defaultdict(list)
objects_data = defaultdict(list)
for opegroup in opegroups:
opegroup_dict = {
'modelname': 'opegroup',
'content': {
'id': opegroup.id,
'amount': opegroup.amount,
'at': opegroup.at,
'is_cof': opegroup.is_cof,
'comment': opegroup.comment,
'trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
},
'children': [],
'id': opegroup.id,
'amount': opegroup.amount,
'at': opegroup.at,
'is_cof': opegroup.is_cof,
'comment': opegroup.comment,
'trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
}
if request.user.has_perm('kfet.is_team'):
opegroup_dict['content']['valid_by'] = (
opegroup_dict['valid_by'] = (
opegroup.valid_by and opegroup.valid_by.trigramme or None)
for ope in opegroup.opes.all():
ope_dict = {
'content': {
'id': ope.id,
'amount': ope.amount,
'canceled_at': ope.canceled_at,
'is_cof': opegroup.is_cof,
'trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
},
'id': ope.id,
'amount': ope.amount,
'canceled_at': ope.canceled_at,
'is_cof': opegroup.is_cof,
'trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
'opegroup__id': opegroup.id,
}
if request.user.has_perm('kfet.is_team'):
ope_dict['content']['canceled_by'] = (
ope_dict['canceled_by'] = (
ope.canceled_by and ope.canceled_by.trigramme or None)
if ope.type == Operation.PURCHASE:
ope_dict['modelname'] = 'purchase'
ope_dict['content'].update({
ope_dict.update({
'article_name': ope.article.name,
'article_nb': ope.article_nb,
'addcost_amount': ope.addcost_amount,
'addcost_for':
ope.addcost_for and ope.addcost_for.trigramme or None,
})
objects_data['purchase'].append(ope_dict)
else:
ope_dict['modelname'] = 'specialope'
ope_dict['content'].update({
ope_dict.update({
'type': ope.type,
'is_checkout': ope.is_checkout,
})
objects_data['specialope'].append(ope_dict)
opegroup_dict['children'].append(ope_dict)
opegroup_list.append(opegroup_dict)
related_data['opegroup'].append(opegroup_dict)
for transfergroup in transfergroups:
if transfergroup.filtered_transfers:
transfergroup_dict = {
'modelname': 'transfergroup',
'content': {
'id': transfergroup.id,
'at': transfergroup.at,
'comment': transfergroup.comment,
},
'children': [],
'id': transfergroup.id,
'at': transfergroup.at,
'comment': transfergroup.comment,
}
if request.user.has_perm('kfet.is_team'):
transfergroup_dict['content']['valid_by'] = (
transfergroup_dict['valid_by'] = (
transfergroup.valid_by and
transfergroup.valid_by.trigramme or
None)
for transfer in transfergroup.filtered_transfers:
transfer_dict = {
'modelname': 'transfer',
'content': {
'id': transfer.id,
'amount': transfer.amount,
'canceled_at': transfer.canceled_at,
'from_acc': transfer.from_acc.trigramme,
'to_acc': transfer.to_acc.trigramme,
},
'id': transfer.id,
'amount': transfer.amount,
'canceled_at': transfer.canceled_at,
'from_acc': transfer.from_acc.trigramme,
'to_acc': transfer.to_acc.trigramme,
'transfergroup__id': transfergroup.id,
}
if request.user.has_perm('kfet.is_team'):
transfer_dict['content']['canceled_by'] = (
transfer_dict['canceled_by'] = (
transfer.canceled_by and
transfer.canceled_by.trigramme or
None)
transfergroup_dict['children'].append(transfer_dict)
opegroup_list.append(transfergroup_dict)
objects_data['transfer'].append(transfer_dict)
related_data['transfergroup'].append(transfergroup_dict)
data = {
'objects': objects_data,
'related': related_data,
}
return JsonResponse(data)
return JsonResponse(opegroup_list, safe=False)
@teamkfet_required
def kpsul_articles_data(request):
@ -1626,26 +1620,32 @@ def kpsul_articles_data(request):
.filter(is_sold=True)
.select_related('category'))
articlelist = []
categorylist = []
# TODO: nice queryset, no duplicate categories
for article in articles:
articlelist.append({
'modelname': 'article',
'content': {
'id': article.id,
'name': article.name,
'price': article.price,
'stock': article.stock,
},
'parent': {
'modelname': 'category',
'content': {
'id': article.category.id,
'name': article.category.name,
'has_addcost': article.category.has_addcost,
},
}
'id': article.id,
'name': article.name,
'price': article.price,
'stock': article.stock,
'category__id': article.category.id,
})
return JsonResponse(articlelist, safe=False)
categorylist.append({
'id': article.category.id,
'name': article.category.name,
'has_addcost': article.category.has_addcost,
})
data = {
'objects': {
'article': articlelist,
},
'related': {
'category': categorylist
}
}
return JsonResponse(data)
@teamkfet_required