diff --git a/Back/database/Pipe/DB_Mother/035_Fix_Conf_protocol_Station.txt b/Back/database/Pipe/DB_Mother/035_Fix_Conf_protocol_Station.txt new file mode 100644 index 000000000..8c56698c5 --- /dev/null +++ b/Back/database/Pipe/DB_Mother/035_Fix_Conf_protocol_Station.txt @@ -0,0 +1,22 @@ +UPDATE f set Name = 'weight' + FROM [EcoReleve_ECWP].[dbo].[ModuleForms] f + where TypeObj = 222 and name = 'Weight' + +UPDATE f set Name = 'picture' + FROM [EcoReleve_ECWP].[dbo].[ModuleForms] f + where TypeObj = 225 AND Name = 'Picture' + +UPDATE f SET TypeObj = NULL +FROM ModuleForms f +WHERE module_id = 2 and Name = 'FK_Region' + +UPDATE [EcoReleve_ECWP].[dbo].[ModuleForms] SET Legend = NULL +WHERE TypeObj = 218 and module_id = 1 + +GO + + +INSERT INTO [dbo].[TVersion] (TVer_FileName,TVer_Date,TVer_DbName) VALUES ('35_Fix_Conf_protocol_Station',GETDATE(),(SELECT db_name())) + + +GO diff --git a/Back/ecoreleve_server/Models/Equipment.py b/Back/ecoreleve_server/Models/Equipment.py index b89ff0827..504231f67 100644 --- a/Back/ecoreleve_server/Models/Equipment.py +++ b/Back/ecoreleve_server/Models/Equipment.py @@ -54,55 +54,43 @@ def linkProperty(self,StartDate,**kwargs): def checkSensor(fk_sensor,equipDate,fk_indiv=None,fk_site=None): session = threadlocal.get_current_registry().dbmaker() - # session = threadlocal.get_current_request().dbsession - # if fk_indiv is not None : - # table = Base.metadata.tables['IndividualEquipment'] - # fk = 'FK_Individual' - # else : - # table = Base.metadata.tables['MonitoredSiteEquipment'] - # fk = 'MonitoredSite' - - # subQuery = select(table.c - # ).where(table.c['FK_Sensor'] == fk_sensor - # ).where(table.c['StartDate'] < equipDate - # ).where(or_(table.c['EndDate'] == None,table.c['EndDate'] > equipDate )) - - # fullQuery = select([True]).where(~exists(subQuery)) - # sensorEquip = session.execute(fullQuery).scalar() - # print('***************************CHECKSENSOR***********************') - # # return sensorEquip + e2 = aliased(Equipment) + # curQuery = "select * from Sensor S " - # print(curQuery) - # print(equipDate) + # curQuery += "where not exists (select * from Equipment E " - # print(curQuery) - # print(equipDate) + # curQuery += "WHERE NOT EXISTS (select * from Equipment E2 where E2.FK_Sensor = E.FK_Sensor and E2.StartDate > E.StartDate and e2.StartDate < convert(datetime,'" + equipDate.strftime("%Y/%m/%d %H:%M:%S") + "',103) )" - # print(curQuery) - # print(fk_sensor) - # print('curQuery') + # curQuery += "AND e.FK_Sensor =s.ID and e.Deploy = 1 and e.StartDate < convert(datetime,'" + equipDate.strftime("%Y/%m/%d %H:%M:%S") + "',103) ) AND S.ID=" + str(fk_sensor) # print(curQuery) - # Nb = len(session.execute(curQuery).fetchall()) - # print (Nb) - # if Nb>0: - # return True - # else: - # return False - e1 = aliased(Equipment) - - subQuery = select([e1]).where(and_(e1.FK_Sensor == Equipment.FK_Sensor - ,and_(e1.StartDate>Equipment.StartDate,e1.StartDate<=equipDate))) - - query = select([Equipment]).where(and_(~exists(subQuery) - ,and_(Equipment.StartDate<=equipDate - ,and_(Equipment.Deploy == 0,Equipment.FK_Sensor == fk_sensor)))) - fullQuery = select([True]).where(or_(exists(query),~exists(select([Equipment]).where(Equipment.FK_Sensor==fk_sensor) ) ) ) - - sensorEquip = session.execute(fullQuery).scalar() - # session.close() - return sensorEquip + subQuery = select([e2] + ).where( + and_(e2.FK_Sensor == Equipment.FK_Sensor + ,and_(e2.StartDate > Equipment.StartDate,e2.StartDate0: + return True + else: + return None def checkEquip(fk_sensor,equipDate,fk_indiv=None,fk_site=None): availableToEquip = True @@ -138,39 +126,34 @@ def existingEquipment (fk_sensor,equipDate,fk_indiv=None): def alreadyUnequip (fk_sensor,equipDate,fk_indiv=None,fk_site=None): session = threadlocal.get_current_request().dbsession - objToUnequip = None - - if fk_indiv is None: - table = Base.metadata.tables['MonitoredSiteEquipment'] - objToUnequip = 'FK_MonitoredSite' - val = fk_site - else: - table = Base.metadata.tables['IndividualEquipment'] - objToUnequip = 'FK_Individual' - val = fk_indiv - - # e1 = aliased(Equipment) - # e2 = aliased(Equipment) - # subQueryExists = select([e1]).where(and_(e1.FK_Sensor == Equipment.FK_Sensor - # ,and_(e1.__table__.c[objToUnequip] == Equipment.__table__.c[objToUnequip] - # ,and_(e1.StartDate>Equipment.StartDate,e1.StartDate<=equipDate)))) - - # query = select([Equipment]).where(and_(~exists(subQueryExists) - # ,and_(Equipment.StartDate<=equipDate,and_(Equipment.Deploy == 1 - # ,and_(Equipment.FK_Sensor == fk_sensor,Equipment.__table__.c[objToUnequip] == fk_indiv))))) - - # subQueryUnequip = select([e2]).where(and_(e2.__table__.c[objToUnequip] == Equipment.__table__.c[objToUnequip] - # ,and_(e2.StartDate>Equipment.StartDate - # ,and_(e2.FK_Sensor == Equipment.FK_Sensor,and_(e2.Deploy == 0,e2.StartDate<=equipDate))))) - - # query = query.where(~exists(subQueryUnequip)) - query = select(table.c - ).where(table.c['FK_Sensor'] == fk_sensor - ).where(table.c[objToUnequip] == val - ).where(table.c['EndDate'] == None) - fullQuery = select([True]).where(~exists(query)) + e1 = aliased(Equipment) + e2 = aliased(Equipment) + + subQuery = select([e1] + ).where( + and_(e1.FK_Sensor == Equipment.FK_Sensor, + and_(e1.FK_Individual == Equipment.FK_Individual, + and_(e1.FK_MonitoredSite == Equipment.FK_MonitoredSite, + and_(e1.StartDate>Equipment.StartDate,e1.StartDate<=equipDate) + ) + ) + ) + ) + + query = select([Equipment]).where( + and_(~exists(subQuery), + and_(Equipment.StartDate<=equipDate, + and_(Equipment.Deploy == 0, + and_(Equipment.FK_Sensor == fk_sensor, + and_(Equipment.FK_Individual == fk_indiv,Equipment.FK_MonitoredSite == fk_site) + ) + ) + ) + ) + ) + fullQuery = select([True]).where(exists(query)) result = session.execute(fullQuery).scalar() # session.close() return result @@ -225,6 +208,8 @@ def set_equipment(target, value=None, oldvalue=None, initiator=None): elif 'site' in typeName.lower(): fk_site = target.Station.GetProperty('FK_MonitoredSite') fk_indiv = None + if fk_site is None : + raise ErrorAvailable({'errorSite':True}) if deploy == 1 : availability = checkEquip(fk_sensor=fk_sensor @@ -250,15 +235,16 @@ def set_equipment(target, value=None, oldvalue=None, initiator=None): def unlinkEquipement(mapper, connection, target): session = threadlocal.get_current_request().dbsession # session = threadlocal.get_current_registry().dbmaker() + if target.FK_Individual is not None and target.Deploy == 1: + curIndiv = session.query(Individual).get(target.FK_Individual) - curIndiv = session.query(Individual).get(target.FK_Individual) - curSensor = session.query(Sensor).get(target.FK_Sensor) + curSensor = session.query(Sensor).get(target.FK_Sensor) - dynPropToDel = curIndiv.GetDynPropWithDate(['Survey_type','Monitoring_Status'],target.StartDate) - dynPropToDel.append(curSensor.GetDynPropWithDate('Status',target.StartDate)) + dynPropToDel = curIndiv.GetDynPropWithDate(['Survey_type','Monitoring_Status'],target.StartDate) + dynPropToDel.append(curSensor.GetDynPropWithDate('Status',target.StartDate)) - for dynprop in dynPropToDel: - session.delete(dynprop) + for dynprop in dynPropToDel: + session.delete(dynprop) class ErrorAvailable(Exception): def __init__(self, value): diff --git a/Back/ecoreleve_server/Views/RFIDimport.py b/Back/ecoreleve_server/Views/RFIDimport.py index 9c28a742f..8ba2a454f 100644 --- a/Back/ecoreleve_server/Views/RFIDimport.py +++ b/Back/ecoreleve_server/Views/RFIDimport.py @@ -27,12 +27,11 @@ def uploadFileRFID(request): isHead = False now=datetime.now() try: - creator = request.authenticated_userid['iss'] + creator = int(request.authenticated_userid['iss']) content = request.POST['data'] - idModule = request.POST['FK_Sensor'] + idModule = int(request.POST['FK_Sensor']) startEquip = request.POST['StartDate'] endEquip = request.POST['EndDate'] - creator = request.authenticated_userid['iss'] if re.compile('\r\n').search(content): data = content.split('\r\n') @@ -122,22 +121,27 @@ def uploadFileRFID(request): j=j+1 data_to_check = pd.DataFrame.from_records(list_RFID,columns = ['id_','FK_Sensor','date_','chip_code','creator','creation_date','validated','checked']) - maxDateEquip = datetime.fromtimestamp(int(startEquip)) + minDateEquip = datetime.fromtimestamp(int(startEquip)) try : - minDateEquip = datetime.fromtimestamp(int(endEquip)) + maxDateEquip = datetime.fromtimestamp(int(endEquip)) except: - minDateEquip = None + maxDateEquip = None ## check if Date corresponds with pose remove module ## - if min(allDate)>= maxDateEquip and (minDateEquip is None or max(allDate)<= minDateEquip): + if min(allDate)>= minDateEquip and (maxDateEquip is None or max(allDate)<= maxDateEquip): - # data_to_insert = checkDuplicatedRFID(data_to_check,min(allDate),max(allDate)) - # Rfids = [{Rfid.creator.name: crea, Rfid.FK_Sensor.name: idMod, Rfid.checked.name: '0', - # Rfid.chip_code.name: c, Rfid.date_.name: d, Rfid.creation_date.name: now} for crea, idMod, c, d in Rfids] + data_to_insert = checkDuplicatedRFID(data_to_check,minDateEquip,maxDateEquip,idModule) + Rfids = [{Rfid.creator.name: crea, Rfid.FK_Sensor.name: idMod, Rfid.checked.name: '0', + Rfid.chip_code.name: c, Rfid.date_.name: d, Rfid.creation_date.name: now} for crea, idMod, c, d in Rfids] # # Insert data. # session.execute(insert(Rfid), Rfids) - data_to_check = data_to_check.drop(['id_'],1) - data_to_check.to_sql(Rfid.__table__.name, session.get_bind(), if_exists='append', schema = dbConfig['sensor_schema'], index=False) + data_to_insert = data_to_insert.drop(['id_'],1) + data_to_insert = data_to_insert.drop_duplicates() + + if data_to_insert.shape[0] == 0: + raise(IntegrityError) + + data_to_insert.to_sql(Rfid.__table__.name, session.get_bind(), if_exists='append', schema = dbConfig['sensor_schema'], index=False) message = 'inserted rows : '+str(len(Rfids)) return message @@ -152,17 +156,22 @@ def uploadFileRFID(request): # if len(unknown_chips) > 0: # message += '\n\nWarning : chip codes ' + str(unknown_chips) + ' are unknown.' except IntegrityError as e: - request.response.status_code = 500 + print_exc() + request.response.status_code = 520 message = 'Data already exist.' except Exception as e: print_exc() - request.response.status_code = 520 + request.response.status_code = 500 message = 'Error' return message -def checkDuplicatedRFID(data_to_check,startEquip,endEquip): +def checkDuplicatedRFID(data_to_check,startEquip,endEquip,fk_sensor): session1 = threadlocal.get_current_registry().dbmaker() - query = select([Rfid]).where(and_(Rfid.date_>=startEquip,Rfid.date_<=endEquip)) + query = select([Rfid] + ).where( + and_(Rfid.date_ >= startEquip + ,and_(Rfid.date_<=endEquip,Rfid.FK_Sensor == fk_sensor)) + ) result = session1.execute(query).fetchall() existingData = pd.DataFrame.from_records(result, @@ -179,7 +188,7 @@ def checkDuplicatedRFID(data_to_check,startEquip,endEquip): merge = data_to_check.merge(existingData, left_on = ['FK_Sensor'], right_on = ['$FK_Sensor']) DFToInsert = data_to_check[~data_to_check['id_'].isin(merge['id_'])] - session1.close() + # session1.close() print(DFToInsert) return DFToInsert diff --git a/Back/ecoreleve_server/Views/monitoredSite.py b/Back/ecoreleve_server/Views/monitoredSite.py index 02f747057..9d015ebf1 100644 --- a/Back/ecoreleve_server/Views/monitoredSite.py +++ b/Back/ecoreleve_server/Views/monitoredSite.py @@ -225,12 +225,16 @@ def insertOneNewMonitoredSite (request) : newMonitoredSite.UpdateFromJson(data) session.add(newMonitoredSite) session.flush() - response = {'ID': newMonitoredSite.ID} - except Exception as e: + + except IntegrityError as e: + session.rollback() + request.response.status_code = 520 response = request.response response.text = "This name is already used for another monitored site" - return response + pass + + return response # ------------------------------------------------------------------------------------------------------------------------- # @view_config(route_name= prefix, renderer='json', request_method = 'GET') diff --git a/Front/app/modules/importFile/rfid/lyt-step2-rfid.js b/Front/app/modules/importFile/rfid/lyt-step2-rfid.js index 37874ee48..be0c6ed0e 100644 --- a/Front/app/modules/importFile/rfid/lyt-step2-rfid.js +++ b/Front/app/modules/importFile/rfid/lyt-step2-rfid.js @@ -127,7 +127,7 @@ define([ }).fail(function(data) { $('#btnNext').attr('disabled'); - if (data.status == 500 || data.status == 510) { + if (data.status == 520 || data.status == 510) { var type = 'warning'; var title = 'Warning !' self.ui.progressBar.css({'background-color': 'rgb(218, 146, 15)'}) diff --git a/Front/app/modules/monitoredSites/layouts/lyt-ms-new.js b/Front/app/modules/monitoredSites/layouts/lyt-ms-new.js index 89adf75e4..bc6a52db5 100644 --- a/Front/app/modules/monitoredSites/layouts/lyt-ms-new.js +++ b/Front/app/modules/monitoredSites/layouts/lyt-ms-new.js @@ -62,12 +62,24 @@ define([ ); }, savingError: function(response) { + var type; + var msg; + var color; + if (response.status == 520) { + type = 'warning'; + msg = 'This name is already used for another monitored site'; + color = 'rgb(218, 146, 15)'; + } else { + type = 'error'; + msg = 'an unknow error ooccured'; + color = 'rgb(147, 14, 14)'; + } Swal({ title: 'Error', - text: 'creating a new monitored site', - type: 'error', + text: msg, + type: type, showCancelButton: false, - confirmButtonColor: 'rgb(147, 14, 14)', + confirmButtonColor: color, confirmButtonText: 'OK', closeOnConfirm: true, } diff --git a/Front/app/modules/stations/layouts/lyt-observation.js b/Front/app/modules/stations/layouts/lyt-observation.js index e1a59d187..efdf991a1 100644 --- a/Front/app/modules/stations/layouts/lyt-observation.js +++ b/Front/app/modules/stations/layouts/lyt-observation.js @@ -70,6 +70,9 @@ define([ else if(response.responseJSON.existing_equipment == false ){ _this.sweetAlert('Data saving error', 'error', 'Selected sensor is not equiped with this individual'); } + else if(response.responseJSON.errorSite == true ){ + _this.sweetAlert('Data saving error', 'error', 'No monitored site is attached'); + } } }); diff --git a/Front/app/ns_modules/ns_bbfe/bbfe-autocompTree.js b/Front/app/ns_modules/ns_bbfe/bbfe-autocompTree.js index 10ede3fde..cc58fc779 100644 --- a/Front/app/ns_modules/ns_bbfe/bbfe-autocompTree.js +++ b/Front/app/ns_modules/ns_bbfe/bbfe-autocompTree.js @@ -51,7 +51,7 @@ define([ this.isTermError = false; Form.editors.Base.prototype.initialize.call(this, options); this.template = options.template || this.constructor.template; - this.id = options.id; + this.id = options.id + options.form.cid; var editorAttrs = ""; if (options.schema.editorAttrs && options.schema.editorAttrs.disabled) { editorAttrs += 'disabled="disabled"'; diff --git a/Front/bower.json b/Front/bower.json index 06d055eb9..bcc3982dd 100644 --- a/Front/bower.json +++ b/Front/bower.json @@ -14,7 +14,7 @@ "sweetalert": "~0.5.0", "backbone.paginator": "~2.0.2", "backgrid-select-all": "~0.3.5", - "moment": "~2.10.2", + "moment": "~2.10.6", "backbone-forms": "https://github.com/powmedia/backbone-forms.git#~0.14.0", "eonasdan-bootstrap-datetimepicker": "~4.7.14", "backgrid-paginator": "~0.3.5", @@ -30,7 +30,7 @@ "tooltipster": "~3.3.0", "chartjs": "~1.0.2", "reneco-fonts": "*", - "NaturalJS_Filter":"1.0.2" + "NaturalJS_Filter":"1.0.3" }, "resolutions": { "jquery": "~1.10.2",