Skip to content

Commit 468efd6

Browse files
committed
solve bug with submodules, allow to reupload BOM
1 parent 91c8369 commit 468efd6

4 files changed

Lines changed: 123 additions & 18 deletions

File tree

src/OpenIntranet/plugins/production.py

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import tornado.escape
55
import tornado.web
66
import tornado.websocket
7+
import tornado.httputil
78
from . import Intranet
89
from . import BaseHandler, BaseHandlerJson
910
#from pyoctopart.octopart import Octopart
@@ -13,6 +14,40 @@
1314
import datetime
1415
import pandas as pd
1516
from fpdf import FPDF
17+
from enum import Enum
18+
19+
20+
class ComponentStatus(Enum):
21+
Err = -1 # Nějaká chyba..
22+
Actual = 0 # polozka je nahrana z BOMu
23+
Obsolete = 1 # Byl nahran novy BOM, ale polozka se v nem nevyskytuje
24+
ModifiedManually = 2 # Polozka byla rucne upravena
25+
Failed = 3 # Tohle nastane napriklad v pripade, ze se zmeni UST_ID a hodnota zustane stejna
26+
27+
28+
ComponentStatusTable = {
29+
ComponentStatus.Err.value: {
30+
"label": "Chyba určení stavu položky",
31+
"color": "Gray"
32+
},
33+
ComponentStatus.Actual.value: {
34+
"label": "Položka je načtena z posledního BOMu",
35+
"color": "green",
36+
},
37+
ComponentStatus.Obsolete.value: {
38+
"label": "Položka může být zastaralá. Nebyla aktualizována při posledním nahrávání BOMu.",
39+
"color": "orange",
40+
},
41+
ComponentStatus.ModifiedManually.value: {
42+
"label": "Položka byla manuálně upravena",
43+
"color": "blue",
44+
},
45+
ComponentStatus.Failed.value: {
46+
"label": "Nějaká divnost",
47+
"color": "gray",
48+
}
49+
50+
}
1651

1752

