本文整理汇总了Python中skylines.model.session.DBSession.query方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.query方法的具体用法?Python DBSession.query怎么用?Python DBSession.query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skylines.model.session.DBSession
的用法示例。
在下文中一共展示了DBSession.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: guess_model
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def guess_model(self):
from skylines.model import Flight, AircraftModel
# first try to find the reg number in the database
if self.registration is not None:
glider_reg = self.registration
result = DBSession.query(Flight) \
.filter(func.upper(Flight.registration) == func.upper(glider_reg)) \
.order_by(desc(Flight.id)) \
.first()
if result and result.model_id:
return result.model_id
# try to find another flight with the same logger and use it's aircraft type
if (self.logger_id is not None
and self.logger_manufacturer_id is not None):
logger_id = self.logger_id
logger_manufacturer_id = self.logger_manufacturer_id
result = DBSession.query(Flight).outerjoin(IGCFile) \
.filter(func.upper(IGCFile.logger_manufacturer_id) == func.upper(logger_manufacturer_id)) \
.filter(func.upper(IGCFile.logger_id) == func.upper(logger_id)) \
.filter(Flight.model_id == None) \
.order_by(desc(Flight.id))
if self.logger_manufacturer_id.startswith('X'):
result = result.filter(Flight.pilot == self.owner)
result = result.first()
if result and result.model_id:
return result.model_id
if self.model is not None:
glider_type = self.model.lower()
# otherwise, try to guess the glider model by the glider type igc header
text_fragments = ['%{}%'.format(v) for v in re.sub(r'[^a-z]', ' ', glider_type).split()]
digit_fragments = ['%{}%'.format(v) for v in re.sub(r'[^0-9]', ' ', glider_type).split()]
if not text_fragments and not digit_fragments:
return None
glider_type_clean = re.sub(r'[^a-z0-9]', '', glider_type)
result = DBSession \
.query(AircraftModel) \
.filter(and_(
func.regexp_replace(func.lower(AircraftModel.name), '[^a-z]', ' ').like(func.any(text_fragments)),
func.regexp_replace(func.lower(AircraftModel.name), '[^0-9]', ' ').like(func.all(digit_fragments)))) \
.order_by(func.levenshtein(func.regexp_replace(func.lower(AircraftModel.name), '[^a-z0-9]', ''), glider_type_clean))
if result.first():
return result.first().id
# nothing found
return None
示例2: get_clustered_locations
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def get_clustered_locations(location_column,
threshold_radius=1000, filter=None):
'''
SELECT ST_Centroid(
(ST_Dump(
ST_Union(
ST_Buffer(
takeoff_location_wkt::geography, 1000
)::geometry
)
)
).geom) FROM flights WHERE pilot_id=31;
'''
# Cast the takeoff_location_wkt column to Geography
geography = cast(location_column, Geography)
# Add a metric buffer zone around the locations
buffer = cast(geography.ST_Buffer(threshold_radius), Geometry)
# Join the locations into one MultiPolygon
union = buffer.ST_Union()
# Split the MultiPolygon into separate polygons
dump = union.ST_Dump().geom
# Calculate center points of each polygon
locations = func.ST_Centroid(dump)
query = DBSession.query(locations.label('location'))
if filter is not None:
query = query.filter(filter)
return [Location.from_wkb(row.location) for row in query]
示例3: get_info
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def get_info(cls, location):
'''Returns a query object of mountain waves around the location'''
return DBSession.query(cls) \
.filter(func.ST_DWithin(
cast(WKTElement(location.to_wkt(), srid=4326), Geography),
cast(cls.location, Geography),
5000))
示例4: get_optimised_contest_trace
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def get_optimised_contest_trace(self, contest_type, trace_type):
from skylines.model.trace import Trace
query = DBSession.query(Trace) \
.filter(Trace.contest_type == contest_type) \
.filter(Trace.trace_type == trace_type) \
.filter(Trace.flight == self).first()
return query
示例5: mark_all_read
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def mark_all_read(cls, user, filter_func=None):
query = DBSession.query(cls) \
.filter(cls.recipient == user)
if filter_func is not None:
query = filter_func(query)
query.update(dict(time_read=datetime.utcnow()))
示例6: by_location
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def by_location(cls, location):
location = func.ST_MakePoint(location.longitude, location.latitude)
filter = functions.gcontains(cls.the_geom, location)
zone = DBSession.query(cls.tzid).filter(filter).scalar()
if zone is None:
return None
return timezone(unicode(zone))
示例7: by_location
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def by_location(cls, location, distance_threshold = 0.025):
airport = DBSession.query(cls, functions.distance(cls.location_wkt, location.to_wkt()).label('distance'))\
.order_by(functions.distance(cls.location_wkt, location.to_wkt())).first()
if airport is not None and (distance_threshold is None or
airport.distance < distance_threshold):
return airport.Airport
else:
return None
示例8: by_location
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def by_location(cls, location, distance_threshold=0.025):
location = WKTElement(location.to_wkt(), srid=4326)
distance = func.ST_Distance(cls.location_wkt, location)
airport = DBSession.query(cls, distance.label('distance')) \
.order_by(distance).first()
if airport is not None and (distance_threshold is None or
airport.distance < distance_threshold):
return airport.Airport
else:
return None
示例9: get_requested_record_list
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def get_requested_record_list(model, ids, **kw):
"""Similar to get_requested_record(), but expects a
comma-separated list of ids, and returns a list of (unique)
records."""
ids = _parse_id_list(ids)
q = DBSession.query(model).filter(model.id.in_(ids))
q = _patch_query(q, **kw)
records = {record.id: record for record in q}
if len(records) != len(ids):
raise HTTPNotFound(
detail=_('Sorry, {num_missing} of the requested records ({ids}) do not exist in our database.')
.format(num_missing=(len(ids) - len(records)), ids=ids))
return [records[id] for id in ids]
示例10: get_requested_record
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def get_requested_record(model, id, **kw):
"""Look up a record with the id (string) specified by a remote
client. Aborts the current request if the id is malformed or if
the record does not exist."""
try:
id = int(id)
except ValueError:
raise HTTPNotFound(detail=_('Sorry, the record id ({id}) that you '
'requested is not a valid id.').format(id=id))
q = DBSession.query(model)
q = _patch_query(q, **kw)
record = q.get(id)
if record is None:
raise HTTPNotFound(detail=_('Sorry, there is no such record ({id}) in '
'our database.').format(id=id))
return record
示例11: create_flight_notifications
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def create_flight_notifications(flight):
'''
Create notifications for the followers of the owner and pilots of the flight
'''
# Create list of flight-related users
senders = [flight.pilot_id, flight.co_pilot_id, flight.igc_file.owner_id]
senders = OrderedDict([(s, None) for s in senders if s is not None])
# Request followers/recipients of the flight-related users from the DB
followers = DBSession.query(Follower.source_id, Follower.destination_id) \
.filter(Follower.destination_id.in_(senders.keys())) \
.all()
# Determine the recipients and their most important sender
recipients = dict()
# For each flight-related user in decreasing importance ..
for sender in senders.keys():
# For each of his followers
for follower in followers:
if follower.destination_id != sender:
continue
# Don't send notifications to the senders if they follow each other
if follower.source_id in senders:
continue
# If the recipient/follower is not registered
# yet by a more important sender
if follower.source_id not in recipients:
# Register the recipient with the sender's id
recipients[follower.source_id] = sender
# Create notifications for the recipients
for recipient, sender in recipients.iteritems():
item = Notification(type=Notification.NT_FLIGHT,
sender_id=sender,
recipient_id=recipient,
flight=flight)
DBSession.add(item)
示例12: guess_registration
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def guess_registration(self):
from skylines.model.flight import Flight
# try to find another flight with the same logger and use it's aircraft registration
if self.logger_id is not None \
and self.logger_manufacturer_id is not None:
logger_id = self.logger_id
logger_manufacturer_id = self.logger_manufacturer_id
result = DBSession.query(Flight).outerjoin(IGCFile) \
.filter(func.upper(IGCFile.logger_manufacturer_id) == func.upper(logger_manufacturer_id)) \
.filter(func.upper(IGCFile.logger_id) == func.upper(logger_id)) \
.filter(Flight.registration != None) \
.order_by(desc(Flight.id))
if self.logger_manufacturer_id.startswith('X'):
result = result.filter(Flight.pilot == self.owner)
result = result.first()
if result and result.registration:
return result.registration
return None
示例13: get_largest
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def get_largest(cls):
'''Returns a query object ordered by distance'''
return DBSession.query(cls).order_by(desc(cls.olc_classic_distance))
示例14: by_md5
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def by_md5(cls, _md5):
return DBSession.query(cls).filter_by(md5=_md5).first()
示例15: test_query_obj
# 需要导入模块: from skylines.model.session import DBSession [as 别名]
# 或者: from skylines.model.session.DBSession import query [as 别名]
def test_query_obj(self):
"""Model objects can be queried"""
obj = DBSession.query(self.klass).one()
for key, value in self.attrs.iteritems():
assert_equals(getattr(obj, key), value)