Skip to content

Commit

Permalink
Merge pull request #121 from romfabbro/protocols
Browse files Browse the repository at this point in the history
generic autocomplete + fix release
  • Loading branch information
romfabbro committed Nov 26, 2015
2 parents c736a12 + db84ef5 commit 76590a0
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 113 deletions.
10 changes: 6 additions & 4 deletions Back/ecoreleve_server/GenericObjets/FrontModules.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ def InputThesaurus(self) :
def InputAutocomplete(self):
if self.Options is not None and self.Options != '':
option = json.loads(self.Options)
result = DBSession.execute(text(option['source'])).fetchall()
self.dto['options']= {'source':[]}
for row in result:
self.dto['options']['source'].append(row[0])
if 'SELECT' in option['source']:
result = DBSession.execute(text(option['source'])).fetchall()
for row in result:
self.dto['options']['source'].append(row[0])
else :
self.dto['options'] = {'source': option['source'],'minLength' :option['minLength']}


func_type_context = {
Expand Down
6 changes: 2 additions & 4 deletions Back/ecoreleve_server/Views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ def add_routes(config):
##### User #####
config.add_route('core/user', 'ecoReleve-Core/user')
config.add_route('core/currentUser', 'ecoReleve-Core/currentUser')
config.add_route('autocomplete', 'ecoReleve-Core/autocomplete/{obj}/{prop}')


# ------------------------------------------------------------------------------------------------------------------------- #
##### Stations #####
config.add_route('area', 'ecoReleve-Core/area')
config.add_route('locality', 'ecoReleve-Core/locality')
config.add_route('stations', 'ecoReleve-Core/stations/')
config.add_route('stations/autocomplete', 'ecoReleve-Core/stations/autocomplete/{prop}')
#config.add_route('stations/fileImport', 'ecoReleve-Core/stations/fileImport/{id}')
config.add_route('stations/id', 'ecoReleve-Core/stations/{id}',custom_predicates = (integers('id'),))
config.add_route('stations/action', 'ecoReleve-Core/stations/{action}')
Expand Down Expand Up @@ -82,7 +83,6 @@ def add_routes(config):
##### Sensors caracteristics(Argos + GSM + RFID) #####

config.add_route('sensors', 'ecoReleve-Core/sensors/')
config.add_route('sensors/autocomplete', 'ecoReleve-Core/sensors/autocomplete/{prop}')
config.add_route('sensors/insert', 'ecoReleve-Core/sensors')
config.add_route('sensors/export', 'ecoReleve-Core/sensors/export')
config.add_route('sensors/id', 'ecoReleve-Core/sensors/{id}',custom_predicates = (integers('id'),))
Expand All @@ -93,7 +93,6 @@ def add_routes(config):
# ------------------------------------------------------------------------------------------------------------------------- #
##### Individuals #####
config.add_route('individuals', 'ecoReleve-Core/individuals/')
config.add_route('individuals/autocomplete', 'ecoReleve-Core/individuals/autocomplete/{prop}')
config.add_route('individuals/insert', 'ecoReleve-Core/individuals')
config.add_route('individuals/id', 'ecoReleve-Core/individuals/{id}',custom_predicates = (integers('id'),))
config.add_route('individuals/id/history', 'ecoReleve-Core/individuals/{id}/history',custom_predicates = (integers('id'),))
Expand All @@ -105,7 +104,6 @@ def add_routes(config):
# ------------------------------------------------------------------------------------------------------------------------- #
##### MonitoredSite #####
config.add_route('monitoredSite', 'ecoReleve-Core/monitoredSites/')
config.add_route('monitoredSite/autocomplete', 'ecoReleve-Core/monitoredSites/autocomplete/{prop}')
config.add_route('monitoredSite/', 'ecoReleve-Core/monitoredSites')
config.add_route('monitoredSite/id', 'ecoReleve-Core/monitoredSites/{id}',custom_predicates = (integers('id'),))
config.add_route('monitoredSite/id/history', 'ecoReleve-Core/monitoredSites/{id}/history/',custom_predicates = (integers('id'),))
Expand Down
39 changes: 39 additions & 0 deletions Back/ecoreleve_server/Views/autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pyramid.view import view_config
from ..Models import (
DBSession,
Base
)
import transaction
from sqlalchemy import select, and_,cast, DATE,func,asc
from pyramid.security import NO_PERMISSION_REQUIRED

dictObj = {
'stations':'Station',
'sensors':'Sensor',
'individuals':'Individual',
'monitoredSites':'MonitoredSite',
'users':'User',
'regions':'Region'
}

def asInt(str):
try : return int(str)
except : return str

@view_config(route_name= 'autocomplete', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED )
def autocomplete (request):
objName = dictObj[request.matchdict['obj']]
criteria = request.params['term']
prop = asInt(request.matchdict['prop'])

if isinstance(prop,int):
table = Base.metadata.tables[objName+'DynPropValuesNow']
query = select([table.c['ValueString'].label('label'),table.c['ValueString'].label('value')]
).where(table.c['FK_'+objName+'DynProp']== prop)
query = query.where(table.c['ValueString'].like('%'+criteria+'%')).order_by(asc(table.c['ValueString']))
else:
table = Base.metadata.tables[objName]
query = select([table.c[prop].label('value'),table.c[prop].label('label')])
query = query.where(table.c[prop].like('%'+criteria+'%')).order_by(asc(table.c[prop]))

return [dict(row) for row in DBSession.execute(query).fetchall()]
15 changes: 0 additions & 15 deletions Back/ecoreleve_server/Views/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,6 @@ def getIndividualType(request):
response = [ OrderedDict(row) for row in DBSession.execute(query).fetchall()]
return response


# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/autocomplete', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED )
def autocomplete (request):
criteria = request.params['term']
prop = request.matchdict['prop']

table = Base.metadata.tables['IndividualDynPropValuesNow']
query = select([table.c['ValueString'].label('label'),table.c['ValueString'].label('value')]
).where(table.c['FK_IndividualDynProp']== prop)
query = query.where(table.c['ValueString'].like('%'+criteria+'%')).order_by(asc(table.c['ValueString']))

return [dict(row) for row in DBSession.execute(query).fetchall()]


# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/id', renderer='json', request_method = 'GET')
def getIndiv(request):
Expand Down
17 changes: 0 additions & 17 deletions Back/ecoreleve_server/Views/monitoredSite.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,6 @@ def getMonitoredSiteType(request):
response = [ OrderedDict(row) for row in DBSession.execute(query).fetchall()]
return response

# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/autocomplete', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED )
def autocomplete (request):
criteria = request.params['term']
prop = request.matchdict['prop']
if isinstance(prop,int):
table = Base.metadata.tables['MontoredSiteDynPropValuesNow']
query = select([table.c['ValueString'].label('label'),table.c['ValueString'].label('value')]
).where(table.c['FK_MontoredSiteDynProp']== prop)
query = query.where(table.c['ValueString'].like('%'+criteria+'%')).order_by(asc(table.c['ValueString']))
else:
table = Base.metadata.tables['MontoredSite']
query = select([table.c[prop].label('value'),table.c[prop].label('label')])
query = query.where(table.c[prop].like('%'+criteria+'%'))

return [dict(row) for row in DBSession.execute(query).fetchall()]

# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/id', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED)
def getMonitoredSite(request):
Expand Down
16 changes: 0 additions & 16 deletions Back/ecoreleve_server/Views/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,6 @@ def getSensorType(request):
transaction.commit()
return response

# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/autocomplete', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED )
def autocomplete (request):
criteria = request.params['term']
prop = request.matchdict['prop']
if isinstance(prop,int):
table = Base.metadata.tables['SensorDynPropValuesNow']
query = select([table.c['ValueString'].label('label'),table.c['ValueString'].label('value')]
).where(table.c['FK_SensorDynProp']== prop)
query = query.where(table.c['ValueString'].like('%'+criteria+'%')).order_by(asc(table.c['ValueString']))
else:
table = Base.metadata.tables['Sensor']
query = select([table.c[prop].label('value'),table.c[prop].label('label')])
query = query.where(table.c[prop].like('%'+criteria+'%'))
return [dict(row) for row in DBSession.execute(query).fetchall()]

# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/id', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED)
def getSensor(request):
Expand Down
18 changes: 0 additions & 18 deletions Back/ecoreleve_server/Views/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ def getFields(request) :
transaction.commit()
return cols


# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/autocomplete', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED )
def autocomplete (request):
criteria = request.params['term']
prop = request.matchdict['prop']
if isinstance(prop,int):
table = Base.metadata.tables['StationDynPropValuesNow']
query = select([table.c['ValueString'].label('label'),table.c['ValueString'].label('value')]
).where(table.c['FK_StationDynProp']== prop)
query = query.where(table.c['ValueString'].like('%'+criteria+'%')).order_by(asc(table.c['ValueString']))
else:
table = Base.metadata.tables['Station']
query = select([table.c[prop].label('value'),table.c[prop].label('label')])
query = query.where(table.c[prop].like('%'+criteria+'%'))

return [dict(row) for row in DBSession.execute(query).fetchall()]

# ------------------------------------------------------------------------------------------------------------------------- #
@view_config(route_name= prefix+'/id', renderer='json', request_method = 'GET',permission = NO_PERMISSION_REQUIRED)
def getStation(request):
Expand Down
5 changes: 4 additions & 1 deletion Back/ecoreleve_server/Views/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def location_graph(request):

for row in DBSession.execute(query).fetchall() :
curRow = OrderedDict(row)
data.append({'value':curRow['nb'],'label':curRow['type_'].upper()})
lab = curRow['type_'].upper()
if lab == 'ARG':
lab = 'ARGOS'
data.append({'value':curRow['nb'],'label':lab})
data.sort(key = itemgetter('label'))
transaction.commit()
return data
Expand Down
18 changes: 15 additions & 3 deletions Back/ecoreleve_server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
import datetime
from decimal import Decimal
import transaction
from urllib.parse import quote_plus
Expand Down Expand Up @@ -34,7 +34,17 @@

def datetime_adapter(obj, request):
"""Json adapter for datetime objects."""
return obj.strftime ('%d/%m/%Y %H:%M:%S')
try:
return obj.strftime ('%d/%m/%Y %H:%M:%S')
except :
return obj.strftime ('%d/%m/%Y')

def time_adapter(obj, request):
"""Json adapter for datetime objects."""
try:
return obj.strftime('%H:%M')
except:
return obj.strftime('%H:%M:%S')

def decimal_adapter(obj, request):
"""Json adapter for Decimal objects."""
Expand All @@ -58,8 +68,10 @@ def main(global_config, **settings):
config = Configurator(settings=settings)
# Add renderer for datetime objects
json_renderer = JSON()
json_renderer.add_adapter(datetime, datetime_adapter)
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
json_renderer.add_adapter(datetime.date, datetime_adapter)
json_renderer.add_adapter(Decimal, decimal_adapter)
json_renderer.add_adapter(datetime.time, time_adapter)
config.add_renderer('json', json_renderer)

# Add renderer for CSV, PDF,GPX files.
Expand Down
2 changes: 1 addition & 1 deletion Back/ecoreleve_server/renderers/csvrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __call__(self, value, system):
response.content_type = 'text/csv'

fout = io.StringIO()
writer = csv.writer(fout, delimiter=',', quoting=csv.QUOTE_MINIMAL)
writer = csv.writer(fout, delimiter=';', quoting=csv.QUOTE_MINIMAL)

writer.writerow(value.get('header', []))
writer.writerows(value.get('rows', []))
Expand Down
43 changes: 25 additions & 18 deletions Back/ecoreleve_server/utils/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,33 @@ def get_geoJSON(self,criteria={}) :
result=[]
total=None
countResult = self.count_(criteria)
if(countResult <= 50000):
query = self.getFullQuery(criteria)
try :
data=DBSession.execute(query.where(self.table.c['LAT'] != None)).fetchall()
except :
data=DBSession.execute(query.where(self.table.c['lat'] != None)).fetchall()
tmp = data[0]
lat = self.case(tmp, 'LAT')
lon = self.case(tmp, 'LON')
geoJson=[]
for row in data:
properties = {}
# if cols_for_properties != None :
# for col in cols_for_properties :
# properties[col.replace('_',' ')] = row[col]
geoJson.append({'type':'Feature', 'properties':properties, 'geometry':{'type':'Point', 'coordinates':[row[lat],row[lon]]}})
transaction.commit()
return {'type':'FeatureCollection', 'features': geoJson, 'exceed': False, 'total':countResult}

if 'lat' in self.table.c or 'LAT'in self.table.c:
if(countResult <= 50000):
query = self.getFullQuery(criteria)
try :
data=DBSession.execute(query.where(self.table.c['LAT'] != None)).fetchall()
except :
try:
data=DBSession.execute(query.where(self.table.c['lat'] != None)).fetchall()
except:
pass

tmp = data[0]
lat = self.case(tmp, 'LAT')
lon = self.case(tmp, 'LON')
geoJson=[]
for row in data:
properties = {}
# if cols_for_properties != None :
# for col in cols_for_properties :
# properties[col.replace('_',' ')] = row[col]
geoJson.append({'type':'Feature', 'properties':properties, 'geometry':{'type':'Point', 'coordinates':[row[lat],row[lon]]}})
transaction.commit()
return {'type':'FeatureCollection', 'features': geoJson, 'exceed': False, 'total':countResult}
else :
return {'type':'FeatureCollection', 'features': [],'exceed': True, 'total':countResult}


def case(self, row, arg) :
if( arg in row ) :
Expand Down
Loading

0 comments on commit 76590a0

Please sign in to comment.