Add related objects
This commit is contained in:
parent
5e8752632c
commit
47da80f21c
3 changed files with 123 additions and 92 deletions
|
@ -79,7 +79,7 @@ class KHistory {
|
||||||
}
|
}
|
||||||
|
|
||||||
add_node(data) {
|
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);
|
this.list.add_to_container(this._$container, node, this.templates);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -927,8 +927,8 @@ class ModelForest {
|
||||||
* Creates empty instance and populates it with data if given
|
* Creates empty instance and populates it with data if given
|
||||||
* @param {Object[]} [datalist=[]]
|
* @param {Object[]} [datalist=[]]
|
||||||
*/
|
*/
|
||||||
constructor(datalist) {
|
constructor(data) {
|
||||||
this.from(datalist || []);
|
this.from(data || {});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -959,21 +959,21 @@ class ModelForest {
|
||||||
* @param {Object} data
|
* @param {Object} data
|
||||||
* @param {number} direction
|
* @param {number} direction
|
||||||
*/
|
*/
|
||||||
get_or_create(data, direction) {
|
get_or_create(modelname, data, direction) {
|
||||||
var struct = this.constructor.structure ;
|
var struct = this.constructor.structure ;
|
||||||
var struct_data = struct[data.modelname];
|
var struct_data = struct[modelname];
|
||||||
var model = struct_data.model;
|
var model = struct_data.model;
|
||||||
|
|
||||||
var existing = this.find(data.modelname, data.content.id);
|
var existing = this.find(modelname, data.id);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
var node;
|
var node;
|
||||||
if (data.content instanceof ModelObject)
|
if (data instanceof ModelObject)
|
||||||
node = data.content;
|
node = data;
|
||||||
else
|
else
|
||||||
node = new model(data.content);
|
node = new model(data);
|
||||||
|
|
||||||
if (direction <= 0) {
|
if (direction <= 0) {
|
||||||
var parent_name = struct_data.parent;
|
var parent_name = struct_data.parent;
|
||||||
|
@ -982,26 +982,32 @@ class ModelForest {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var parent_modelname, parent_data;
|
||||||
// If index, we create it
|
// If index, we create it
|
||||||
if (struct_data.index) {
|
if (struct_data.index) {
|
||||||
var new_parent = {}
|
var new_parent = {}
|
||||||
for (let key of struct_data.index.fields) {
|
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
|
// We create model in case there are some special fields
|
||||||
var data_parent = {
|
parent_data = new struct[struct_data.index.modelname].model(new_parent);
|
||||||
'modelname': struct_data.index.modelname,
|
parent_modelname = struct_data.index.modelname;
|
||||||
'content': new struct[struct_data.index.modelname].model(new_parent),
|
|
||||||
}
|
} else if (struct_data.related) {
|
||||||
console.log(data_parent.content);
|
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 {
|
} 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 = this.get_or_create(parent_modelname, parent_data, -1);
|
||||||
var parent_childname = struct[parent.constructor.verbose_name].children;
|
var parent_childname = struct[parent_modelname].children;
|
||||||
node[parent_name] = parent ;
|
node[parent_name] = parent ;
|
||||||
parent[parent_childname].push(node);
|
parent[parent_childname].push(node);
|
||||||
}
|
}
|
||||||
|
@ -1010,8 +1016,9 @@ class ModelForest {
|
||||||
var child_name = struct_data.children;
|
var child_name = struct_data.children;
|
||||||
if (data.children && data.children.length) {
|
if (data.children && data.children.length) {
|
||||||
for (let child_data of data.children) {
|
for (let child_data of data.children) {
|
||||||
var child = this.get_or_create(child_data, 1);
|
console.log(child_data);
|
||||||
var child_parent = struct[child.constructor.verbose_name];
|
var child = this.get_or_create(child_data.modelname, child_data.content, 1);
|
||||||
|
var child_parent = struct[child_data.modelname];
|
||||||
child[child_parent] = node;
|
child[child_parent] = node;
|
||||||
node[child_name].push(child);
|
node[child_name].push(child);
|
||||||
}
|
}
|
||||||
|
@ -1025,10 +1032,14 @@ class ModelForest {
|
||||||
* Resets then populates the instance with the given data.
|
* Resets then populates the instance with the given data.
|
||||||
* @param {Object[]} datalist
|
* @param {Object[]} datalist
|
||||||
*/
|
*/
|
||||||
from(datalist) {
|
from(data) {
|
||||||
this.roots = [];
|
this.roots = [];
|
||||||
for (let data of datalist) {
|
if (data.objects) {
|
||||||
this.get_or_create(data, 0);
|
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.
|
* Removes all Models.TreeNode from the tree.
|
||||||
*/
|
*/
|
||||||
clear() {
|
clear() {
|
||||||
this.from([]);
|
this.from({});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1223,6 +1234,10 @@ class ArticleList extends APIModelForest {
|
||||||
'article': {
|
'article': {
|
||||||
'model': Article,
|
'model': Article,
|
||||||
'parent': 'category',
|
'parent': 'category',
|
||||||
|
'related': {
|
||||||
|
'modelname': 'category',
|
||||||
|
'field': 'id',
|
||||||
|
}
|
||||||
},
|
},
|
||||||
'category': {
|
'category': {
|
||||||
'model': ArticleCategory,
|
'model': ArticleCategory,
|
||||||
|
@ -1271,19 +1286,35 @@ class OperationList extends APIModelForest {
|
||||||
'transfergroup': {
|
'transfergroup': {
|
||||||
'model': TransferGroup,
|
'model': TransferGroup,
|
||||||
'parent': 'day',
|
'parent': 'day',
|
||||||
'children': 'transfers'
|
'children': 'transfers',
|
||||||
|
'index': {
|
||||||
|
'modelname': 'day',
|
||||||
|
'fields': ["at"]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'purchase': {
|
'purchase': {
|
||||||
'model': Purchase,
|
'model': Purchase,
|
||||||
'parent': 'opegroup',
|
'parent': 'opegroup',
|
||||||
|
'related': {
|
||||||
|
'modelname': 'opegroup',
|
||||||
|
'field': 'id',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'specialope': {
|
'specialope': {
|
||||||
'model': SpecialOperation,
|
'model': SpecialOperation,
|
||||||
'parent': 'opegroup',
|
'parent': 'opegroup',
|
||||||
|
'related': {
|
||||||
|
'modelname': 'opegroup',
|
||||||
|
'field': 'id',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'transfer': {
|
'transfer': {
|
||||||
'model': Transfer,
|
'model': Transfer,
|
||||||
'parent': 'transfergroup',
|
'parent': 'transfergroup',
|
||||||
|
'related': {
|
||||||
|
'modelname': 'transfergroup',
|
||||||
|
'field': 'id',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
134
kfet/views.py
134
kfet/views.py
|
@ -1181,8 +1181,8 @@ def kpsul_perform_operations(request):
|
||||||
# Used to filter websocket updates
|
# Used to filter websocket updates
|
||||||
'account_id': operationgroup.on_acc.pk,
|
'account_id': operationgroup.on_acc.pk,
|
||||||
'checkout_id': operationgroup.checkout.pk,
|
'checkout_id': operationgroup.checkout.pk,
|
||||||
|
'children': [],
|
||||||
},
|
},
|
||||||
'children': [],
|
|
||||||
}]
|
}]
|
||||||
for ope in operations:
|
for ope in operations:
|
||||||
ope_data = {
|
ope_data = {
|
||||||
|
@ -1208,7 +1208,7 @@ def kpsul_perform_operations(request):
|
||||||
ope_data['content'].update({
|
ope_data['content'].update({
|
||||||
'type': ope.type,
|
'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
|
# Need refresh from db cause we used update on queryset
|
||||||
operationgroup.checkout.refresh_from_db()
|
operationgroup.checkout.refresh_from_db()
|
||||||
websocket_data['checkouts'] = [{
|
websocket_data['checkouts'] = [{
|
||||||
|
@ -1527,97 +1527,91 @@ def history_json(request):
|
||||||
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
|
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
|
||||||
|
|
||||||
# Construction de la réponse
|
# Construction de la réponse
|
||||||
opegroup_list = []
|
related_data = defaultdict(list)
|
||||||
|
objects_data = defaultdict(list)
|
||||||
for opegroup in opegroups:
|
for opegroup in opegroups:
|
||||||
opegroup_dict = {
|
opegroup_dict = {
|
||||||
'modelname': 'opegroup',
|
'id': opegroup.id,
|
||||||
'content': {
|
'amount': opegroup.amount,
|
||||||
'id': opegroup.id,
|
'at': opegroup.at,
|
||||||
'amount': opegroup.amount,
|
'is_cof': opegroup.is_cof,
|
||||||
'at': opegroup.at,
|
'comment': opegroup.comment,
|
||||||
'is_cof': opegroup.is_cof,
|
'trigramme':
|
||||||
'comment': opegroup.comment,
|
opegroup.on_acc and opegroup.on_acc.trigramme or None,
|
||||||
'trigramme':
|
|
||||||
opegroup.on_acc and opegroup.on_acc.trigramme or None,
|
|
||||||
},
|
|
||||||
'children': [],
|
|
||||||
}
|
}
|
||||||
if request.user.has_perm('kfet.is_team'):
|
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)
|
opegroup.valid_by and opegroup.valid_by.trigramme or None)
|
||||||
|
|
||||||
for ope in opegroup.opes.all():
|
for ope in opegroup.opes.all():
|
||||||
ope_dict = {
|
ope_dict = {
|
||||||
'content': {
|
'id': ope.id,
|
||||||
'id': ope.id,
|
'amount': ope.amount,
|
||||||
'amount': ope.amount,
|
'canceled_at': ope.canceled_at,
|
||||||
'canceled_at': ope.canceled_at,
|
'is_cof': opegroup.is_cof,
|
||||||
'is_cof': opegroup.is_cof,
|
'trigramme':
|
||||||
'trigramme':
|
opegroup.on_acc and opegroup.on_acc.trigramme or None,
|
||||||
opegroup.on_acc and opegroup.on_acc.trigramme or None,
|
'opegroup__id': opegroup.id,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if request.user.has_perm('kfet.is_team'):
|
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)
|
ope.canceled_by and ope.canceled_by.trigramme or None)
|
||||||
|
|
||||||
if ope.type == Operation.PURCHASE:
|
if ope.type == Operation.PURCHASE:
|
||||||
ope_dict['modelname'] = 'purchase'
|
ope_dict.update({
|
||||||
ope_dict['content'].update({
|
|
||||||
'article_name': ope.article.name,
|
'article_name': ope.article.name,
|
||||||
'article_nb': ope.article_nb,
|
'article_nb': ope.article_nb,
|
||||||
'addcost_amount': ope.addcost_amount,
|
'addcost_amount': ope.addcost_amount,
|
||||||
'addcost_for':
|
'addcost_for':
|
||||||
ope.addcost_for and ope.addcost_for.trigramme or None,
|
ope.addcost_for and ope.addcost_for.trigramme or None,
|
||||||
})
|
})
|
||||||
|
objects_data['purchase'].append(ope_dict)
|
||||||
else:
|
else:
|
||||||
ope_dict['modelname'] = 'specialope'
|
ope_dict.update({
|
||||||
ope_dict['content'].update({
|
|
||||||
'type': ope.type,
|
'type': ope.type,
|
||||||
'is_checkout': ope.is_checkout,
|
|
||||||
})
|
})
|
||||||
|
objects_data['specialope'].append(ope_dict)
|
||||||
|
|
||||||
opegroup_dict['children'].append(ope_dict)
|
related_data['opegroup'].append(opegroup_dict)
|
||||||
opegroup_list.append(opegroup_dict)
|
|
||||||
|
|
||||||
for transfergroup in transfergroups:
|
for transfergroup in transfergroups:
|
||||||
if transfergroup.filtered_transfers:
|
if transfergroup.filtered_transfers:
|
||||||
transfergroup_dict = {
|
transfergroup_dict = {
|
||||||
'modelname': 'transfergroup',
|
'id': transfergroup.id,
|
||||||
'content': {
|
'at': transfergroup.at,
|
||||||
'id': transfergroup.id,
|
'comment': transfergroup.comment,
|
||||||
'at': transfergroup.at,
|
|
||||||
'comment': transfergroup.comment,
|
|
||||||
},
|
|
||||||
'children': [],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.user.has_perm('kfet.is_team'):
|
if request.user.has_perm('kfet.is_team'):
|
||||||
transfergroup_dict['content']['valid_by'] = (
|
transfergroup_dict['valid_by'] = (
|
||||||
transfergroup.valid_by and
|
transfergroup.valid_by and
|
||||||
transfergroup.valid_by.trigramme or
|
transfergroup.valid_by.trigramme or
|
||||||
None)
|
None)
|
||||||
|
|
||||||
for transfer in transfergroup.filtered_transfers:
|
for transfer in transfergroup.filtered_transfers:
|
||||||
transfer_dict = {
|
transfer_dict = {
|
||||||
'modelname': 'transfer',
|
'id': transfer.id,
|
||||||
'content': {
|
'amount': transfer.amount,
|
||||||
'id': transfer.id,
|
'canceled_at': transfer.canceled_at,
|
||||||
'amount': transfer.amount,
|
'from_acc': transfer.from_acc.trigramme,
|
||||||
'canceled_at': transfer.canceled_at,
|
'to_acc': transfer.to_acc.trigramme,
|
||||||
'from_acc': transfer.from_acc.trigramme,
|
'transfergroup__id': transfergroup.id,
|
||||||
'to_acc': transfer.to_acc.trigramme,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if request.user.has_perm('kfet.is_team'):
|
if request.user.has_perm('kfet.is_team'):
|
||||||
transfer_dict['content']['canceled_by'] = (
|
transfer_dict['canceled_by'] = (
|
||||||
transfer.canceled_by and
|
transfer.canceled_by and
|
||||||
transfer.canceled_by.trigramme or
|
transfer.canceled_by.trigramme or
|
||||||
None)
|
None)
|
||||||
transfergroup_dict['children'].append(transfer_dict)
|
objects_data['transfer'].append(transfer_dict)
|
||||||
opegroup_list.append(transfergroup_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
|
@teamkfet_required
|
||||||
def kpsul_articles_data(request):
|
def kpsul_articles_data(request):
|
||||||
|
@ -1626,26 +1620,32 @@ def kpsul_articles_data(request):
|
||||||
.filter(is_sold=True)
|
.filter(is_sold=True)
|
||||||
.select_related('category'))
|
.select_related('category'))
|
||||||
articlelist = []
|
articlelist = []
|
||||||
|
categorylist = []
|
||||||
|
|
||||||
|
# TODO: nice queryset, no duplicate categories
|
||||||
for article in articles:
|
for article in articles:
|
||||||
articlelist.append({
|
articlelist.append({
|
||||||
'modelname': 'article',
|
'id': article.id,
|
||||||
'content': {
|
'name': article.name,
|
||||||
'id': article.id,
|
'price': article.price,
|
||||||
'name': article.name,
|
'stock': article.stock,
|
||||||
'price': article.price,
|
'category__id': article.category.id,
|
||||||
'stock': article.stock,
|
|
||||||
},
|
|
||||||
'parent': {
|
|
||||||
'modelname': 'category',
|
|
||||||
'content': {
|
|
||||||
'id': article.category.id,
|
|
||||||
'name': article.category.name,
|
|
||||||
'has_addcost': article.category.has_addcost,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
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
|
@teamkfet_required
|
||||||
|
|
Loading…
Reference in a new issue