def setUp(self): """ Set up organisation records """ # auth = current.auth s3db = current.s3db auth.override = True ptable = s3db.project_project atable = s3db.project_activity p1 = Row(name="Test Project 1", code="TP1") p1_id = ptable.insert(**p1) p1.update(id=p1_id) a1 = Row(name="Test Activity 1", project_id=p1_id) a1_id = atable.insert(**a1) a1.update(id=a1_id) # activity without a project a2 = Row(name="Test Activity 2") a2_id = atable.insert(**a2) a2.update(id=a2_id) self.p1 = p1 self.a1 = a1 self.a2 = a2
def get_base_location(self, _fields=None, _filter=None, as_rows=False): """ Get the base location of the instance(s) @param _fields: fields to retrieve from the location records (None for ALL) @param _filter: filter for the locations @param as_rows: return the result as Rows object @return: the base location(s) of the current instance """ db = current.db s3db = current.s3db ltable = s3db[LOCATION] rtable = self.rtable locations = [] for r in self.records: location = None query = None if LOCATION_ID in r: query = (ltable.id == r[LOCATION_ID]) if rtable: query = query & (rtable[LOCATION_ID] == ltable.id) if TRACK_ID in r: query = query & (rtable[TRACK_ID] == r[TRACK_ID]) elif TRACK_ID in r: q = (self.table[TRACK_ID] == r[TRACK_ID]) trackable = db(q).select(limitby=(0, 1)).first() table = s3db[trackable.instance_type] if LOCATION_ID in table.fields: query = ((table[TRACK_ID] == r[TRACK_ID]) & (table[LOCATION_ID] == ltable.id)) if query: if _filter is not None: query = query & _filter if not _fields: location = db(query).select(ltable.ALL, limitby=(0, 1)).first() else: location = db(query).select(limitby=(0, 1), *_fields).first() if location: locations.append(location) else: # Ensure we return an entry so that indexes match locations.append(Row({"lat": None, "lon": None})) if as_rows: return Rows(records=locations, compact=False) if not locations: return None elif len(locations) == 1: return locations[0] else: return locations
def get_location(self, timestmp=None, _fields=None, _filter=None, as_rows=False, exclude=None, empty=True): """ Get the current location of the instance(s) (at the given time) @param timestmp: last datetime for presence (defaults to current time) @param _fields: fields to retrieve from the location records (None for ALL) @param _filter: filter for the locations @param as_rows: return the result as Rows object @param exclude: interlocks to break at (avoids circular check-ins) @param empty: return None if no locations (set to False by gis.get_location_data()) @return: a location record, or a list of location records (if multiple) @ToDo: Also show Timestamp of when seen there """ if exclude is None: exclude = [] db = current.db s3db = current.s3db ptable = s3db[PRESENCE] ltable = s3db[LOCATION] if timestmp is None: timestmp = datetime.utcnow() locations = [] for r in self.records: location = None if TRACK_ID in r: query = ((ptable.deleted == False) & \ (ptable[TRACK_ID] == r[TRACK_ID]) & \ (ptable.timestmp <= timestmp)) presence = db(query).select(orderby=~ptable.timestmp, limitby=(0, 1)).first() if presence: if presence.interlock: exclude = [r[TRACK_ID]] + exclude tablename, record_id = presence.interlock.split(",", 1) trackable = S3Trackable(tablename=tablename, record_id=record_id) record = trackable.records.first() if TRACK_ID not in record or \ record[TRACK_ID] not in exclude: location = trackable.get_location( timestmp=timestmp, exclude=exclude, _fields=_fields, as_rows=True).first() elif presence.location_id: query = (ltable.id == presence.location_id) if _filter is not None: query = query & _filter if _fields is None: location = db(query).select(ltable.ALL, limitby=(0, 1)).first() else: location = db(query).select(limitby=(0, 1), *_fields).first() if not location: if len(self.records) > 1: trackable = S3Trackable(record=r, rtable=self.rtable) else: trackable = self location = trackable.get_base_location(_fields=_fields) if location: locations.append(location) elif not empty: # Ensure we return an entry for gis.get_location_data() so that indexes match locations.append(Row({"lat": None, "lon": None})) if as_rows: return Rows(records=locations, compact=False) if not locations: return None else: return locations