Skip to content

Commit

Permalink
Merge pull request #189 from romfabbro/hot_fix
Browse files Browse the repository at this point in the history
Hot fix : Equipment , RFID Import, Form Conf, improve alert message
  • Loading branch information
Matheo13 committed Feb 25, 2016
2 parents 8d0f516 + 2eff43a commit b80edb7
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 107 deletions.
22 changes: 22 additions & 0 deletions Back/database/Pipe/DB_Mother/035_Fix_Conf_protocol_Station.txt
Original file line number Diff line number Diff line change
@@ -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
146 changes: 66 additions & 80 deletions Back/ecoreleve_server/Models/Equipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.StartDate<equipDate)
)
)

query = select([Equipment]
).where(
and_(~exists(subQuery)
,and_(Equipment.StartDate < equipDate
,and_(Equipment.Deploy == 1,Equipment.FK_Sensor == Sensor.ID)
)
)
)

fullQuery = select([Sensor.ID]
).where(
and_(~exists(query),Sensor.ID == fk_sensor)
)

Nb = len(session.execute(fullQuery).fetchall())
if Nb>0:
return True
else:
return None

def checkEquip(fk_sensor,equipDate,fk_indiv=None,fk_site=None):
availableToEquip = True
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
43 changes: 26 additions & 17 deletions Back/ecoreleve_server/Views/RFIDimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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

10 changes: 7 additions & 3 deletions Back/ecoreleve_server/Views/monitoredSite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion Front/app/modules/importFile/rfid/lyt-step2-rfid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)'})
Expand Down
18 changes: 15 additions & 3 deletions Front/app/modules/monitoredSites/layouts/lyt-ms-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
3 changes: 3 additions & 0 deletions Front/app/modules/stations/layouts/lyt-observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion Front/app/ns_modules/ns_bbfe/bbfe-autocompTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"';
Expand Down
4 changes: 2 additions & 2 deletions Front/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit b80edb7

Please sign in to comment.