1853
def get_plugin_handlers():
@@ -172,6 +207,7 @@ def get(self, name):
172207
'_id': {'UST_ID': '$components.UST_ID',
173208
'Value': '$components.Value',
174209
'Footprint': '$components.Footprint',
210+
'status': '$components.status',
175211
# 'Distributor': '$components.Distributor',
176212
# 'Datasheet': '$components.Datasheet',
177213
# 'MFPN': '$components.MFPN',
@@ -212,7 +248,8 @@ def get(self, name):
212248
dout = list(self.mdb.production.aggregate(query))
213249
#out = bson.json_util.dumps(dout)
214250

215-
self.render('production.bom_table.hbs', data = dout, bson=bson, current_warehouse = bson.ObjectId(self.get_cookie('warehouse')))
251+
252+
self.render('production.bom_table.hbs', data = dout, bson=bson, current_warehouse = bson.ObjectId(self.get_cookie('warehouse')), ComponentStatusTable = ComponentStatusTable)
216253

217254

218255
'''
@@ -600,7 +637,8 @@ def make_comp_dict(self, element):
600637
"Ref": element.get('ref'),
601638
"Value": element.findall('value')[0].text,
602639
"UST_ID": '',
603-
"stock_count": None
640+
"stock_count": None,
641+
"status": ComponentStatus.Actual.value
604642
}
605643

606644
update = {x.get('name'):x.get('value') for x in element.findall('property')}
@@ -616,13 +654,27 @@ def make_comp_dict(self, element):
616654

617655

618656
def post(self, name):
619-
data = (self.request.body.decode('utf-8'))
657+
658+
file_dic = {}
659+
arg_dic = {}
660+
661+
tornado.httputil.parse_body_arguments(self.request.headers["Content-Type"], self.request.body, arg_dic, file_dic)
662+
remove_obsolete = int(arg_dic['remove_obsolete'][0])
663+
# print(file_dic, arg_dic)
664+
665+
data = file_dic['file'][0]['body']
666+
#print("file", data)
620667

621668
from xml.etree import ElementTree
622669
root = ElementTree.fromstring(data)
623-
624670
components = root.findall('components')[0]
625671

672+
# Nastav, ze je polozka zastarala (obsolete)
673+
self.mdb.production.update(
674+
{"_id": bson.ObjectId(name)},
675+
{"$set": {"components.$[].status": ComponentStatus.Obsolete.value}}, multi=True
676+
)
677+
626678
for component_xml in components.iter('comp'):
627679
try:
628680
print("Nacitani soucastky")
@@ -635,26 +687,34 @@ def post(self, name):
635687

636688

637689
if exist.count() > 0:
690+
print("Update polozky")
638691
update = self.mdb.production.update(
639692
{
640693
'_id': bson.ObjectId(name),
641694
"components.Tstamp": component['Tstamp']
642695
},{
643-
"$set": component
644-
}, upsert = True)
696+
"$set": {"components.$": component},
697+
})
645698
else:
646699
print("NOVA POLOZKA")
700+
component['status'] = ComponentStatus.Actual.value
647701
update = self.mdb.production.update(
648702
{
649703
'_id': bson.ObjectId(name)
650704
},{
651-
"$push": {'components': component
652-
}
705+
"$push": {"components": component},
653706
})
654707

655708
except Exception as e:
656709
print("Problem s nactenim polozky:", e)
657710

711+
if remove_obsolete:
712+
self.mdb.production.update(
713+
{ "_id": bson.ObjectId(name) },
714+
{
715+
"$pull": { "components": { "status": ComponentStatus.Obsolete.value }},
716+
});
717+
658718
self.write("ok")
659719

660720
class print_bom(BaseHandler):

src/OpenIntranet/static/jsrender

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/OpenIntranet/templates/production.bom_table.hbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
<div><b>{{len(row['Ref'])}} &times;<b></div>
1010
</td>
1111
<td>
12-
{%for i_ref, ref in enumerate(row['Ref']) %}{%if i_ref > 0%}, {%end%} <span class="ref bom_table_ref">{{ref}}</span>{% end %}
12+
{%for i_ref, ref in enumerate(row['Ref']) %}{%if i_ref > 0%}, {%end%}
13+
{%set status = row.get('_id', {}).get('status', -1) %}
14+
{%set status_info = ComponentStatusTable[int(status)] %}
15+
<span class="ref bom_table_ref" data-toggle="tooltip" title="{{status_info['label']}}" style="color: {{status_info['color']}};">{{ref}}</span>{% end %}
1316
</td>
1417
<td>
1518
<div>
1619
<div class="table-row" style="display: table-row;"><label class="bom_table_key"> </label></div>
1720
<div class="table-row" style="display: table-row;"><label class="bom_table_key">Value:</label><span class="bom_table_value">{{row['_id']['Value']}}<span></div>
1821
<div class="table-row" style="display: table-row;"><label class="bom_table_key">Footprint:</label><span class="bom_table_value">{{row['_id']['Footprint']}}</span></div>
19-
2022
</div>
2123

2224
</td>

src/OpenIntranet/templates/production.flow.hbs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ F{% extends "base.hbs" %}
132132

133133
<div class="col-4 card" style="padding: 0pt;">
134134
<ul class="list-group list-group-flush">
135-
<a class="list-group-item text-muted" href="#" ><i class="bi bi-cloud-upload"></i> Nahrát data</a>
135+
<a class="list-group-item" href="#" onclick="$('#modal_load_bom').modal('show')"><i class="bi bi-cloud-upload"></i> Nahrát data (BOM)</a>
136136
<a class="list-group-item" href="./../print/" ><i class="bi bi-printer"></i> Tisknout osazovák</a>
137137
<a class="list-group-item text-muted" href="#" ><i class="bi bi-display"></i> Zobrazit gerber</a>
138138
<a class="list-group-item" href="#"><i class="bi bi-question-square"></i> Nastavit počet kusů <input type="number" id="production_multiplication" value="1" min="1" step="1" style="border: solid gray 1px;width: 5em;text-align: center;padding: 0pt;margin-left: 2em;" onchange="count_validation($(this).val())"></a>
@@ -149,6 +149,8 @@ F{% extends "base.hbs" %}
149149

150150

151151
Cenotvorba - cena matiriálu, cena práce, počet komponent, ....
152+
153+
152154
</div>
153155
</div>
154156
</div>
@@ -183,20 +185,54 @@ F{% extends "base.hbs" %}
183185
</div>
184186
</div>
185187

186-
<div class="col-12">
188+
<!-- <div class="col-12">
187189
<div class="card">
188190
XML netlist:
189191
<input type="file" name="bom" id="ust_bom_file"/>
190192
<input type="submit" value="upload" id="ust_bom_btn" onclick="startUpload()" />
191-
192-
193193
</div>
194+
</div> -->
195+
</div>
196+
194197

198+
199+
<div class="modal" id="modal_load_bom" tabindex="-1">
200+
<div class="modal-dialog">
201+
<div class="modal-content">
202+
<div class="modal-header">
203+
<h5 class="modal-title">Importování BOMu (.xml netlist)</h5>
204+
</div>
205+
<div class="modal-body">
206+
<div id="bom_import_step_1">
207+
<label for="formFile" class="form-label">Vyberte soubor:</label>
208+
209+
<div class="input-group mb-3">
210+
<input class="form-control" type="file" id="ust_bom_file" name='bom'>
211+
<input class="btn btn-outline-success" type="button" id="ust_bom_btn" name='' onclick="startUpload()" value="Nahrát">
212+
</div>
213+
<div class="form-check form-switch">
214+
<label class="form-check-label" for="flexSwitchCheckDefault">Odstranit neaktuální položky</label>
215+
<input class="form-check-input" type="checkbox" id="ust_bom_remove_obsolete">
216+
</div>
195217
</div>
218+
219+
<div id="bom_import_step_2" style="display: none">
220+
Loading
221+
</div>
222+
223+
<div id="bom_import_step_3" style="display: none">
224+
Loading
225+
</div>
226+
</div>
227+
<!-- <div class="modal-footer">
228+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
229+
<button type="button" class="btn btn-primary">Save changes</button>
230+
</div> -->
231+
</div>
232+
</div>
196233
</div>
197234

198235

199-
200236
<!-- NOVÉ POCITANI SOUCASTEK -->
201237
<script type="text/javascript">
202238
@@ -618,13 +654,20 @@ var ust_id_autocomplete = function(cell, onRendered, success, cancel){
618654
619655
function startUpload() {
620656
var fileInput = document.getElementById("ust_bom_file");
657+
//console.log(fileInput.files[0])
658+
659+
var fd = new FormData();
660+
fd.append('remove_obsolete', Number($("#ust_bom_remove_obsolete").is(':checked')) );
661+
fd.append('remove_edited', 0 );
662+
fd.append('file', fileInput.files[0]);
621663
622664
if (fileInput.files.length == 0) {
623665
alert("Please choose a file");
624666
return;
625667
}
626668
627669
var xhr = new XMLHttpRequest();
670+
xhr.withCredentials = true;
628671
629672
xhr.onload = function() {
630673
if (xhr.status == 200) {
@@ -639,8 +682,9 @@ function startUpload() {
639682
};
640683
641684
xhr.open("POST", "/production/{{id}}/upload/bom/ust/", true);
642-
xhr.setRequestHeader("Content-Type", fileInput.files[0].type);
643-
xhr.send(fileInput.files[0]);
685+
//xhr.setRequestHeader("Content-Type", "multipart/form-data");
686+
xhr.send(fd);
687+
// xhr.send(fileInput.files[0]+"&remove_obsolete="+$("#ust_bom_remove_obsolete").is(':checked'));
644688
645689
load_components();
646690
}

0 commit comments

Comments
 (0)