def get_all_gatherings(self, extras_originatingDevice=False): attachments = DeviceIndividual.get_attachments_of_individual(self.id) gatherings = [] for a in attachments: timerange = (a["attached"], a["removed"]) doc = document.find(deviceID=a["deviceID"])[0] relevant_gatherings = list(self._filter_gatherings_by_timerange( doc.gatherings, timerange)) for rg in relevant_gatherings: if extras_originatingDevice: rg.extras['originatingDevice'] = a['deviceID'] gatherings.append(rg) return gatherings
def testAttachDetachOne(self): self.assertEquals(0, len(DeviceIndividual.find())) DeviceIndividual.attach(self.D.id, self.I.id, "2015-09-29T14:00:00+03:00") self.assertEquals(1, len(DeviceIndividual.find())) self.assertEquals(self.I.id, DeviceIndividual.get_active_attachment_of_device(self.D.id)["individualID"]) self.assertEquals(self.D.id, DeviceIndividual.get_active_attachment_of_individual(self.I.id)["deviceID"]) self.assertEquals(None, DeviceIndividual.get_active_attachment_of_device(self.D.id)["removed"]) self.assertEquals(None, DeviceIndividual.get_active_attachment_of_individual(self.I.id)["removed"]) DeviceIndividual.detach(self.D.id, self.I.id, "2015-09-29T14:00:00+03:00") self.assertEquals("2015-09-29T14:00:00+03:00", DeviceIndividual.get_attachments_of_device(self.D.id)[0]["removed"]) self.assertEquals("2015-09-29T14:00:00+03:00", DeviceIndividual.get_attachments_of_individual(self.I.id)[0]["removed"]) self.assertEquals(1, len(DeviceIndividual.find())) self.assertEquals(None, DeviceIndividual.get_active_attachment_of_device(self.D.id)) self.assertEquals(None, DeviceIndividual.get_active_attachment_of_individual(self.I.id))
def set_gatherings(self, new_gatherings): # new_gatherings is parsed json (a list of dicts), not a list of Gathering objects totime = _timestamp_to_datetime atts = DeviceIndividual.get_attachments_of_individual(self.id) for a in atts: start = totime(a['attached']) end = totime(a['removed']) if ('removed' in a and a['removed']) else timezone.now() doc = document.find(deviceID=a['deviceID'])[0] # list of dicts relevant_new = [g for g in new_gatherings if (start <= totime(g['dateBegin']) <= end) and g['extras']['originatingDevice'] == a['deviceID']] # list of Gathering objects relevant_old = [g for g in doc.gatherings if (start <= totime(g.dateBegin) <= end)] # nothing to update? if (not relevant_new) and (not relevant_old): continue if len(relevant_new) != len(relevant_old): logger.warn('number of old gatherings different from number of new gatherings in choose/setIndividualGatherings. old: %d, new: %d' % (len(relevant_new), len(relevant_old))) # remove old gatherings for g in relevant_old: try: doc.gatherings.remove(g) except ValueError: pass # insert new gatherings doc.gatherings += [ gathering.from_lajistore_json(**g) for g in relevant_new] doc.update()