44import tornado .escape
55import tornado .web
66import tornado .websocket
7+ import tornado .httputil
78from . import Intranet
89from . import BaseHandler , BaseHandlerJson
910#from pyoctopart.octopart import Octopart
1314import datetime
1415import pandas as pd
1516from 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
1853def 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
660720class print_bom (BaseHandler ):
0 commit comments