def test_iou(self): """Test IoU.""" person = Person([], Rectangle(Point(0, 0), Point(1, 2 / 3)), 1) track = Track( Person([], Rectangle(Point(0, 1 / 3), Point(1, 1)), 1), 1000000) computed_iou = self.bbox_tracker._iou(person, track) self.assertAlmostEqual(computed_iou, 1 / 3, 6)
def test_iou_no_intersection(self): """Test IoU with no intersection.""" person = Person([], Rectangle(Point(0, 0), Point(0.5, 0.5)), 1) track = Track( Person([], Rectangle(Point(0.5, 0.5), Point(1, 1)), 1), 1000000) computed_iou = self.bbox_tracker._iou(person, track) self.assertAlmostEqual(computed_iou, 0.0, 6)
def test_oks(self): """Test OKS.""" person = Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.8), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.1), KeyPoint(BodyPart(3), Point(0.8, 0.7), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1) track = Track( Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.8), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.9), KeyPoint(BodyPart(3), Point(0.8, 0.8), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1), 1000000) oks = self.kpt_tracker._object_keypoint_similarity(person, track) box_area = (0.8 - 0.2) * (0.8 - 0.2) x = 2 * self.tracker_config.keypoint_tracker_params.keypoint_falloff[3] d = 0.1 expected_oks = (1 + 1 + math.exp(-1 * (d**2) / (2 * box_area * (x**2)))) / 3 self.assertAlmostEqual(oks, expected_oks, 6)
def pick_random_char(): people_count = len(NOC) # choose random charcter person_number = randint(0, people_count - 1) actor = Person(NOC[person_number]) return actor
def click_restart(): global person person = Person() FramTopRefresh() FramPersonRefresh() FramRoomRefresh() FramMarketRefresh() mBox.showinfo('重新开始游戏', '重置成功!')
def test_oks_returns_zero(self): """Compute OKS returns 0.0 with less than 2 valid keypoints.""" person = Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.1), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.9), KeyPoint(BodyPart(3), Point(0.8, 0.8), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1) track = Track( Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.8), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.1), KeyPoint(BodyPart(3), Point(0.8, 0.8), 0.1) ], Rectangle(Point(0, 0), Point(0, 0)), 1), 1000000) oks = self.kpt_tracker._object_keypoint_similarity(person, track) self.assertAlmostEqual(oks, 0.0, 6)
def DeleteAllData(): entries = [] entries.extend(Person.query().fetch(keys_only=True)) entries.extend(Troop.query().fetch(keys_only=True)) entries.extend(ScoutGroup.query().fetch(keys_only=True)) entries.extend(Meeting.query().fetch(keys_only=True)) entries.extend(TroopPerson.query().fetch(keys_only=True)) entries.extend(Semester.query().fetch(keys_only=True)) entries.extend(TaskProgress.query().fetch(keys_only=True)) entries.extend(UserPrefs.query().fetch(keys_only=True)) ndb.delete_multi(entries) ndb.get_context().clear_cache() # clear memcache
def test_area(self): """Test area.""" track = Track( Person([ KeyPoint(BodyPart(0), Point(0.1, 0.2), 1), KeyPoint(BodyPart(1), Point(0.3, 0.4), 0.9), KeyPoint(BodyPart(2), Point(0.4, 0.6), 0.9), KeyPoint(BodyPart(3), Point(0.7, 0.8), 0.1) ], Rectangle(Point(0, 0), Point(0, 0)), 1), 1000000) area = self.kpt_tracker._area(track) expected_area = (0.4 - 0.1) * (0.6 - 0.2) self.assertAlmostEqual(area, expected_area, 6)
def extract_data(self): # Ru stuff try: ru_root = xml.etree.ElementTree.parse('ru.xml').getroot() str_http_adress = ru_root.find('adress').text for ru in ru_root.findall('ru'): ru_name = ru.find('name').text ru_id = int(ru.find('id').text) self.ruList.append( RU(ru_name, str_http_adress.format(ru_id))) except Exception as e: print e raise ParseXmlException('error while parsing ru.xml') # User stuff try: user_root = xml.etree.ElementTree.parse('users.xml').getroot() str_http_adress = ru_root.find('adress').text for user in user_root.findall('user'): user_name = user.find('name').text user_id = user.find('cardId').text user_mail = user.find('mail').text user_class = Person(user_name) user_class.cardId = user_id user_class.mail = user_mail self.usersList.append( user_class ) except Exception as e: print e raise ParseXmlException('error while parsing users.xml')
def extract_data(self): # Ru stuff try: ru_root = xml.etree.ElementTree.parse('ru.xml').getroot() str_http_adress = ru_root.find('adress').text for ru in ru_root.findall('ru'): ru_name = ru.find('name').text ru_id = int(ru.find('id').text) self.ruList.append(RU(ru_name, str_http_adress.format(ru_id))) except Exception as e: print e raise ParseXmlException('error while parsing ru.xml') # User stuff try: user_root = xml.etree.ElementTree.parse('users.xml').getroot() str_http_adress = ru_root.find('adress').text for user in user_root.findall('user'): user_name = user.find('name').text user_id = user.find('cardId').text user_mail = user.find('mail').text user_class = Person(user_name) user_class.cardId = user_id user_class.mail = user_mail self.usersList.append(user_class) except Exception as e: print e raise ParseXmlException('error while parsing users.xml')
def _create_track(self, person: Person, timestamp: int, track_id: int = None) -> Track: """Creates a track from information in a pose. Args: person: A `Person`. timestamp: The current timestamp in microseconds. track_id: The id to assign to the new track. If not provided, will assign the next available id. Returns: A `Track`. """ person = Person( person.keypoints, person.bounding_box, person.score, track_id if track_id else self._update_and_get_next_track_id()) track = Track(person, timestamp) return track
def show(sgroup_url=None, person_url=None, action=None): user = UserPrefs.current() if not user.hasAccess(): return "denied", 403 breadcrumbs = [{'link':'/', 'text':'Hem'}] section_title = u'Personer' breadcrumbs.append({'link': '/persons', 'text': section_title}) baselink = '/persons/' sgroup_key = None # type: ndb.Key scoutgroup = None # type: ScoutGroup if sgroup_url is not None: sgroup_key = ndb.Key(urlsafe=sgroup_url) scoutgroup = sgroup_key.get() baselink += sgroup_url+"/" breadcrumbs.append({'link': baselink, 'text': scoutgroup.getname()}) if scoutgroup is None: return render_template( 'index.html', heading=section_title, baselink=baselink, items=ScoutGroup.getgroupsforuser(user), breadcrumbs=breadcrumbs, username=user.getname()) person_key = None # type: ndb.Key person = None # type: Person if person_url is not None: person_key = ndb.Key(urlsafe=person_url) person = person_key.get() baselink += person_url+"/" section_title = person.getname() breadcrumbs.append({'link': baselink, 'text': section_title}) if person is None: if not user.hasGroupKeyAccess(sgroup_key): return "denied", 403 section_title = 'Personer' return render_template( 'persons.html', heading=section_title, baselink=baselink, # TODO: memcache persons=Person.query(Person.scoutgroup == sgroup_key).order(Person.firstname, Person.lastname).fetch(), breadcrumbs=breadcrumbs, username=user.getname()) if person.scoutgroup != sgroup_key: return "denied", 403 if not user.hasPersonAccess(person): return "denied", 403 if action is not None: if action == "deleteperson" or action == "addbackperson": person.removed = action == "deleteperson" person.put() # we only mark the person as removed if person.removed: tps = TroopPerson.query(TroopPerson.person == person.key).fetch() for tp in tps: tp.delete() return redirect(breadcrumbs[-1]['link']) elif action == "removefromtroop" or action == "setasleader" or action == "removeasleader": troop_key = ndb.Key(urlsafe=request.args["troop"]) tps = TroopPerson.query(TroopPerson.person == person.key, TroopPerson.troop == troop_key).fetch(1) if len(tps) == 1: tp = tps[0] if action == "removefromtroop": tp.delete() else: tp.leader = (action == "setasleader") tp.put() elif action == "addtowaitinglist": scoutgroup = person.scoutgroup.get() troop = None tps = TroopPerson.query(TroopPerson.person == person.key).fetch(1) if len(tps) == 1: troop = tps[0].troop.get() scoutgroup = person.scoutgroup.get() if scoutgroup.canAddToWaitinglist(): try: if scoutnet.AddPersonToWaitinglist( scoutgroup, person.firstname, person.lastname, person.personnr, person.email, person.street, person.zip_code, person.zip_name, person.phone, person.mobile, troop, "", "", "", "", "", "", "", "" ): person.notInScoutnet = False person.put() except scoutnet.ScoutnetException as e: return render_template('error.html', error=str(e)) else: logging.error('unknown action=' + action) abort(404) return "" trooppersons = TroopPerson.query(TroopPerson.person == person.key).fetch() # TODO: memcache # render main pages return render_template( 'person.html', heading=section_title, baselink='/persons/' + scoutgroup.key.urlsafe() + '/', trooppersons=sorted(trooppersons, semester_sort), ep=person, scoutgroup=scoutgroup, breadcrumbs=breadcrumbs)
def _postprocess(self, keypoints_with_scores: np.ndarray, image_height: int, image_width: int, detection_threshold: float) -> List[Person]: """Returns a list "Person" corresponding to the input image. Note that coordinates are expressed in (x, y) format for drawing utilities. Args: keypoints_with_scores: Output of the MultiPose TFLite model. image_height: height of the image in pixels. image_width: width of the image in pixels. detection_threshold: minimum confidence score for an entity to be considered. Returns: A list of Person(keypoints, bounding_box, scores), each containing: * the coordinates of all keypoints of the detected entity; * the bounding boxes of the entity. * the confidence core of the entity. """ _, num_instances, _ = keypoints_with_scores.shape list_persons = [] for idx in range(num_instances): # Skip a detected pose if its confidence score is below the threshold person_score = keypoints_with_scores[0, idx, 55] if person_score < detection_threshold: continue # Extract the keypoint coordinates and scores kpts_y = keypoints_with_scores[0, idx, range(0, 51, 3)] kpts_x = keypoints_with_scores[0, idx, range(1, 51, 3)] scores = keypoints_with_scores[0, idx, range(2, 51, 3)] # Create the list of keypoints keypoints = [] for i in range(scores.shape[0]): keypoints.append( KeyPoint( BodyPart(i), Point(int(kpts_x[i] * image_width), int(kpts_y[i] * image_height)), scores[i])) # Calculate the bounding box rect = [ keypoints_with_scores[0, idx, 51], keypoints_with_scores[0, idx, 52], keypoints_with_scores[0, idx, 53], keypoints_with_scores[0, idx, 54] ] bounding_box = Rectangle( Point(int(rect[1] * image_width), int(rect[0] * image_height)), Point(int(rect[3] * image_width), int(rect[2] * image_height))) # Create a Person instance corresponding to the detected entity. list_persons.append(Person(keypoints, bounding_box, person_score)) if self._tracker: list_persons = self._tracker.apply(list_persons, time.time() * 1000) return list_persons
def merge_sg(oldname, newname, commit): oldsg = ScoutGroup.getbyname(oldname) if oldsg is None: raise RuntimeError("Old sg name:%s not found" % oldname) newsg = ScoutGroup.getbyname(newname) if newsg is None: raise RuntimeError("New sg name:%s not found" % newname) if not commit: logging.info("*** testmode ***") keys_to_delete = [] entities_to_put_first = [] entities_to_put = [] semester = Semester.getOrCreateCurrent() keys_to_delete.append(oldsg.key) logging.info("Update all users to the new scoutgroup") for u in UserPrefs.query(UserPrefs.groupaccess == oldsg.key).fetch(): u.groupaccess = newsg.key u.activeSemester = semester.key entities_to_put.append(u) logging.info("Moving all persons to the new ScoutGroup:%s", newsg.getname()) for oldp in Person.query(Person.scoutgroup == oldsg.key).fetch(): logging.info(" * Moving %s %s", oldp.getname(), oldp.personnr) oldp.scoutgroup = newsg.key entities_to_put.append(oldp) logging.info("Move all troops to the new ScoutGroup:%s", newsg.getname()) for oldt in Troop.query(Troop.scoutgroup == oldsg.key).fetch(): logging.info(" * found old troop for %s, semester=%s", str(oldt.key.id()), oldt.semester_key.get().getname()) keys_to_delete.append(oldt.key) newt = Troop.get_by_id(Troop.getid(oldt.scoutnetID, newsg.key, oldt.semester_key), use_memcache=True) if newt is None: logging.info(" * * creating new troop for %s, semester=%s", str(oldt.key.id()), oldt.semester_key.get().getname()) newt = Troop.create(oldt.name, oldt.scoutnetID, newsg.key, oldt.semester_key) entities_to_put_first.append(newt) # put first to be able to reference it else: logging.info(" * * already has new troop for %s, semester=%s", str(newt.key.id()), newt.semester_key.get().getname()) logging.info(" * Move all trooppersons to the new group (it they don't already exist there)") for oldtp in TroopPerson.query(TroopPerson.troop == oldt.key).fetch(): keys_to_delete.append(oldtp.key) newtp = TroopPerson.get_by_id(TroopPerson.getid(newt.key, oldtp.person), use_memcache=True) if newtp is None: logging.info(" * * creating new TroopPerson for %s:%s", newt.getname(), oldtp.getname()) newtp = TroopPerson.create(newt.key, oldtp.person, oldtp.leader) entities_to_put.append(newtp) else: logging.info(" * * already has TroopPerson for %s:%s", newt.getname(), oldtp.getname()) logging.info(" * Move all old meetings to the new troop") for oldm in Meeting.query(Meeting.troop==oldt.key).fetch(): keys_to_delete.append(oldm.key) newm = Meeting.get_by_id(Meeting.getId(oldm.datetime, newt.key), use_memcache=True) if newm is None: logging.info(" * * creating new Meeting for %s:%s", newt.getname(), oldm.datetime.strftime("%Y-%m-%d %H:%M")) newm = Meeting.getOrCreate(newt.key, oldm.name, oldm.datetime, oldm.duration, oldm.ishike) newm.attendingPersons = oldm.attendingPersons entities_to_put.append(newm) else: logging.info(" * * already has Meeting for %s:%s", newt.getname(), newt.datetime.strftime("%Y-%m-%d %H:%M")) logging.info("Putting %d entities first", len(entities_to_put_first)) if commit: ndb.put_multi(entities_to_put_first) logging.info("Putting %d entities", len(entities_to_put)) if commit: ndb.put_multi(entities_to_put) logging.info("Deleting %d keys", len(keys_to_delete)) if commit: ndb.delete_multi(keys_to_delete) logging.info("clear memcache") if commit: ndb.get_context().clear_cache() logging.info("Done!")
def scoutgroupsummary(sgroup_url): user = UserPrefs.current() if not user.canImport(): return "denied", 403 if sgroup_url is None: return "missing group", 404 if user.activeSemester is None: semester = Semester.getOrCreateCurrent() else: semester = user.activeSemester.get() sgroup_key = ndb.Key(urlsafe=sgroup_url) scoutgroup = sgroup_key.get() breadcrumbs = [{'link':'/', 'text':'Hem'}] baselink = "/groupsummary/" + sgroup_url section_title = "Föreningsredovisning - " + scoutgroup.getname() breadcrumbs.append({'link':baselink, 'text':section_title}) class Item(): age = 0 women = 0 womenMeetings = 0 men = 0 menMeetings = 0 def __init__(self, age, women=0, womenMeetings=0, men=0, menMeetings=0): self.age = age self.women = women self.womenMeetings = womenMeetings self.men = men self.menMeetings = menMeetings year = semester.year women = 0 womenMeetings = 0 men = 0 menMeetings = 0 startage = 7 endage = 25 ages = [Item('0 - 6')] ages.extend([Item(i) for i in range(startage, endage+1)]) ages.append(Item('26 - 64')) ages.append(Item('65 -')) leaders = [Item(u't.o.m. 25 år'), Item(u'över 25 år')] boardmebers = [Item('')] from_date_time = datetime.datetime.strptime(str(semester.year) + "-01-01 00:00", "%Y-%m-%d %H:%M") to_date_time = datetime.datetime.strptime(str(semester.year) + "-12-31 00:00", "%Y-%m-%d %H:%M") emails = [] for person in Person.query(Person.scoutgroup==sgroup_key).fetch(): if person.member_years is None or semester.year not in person.member_years: continue if person.email is not None and len(person.email) != 0 and person.email not in emails: emails.append(person.email) age = person.getyearsoldthisyear(year) if scoutgroup.attendance_incl_hike: number_of_meetings = Meeting.query(Meeting.attendingPersons==person.key, Meeting.datetime >= from_date_time, Meeting.datetime <= to_date_time).count() else: meetings = Meeting.query(Meeting.attendingPersons==person.key, Meeting.datetime >= from_date_time, Meeting.datetime <= to_date_time) nr_all = meetings.count() nr_hike_meetings = meetings.filter(Meeting.ishike == True).count() number_of_meetings = nr_all - nr_hike_meetings index = 0 if 7 <= age <= 25: index = age-startage + 1 elif age < 7: index = 0 elif 26 <= age <= 64: index = endage - startage + 2 else: index = endage - startage + 3 if person.isFemale(): women += 1 ages[index].women += 1 else: men += 1 ages[index].men += 1 if number_of_meetings >= scoutgroup.attendance_min_year: if person.isFemale(): womenMeetings += 1 ages[index].womenMeetings += 1 else: menMeetings += 1 ages[index].menMeetings += 1 if person.isBoardMember(): if person.isFemale(): boardmebers[0].women += 1 else: boardmebers[0].men += 1 if person.isLeader() and number_of_meetings > 1: index = 0 if age <= 25 else 1 if person.isFemale(): leaders[index].women += 1 else: leaders[index].men += 1 ages.append(Item("Totalt", women, womenMeetings, men, menMeetings)) return render_template('groupsummary.html', ages=ages, boardmebers=boardmebers, leaders=leaders, breadcrumbs=breadcrumbs, emails=emails, year=semester.year, min_nr_meetings=str(scoutgroup.attendance_min_year), incl_hikes=scoutgroup.attendance_incl_hike)
def test_keypoint_tracker(self): """Test Keypoint tracker.""" # Timestamp: 0. Person becomes the only track persons = [ Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.8), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.9), KeyPoint(BodyPart(3), Point(0.8, 0.8), 0.0) ], Rectangle(Point(0, 0), Point(0, 0)), 1) ] persons = self.kpt_tracker.apply(persons, 0) tracks = self.kpt_tracker._tracks self.assertEqual(len(persons), 1) self.assertEqual(persons[0].id, 1) self.assertEqual(len(tracks), 1) self.assertEqual(tracks[0].person.id, 1) self.assertEqual(tracks[0].last_timestamp, 0) # Timestamp: 100000. First person is linked with track 1. Second person # spawns a new track (id = 2). persons = [ Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.8), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.9), KeyPoint(BodyPart(3), Point(0.8, 0.8), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1), Person( [ KeyPoint(BodyPart(0), Point(0.8, 0.8), 0.8), KeyPoint(BodyPart(1), Point(0.6, 0.6), 0.3), KeyPoint(BodyPart(2), Point(0.4, 0.4), 0.1), # Low confidence. KeyPoint(BodyPart(3), Point(0.2, 0.2), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1) ] persons = self.kpt_tracker.apply(persons, 100000) tracks = self.kpt_tracker._tracks self.assertEqual(len(persons), 2) self.assertEqual(persons[0].id, 1) self.assertEqual(persons[1].id, 2) self.assertEqual(len(tracks), 2) self.assertEqual(tracks[0].person.id, 1) self.assertEqual(tracks[0].last_timestamp, 100000) self.assertEqual(tracks[1].person.id, 2) self.assertEqual(tracks[1].last_timestamp, 100000) # Timestamp: 900000. First person is linked with track 2. Second person # spawns a new track (id = 3). persons = [ # Links with id = 2. Person( [ KeyPoint(BodyPart(0), Point(0.6, 0.7), 0.7), KeyPoint(BodyPart(1), Point(0.5, 0.6), 0.7), KeyPoint(BodyPart(2), Point(0.0, 0.0), 0.1), # Low confidence. KeyPoint(BodyPart(3), Point(0.2, 0.1), 1.0) ], Rectangle(Point(0, 0), Point(0, 0)), 1), # Becomes id = 3. Person( [ KeyPoint(BodyPart(0), Point(0.5, 0.1), 0.6), KeyPoint(BodyPart(1), Point(0.9, 0.3), 0.6), KeyPoint(BodyPart(2), Point(0.1, 0.1), 0.9), KeyPoint(BodyPart(3), Point(0.4, 0.4), 0.1) ], # Low confidence. Rectangle(Point(0, 0), Point(0, 0)), 1) ] persons = self.kpt_tracker.apply(persons, 900000) tracks = self.kpt_tracker._tracks self.assertEqual(len(persons), 2) self.assertEqual(persons[0].id, 2) self.assertEqual(persons[1].id, 3) self.assertEqual(len(tracks), 3) self.assertEqual(tracks[0].person.id, 2) self.assertEqual(tracks[0].last_timestamp, 900000) self.assertEqual(tracks[1].person.id, 3) self.assertEqual(tracks[1].last_timestamp, 900000) self.assertEqual(tracks[2].person.id, 1) self.assertEqual(tracks[2].last_timestamp, 100000) # Timestamp: 1200000. First person spawns a new track (id = 4), even though # it has the same keypoints as track 1. This is because the age exceeds # 1000 msec. The second person links with id 2. The third person spawns a # new track (id = 5). persons = [ # Becomes id = 4. Person([ KeyPoint(BodyPart(0), Point(0.2, 0.2), 1.0), KeyPoint(BodyPart(1), Point(0.4, 0.4), 0.8), KeyPoint(BodyPart(2), Point(0.6, 0.6), 0.9), KeyPoint(BodyPart(3), Point(0.8, 0.8), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1), # Links with id = 2. Person( [ KeyPoint(BodyPart(0), Point(0.55, 0.7), 0.7), KeyPoint(BodyPart(1), Point(0.5, 0.6), 0.9), KeyPoint(BodyPart(2), Point(1.0, 1.0), 0.1), # Low confidence. KeyPoint(BodyPart(3), Point(0.8, 0.1), 0.0) ], # Low confidence. Rectangle(Point(0, 0), Point(0, 0)), 1), # Becomes id = 5. Person( [ KeyPoint(BodyPart(0), Point(0.1, 0.1), 0.1), # Low confidence. KeyPoint(BodyPart(1), Point(0.2, 0.2), 0.9), KeyPoint(BodyPart(2), Point(0.3, 0.3), 0.7), KeyPoint(BodyPart(3), Point(0.4, 0.4), 0.8) ], Rectangle(Point(0, 0), Point(0, 0)), 1) ] persons = self.kpt_tracker.apply(persons, 1200000) tracks = self.kpt_tracker._tracks self.assertEqual(len(persons), 3) self.assertEqual(persons[0].id, 4) self.assertEqual(persons[1].id, 2) self.assertEqual(len(tracks), 4) self.assertEqual(tracks[0].person.id, 2) self.assertEqual(tracks[0].last_timestamp, 1200000) self.assertEqual(tracks[1].person.id, 4) self.assertEqual(tracks[1].last_timestamp, 1200000) self.assertEqual(tracks[2].person.id, 5) self.assertEqual(tracks[2].last_timestamp, 1200000) self.assertEqual(tracks[3].person.id, 3) self.assertEqual(tracks[3].last_timestamp, 900000) # Timestamp: 1300000. First person spawns a new track (id = 6). Since # max_tracks is 4, the oldest track (id = 3) is removed. persons = [ # Becomes id = 6. Person([ KeyPoint(BodyPart(0), Point(0.1, 0.8), 1.0), KeyPoint(BodyPart(1), Point(0.2, 0.9), 0.6), KeyPoint(BodyPart(2), Point(0.2, 0.9), 0.5), KeyPoint(BodyPart(3), Point(0.8, 0.2), 0.4) ], Rectangle(Point(0, 0), Point(0, 0)), 1) ] persons = self.kpt_tracker.apply(persons, 1300000) tracks = self.kpt_tracker._tracks self.assertEqual(len(persons), 1) self.assertEqual(persons[0].id, 6) self.assertEqual(len(tracks), 4) self.assertEqual(tracks[0].person.id, 6) self.assertEqual(tracks[0].last_timestamp, 1300000) self.assertEqual(tracks[1].person.id, 2) self.assertEqual(tracks[1].last_timestamp, 1200000) self.assertEqual(tracks[2].person.id, 4) self.assertEqual(tracks[2].last_timestamp, 1200000) self.assertEqual(tracks[3].person.id, 5) self.assertEqual(tracks[3].last_timestamp, 1200000)
def test_iou_full_overlap(self): """Test IoU full overlap.""" person = Person([], Rectangle(Point(0, 0), Point(1, 1)), 1) track = Track(Person([], Rectangle(Point(0, 0), Point(1, 1)), 1), 1000000) computed_iou = self.bbox_tracker._iou(person, track) self.assertAlmostEqual(computed_iou, 1.0, 6)
def DoImport(self, data, semester): """ :param data: from scoutnet :type data: str :type semester: data.Semester :rtype bool """ if not self.commit: self.result.append("*** sparar inte, test mode ***") if data == None or len(data) < 80: self.result.error(u"ingen data från scoutnet") return False list = scoutnet.GetScoutnetDataListJson(data) self.result.append("antal personer=%d" % len(list)) if len(list) < 1: self.result.error(u"för få rader: %d st" % len(list)) return False personsToSave = [] troopPersonsToSave = [] activePersonIds = set() # person.ids that was seen in this import leadersToAddToTroops = [] # tuple: (troop_id, person) allTroops = {} # dict: int(troop_id): troop for p in list: person_id = int(p["id"]) # type: int personnr = p["personnr"].replace('-', '') person = Person.getByMemberNo(person_id) if len(personnr) < 12: self.result.warning( u"%d %s %s har inte korrekt personnummer: '%s', hoppar över personen" % (person_id, p["firstname"], p["lastname"], personnr)) continue if person == None: person = Person.getByPersonNr( personnr ) # fallback lookup, these records will be converted to member_no. if person != None: person_id = person.key.id() person.firstname = p["firstname"] person.lastname = p["lastname"] person.setpersonnr(personnr) if person.notInScoutnet != None: person.notInScoutnet = False else: person = Person.create(person_id, p["firstname"], p["lastname"], personnr) self.result.append( "Ny person:%s %s %s" % (str(person_id), p["firstname"], p["lastname"])) activePersonIds.add(person.key.id()) person.removed = False person.patrool = p["patrool"] person.email = p["email"] person.member_no = p["member_no"] person.phone = p["phone"] person.mobile = p["mobile"] person.alt_email = p["contact_alt_email"] person.mum_name = p["mum_name"] person.mum_email = p["mum_email"] person.mum_mobile = p["mum_mobile"] person.dad_name = p["dad_name"] person.dad_email = p["dad_email"] person.dad_mobile = p["dad_mobile"] person.street = p["street"] person.zip_code = p["zip_code"] person.zip_name = p["zip_name"] person.troop_roles = p["troop_roles"] person.group_roles = p["group_roles"] if semester.year not in person.member_years: person.member_years.append(semester.year) person._dirty = True scoutgroup = self.GetOrCreateGroup(p["group"], p["group_id"]) person.scoutgroup = scoutgroup.key if len(p["troop"]) == 0: self.result.warning( u"Ingen avdelning vald för %s %s %s" % (str(person.member_no), p["firstname"], p["lastname"])) troop = self.GetOrCreateTroop(p["troop"], p["troop_id"], scoutgroup.key, semester.key) troop_key = troop.key if troop is not None else None if troop is not None: allTroops[troop.scoutnetID] = troop if person._dirty: self.result.append( u"Sparar ändringar:%s %s %s" % (str(person.member_no), p["firstname"], p["lastname"])) if self.commit: personsToSave.append(person) if troop_key != None: if self.commit: tp = TroopPerson.create_if_missing(troop_key, person.key, False) if tp: troopPersonsToSave.append(tp) self.result.append( u"Ny avdelning '%s' för:%s %s" % (p["troop"], p["firstname"], p["lastname"])) # collect leaders that are assigned to troops in scoutnet roles = p['roles'] if "value" in roles: values = roles["value"] if len(values) > 0: for name, value in values.items(): if name == "troop": for troop_id, troop_roles in value.items(): for troop_role_id in troop_roles: if int( troop_role_id ) in scoutnet.Roles.Troop_leader_role_set: leadersToAddToTroops.append( (int(troop_id), person)) break # pass 2, add leaders to troops according to roles for troop_leader in leadersToAddToTroops: troop_id = troop_leader[0] person = troop_leader[1] troop = allTroops.get(troop_id) if troop is None: troop = Troop.getById(troop_id, semester.key) if troop is not None: tp = None # search among the TroopPersons we are about to add to the database for tpcandidate in troopPersonsToSave: if tpcandidate.troop == troop.key and tpcandidate.person == person.key: tpcandidate.leader = True tp = tpcandidate break if tp is None: # try finding the TroopPerson in the database and set to leader tp = TroopPerson.create_or_set_as_leader( troop.key, person.key) if tp: # new or changed leader TroopPerson needs to be saved troopPersonsToSave.append(tp) self.result.append(u"Avdelning '%s' ny ledare:%s" % (troop.getname(), person.getname())) # check if old persons are still members, mark persons not imported in this import session as removed if len( personsToSave ) > 0: # protect agains a failed import, with no persons marking everyone as removed previousPersons = Person.query(Person.scoutgroup == scoutgroup.key, Person.removed != True) for previousPersonKey in previousPersons.iter(keys_only=True): if previousPersonKey.id() not in activePersonIds: personToMarkAsRemoved = previousPersonKey.get() personToMarkAsRemoved.removed = True self.result.append( u"%s finns inte i scoutnet, markeras som borttagen. id:%s" % (personToMarkAsRemoved.getname(), str(previousPersonKey.id()))) if personToMarkAsRemoved in personsToSave: raise Exception( 'A removed person cannot be in that list') personsToSave.append(personToMarkAsRemoved) if self.commit: ndb.put_multi(personsToSave) ndb.put_multi(troopPersonsToSave) return True
#####数据##### DEBUG = False #全局变量 PROCESS_NAME = "buyhouse" ICON_NAME = "ico.ico" VERSION = 1.2 CHECK_URL = "https://github.com/lingliqianxun/game-buyhouse/blob/master/version.txt?raw=true" DOWN_URL = "https://github.com/lingliqianxun/game-buyhouse/blob/master/dist/buyhouse.exe?raw=true" #个人存档数据 person = Person() person.GetData() #####事件##### #成就展示 def ArchiveShow(): archive_get = [] archive_less = [] for a in person.archives: if a['code'] in person.archive_person: continue else: archive_less.append(a)
def GameOver(event): global person person = Person() person.SaveData() win.destroy()
def test_bounding_box_tracker(self): """Test BoundingBoxTracker.""" # Timestamp: 0. Person becomes the first two tracks persons = [ Person([], Rectangle(Point(0, 0), Point(0.5, 0.5)), 1), Person( [], Rectangle(Point(0, 0), Point(1, 1)), 1, ) ] persons = self.bbox_tracker.apply(persons, 0) tracks = self.bbox_tracker._tracks self.assertEqual(len(persons), 2) self.assertEqual(persons[0].id, 1) self.assertEqual(persons[1].id, 2) self.assertEqual(len(tracks), 2) self.assertEqual(tracks[0].person.id, 1) self.assertEqual(tracks[0].last_timestamp, 0) self.assertEqual(tracks[1].person.id, 2) self.assertEqual(tracks[1].last_timestamp, 0) # Timestamp: 100000. First person is linked with track 1. Second person # spawns a new track (id = 2). persons = [ Person([], Rectangle(Point(0.1, 0.1), Point(0.5, 0.5)), 1), Person([], Rectangle(Point(0.3, 0.2), Point(0.9, 0.9)), 1) ] persons = self.bbox_tracker.apply(persons, 100000) tracks = self.bbox_tracker._tracks self.assertEqual(len(persons), 2) self.assertEqual(persons[0].id, 1) self.assertEqual(persons[1].id, 3) self.assertEqual(len(tracks), 3) self.assertEqual(tracks[0].person.id, 1) self.assertEqual(tracks[0].last_timestamp, 100000) self.assertEqual(tracks[1].person.id, 3) self.assertEqual(tracks[1].last_timestamp, 100000) self.assertEqual(tracks[2].person.id, 2) self.assertEqual(tracks[2].last_timestamp, 0) # Timestamp: 1050000. First person is linked with track 1. Second person is # identical to track 2, but is not linked because track 2 is deleted due to # age. Instead it spawns track 4. persons = [ Person([], Rectangle(Point(0.1, 0.1), Point(0.5, 0.55)), 1), Person([], Rectangle(Point(0, 0), Point(1, 1)), 1) ] persons = self.bbox_tracker.apply(persons, 1050000) tracks = self.bbox_tracker._tracks self.assertEqual(len(persons), 2) self.assertEqual(persons[0].id, 1) self.assertEqual(persons[1].id, 4) self.assertEqual(len(tracks), 3) self.assertEqual(tracks[0].person.id, 1) self.assertEqual(tracks[0].last_timestamp, 1050000) self.assertEqual(tracks[1].person.id, 4) self.assertEqual(tracks[1].last_timestamp, 1050000) self.assertEqual(tracks[2].person.id, 3) self.assertEqual(tracks[2].last_timestamp, 100000)
def update_person_ids(commit, sgroup_key, start_cursor, stage, taskProgress): start_time = time.time() time_is_out = False there_is_more = True oldToNewDict = {} # --- Stage 0 --- if stage == 0: while there_is_more: if time.time() - start_time > max_time_seconds: time_is_out = True break # time out person_keys, start_cursor, there_is_more = Person.query(Person.scoutgroup ==sgroup_key) \ .fetch_page(page_size=PAGE_SIZE, start_cursor=start_cursor, keys_only=True) for person_key in person_keys: if len(str(person_key.id())) < 12: # cannot be a personnr if shorter than 12 chars taskProgress.info("Not updating id=%s" % (str(person_key.id()))) continue person = person_key.get() if person.version == 1 or person.version == 2: taskProgress.info("Already updated id=%s" % (str(person_key.id()))) continue if person.member_no is None: taskProgress.warning("Cannot update, member_no is None id=%s, %s" % (str(person_key.id()), person.getname())) continue taskProgress.info("Updating Person: id=%s, pnr=%s, no=%s, %s" % (str(person.key.id()), person.personnr, str(person.member_no), person.getname())) newPerson, pendingKeyChange = person.updateKey() if newPerson is not None: if commit and newPerson._dirty: k = newPerson.put() assert(k == newPerson.key) if commit and person._dirty: person.put() if pendingKeyChange is not None: if pendingKeyChange._dirty: pendingKeyChange.put() # safe to put, to be able to test the rest of the method if there_is_more: return start_cursor, there_is_more, stage else: taskProgress.info("Updated all persons Persons") stage = 1 start_cursor = None there_is_more = True return start_cursor, there_is_more, stage # restart to start fresh and let datastore catch up taskProgress.info("Loading PendingPersonKeyChange records") ppkc_more = True ppkc_start_cursor = None while ppkc_more: ppkcs, ppkc_start_cursor, ppkc_more = PendingPersonKeyChange.query().fetch_page(page_size=1000, start_cursor=ppkc_start_cursor) for ppkc in ppkcs: oldToNewDict[ppkc.old_key] = ppkc.new_key taskProgress.info("Loaded persons to convert %d" % (len(oldToNewDict))) taskProgress.info("Number of persons to convert %d" % (len(oldToNewDict))) # --- Stage 1 --- if stage == 1: num_meetings_updated = 0 troop_count = 0 there_is_more = True taskProgress.info("Updating Meetings") while there_is_more: if time.time() - start_time > max_time_seconds: return start_cursor, there_is_more, stage troop_keys, start_cursor, there_is_more = Troop.query(Troop.scoutgroup == sgroup_key) \ .fetch_page(page_size=5, start_cursor=start_cursor, keys_only=True) for troop_key in troop_keys: troop_count += 1 meetings = Meeting.query(Meeting.troop == troop_key).fetch() for meeting in meetings: if meeting.uppdateOldPersonKeys(oldToNewDict): num_meetings_updated += 1 if commit: meeting.put() if num_meetings_updated % 10 == 0: taskProgress.info("Updated %d meetings, %d troop_count" % (num_meetings_updated, troop_count)) if there_is_more: return start_cursor, there_is_more, stage else: taskProgress.info("Updated all meetings, time taken: %s s" % (str(time.time() - start_time))) stage = 2 there_is_more = True start_cursor = None return start_cursor, there_is_more, stage # --- Stage 2 --- if stage == 2: update_counter = 0 if start_cursor == None: start_cursor = 0 taskProgress.info("Updating TroopPersons") for index, (old_person_key, new_person_key) in enumerate(oldToNewDict.items()): if index < start_cursor: continue for troopPerson in TroopPerson.query(TroopPerson.person==old_person_key).fetch(): troopPerson.person = new_person_key update_counter += 1 if commit: troopPerson.put() if update_counter % 10 == 0: taskProgress.info("Updated %d TroopPersons" % (update_counter)) if time.time() - start_time > max_time_seconds: start_cursor = index there_is_more = True return start_cursor, there_is_more, stage taskProgress.info("Updated all TroopPersons") there_is_more = True stage = 3 start_cursor = None return start_cursor, there_is_more, stage # restart to start fresh and let datastore catch up # --- Stage 3 --- if stage == 3: taskProgress.info("Deleting old persons") if commit and len(oldToNewDict) != 0: old_person_keys = [old_person_key for index, (old_person_key, new_person_key) in enumerate(oldToNewDict.items())] ndb.delete_multi(old_person_keys) taskProgress.info("Deleted %d old persons" % (len(old_person_keys))) there_is_more = False start_cursor = None stage = 4 taskProgress.info("Done updating Person keys!") return None, False, stage
def merge_scout_group(oldname, newname, commit, taskProgress, user, move_users, move_persons, move_troops, delete_sg, semester_id): start_time = time.time() oldsg = ScoutGroup.getbyname(oldname) if oldsg is None: raise RuntimeError("Old sg name:%s not found" % oldname) newsg = ScoutGroup.getbyname(newname) if newsg is None: raise RuntimeError("New sg name:%s not found" % newname) if not commit: taskProgress.info("*** testmode ***") keys_to_delete = [] entities_to_put_first = [] entities_to_put = [] taskProgress.info("termin: %s" % semester_id) convertsemester = Semester.getbyId(semester_id) if convertsemester is None: taskProgress.error("termin: %s does not exist" % semester_id) currentsemester = Semester.getOrCreateCurrent() if move_users: taskProgress.info("Update all users to the new scoutgroup") for u in UserPrefs.query(UserPrefs.groupaccess == oldsg.key).fetch(): logging.info(" * * moving user for %s" % (u.getname())) u.groupaccess = newsg.key u.activeSemester = currentsemester.key entities_to_put.append(u) if move_persons: taskProgress.info("Moving all persons to the new ScoutGroup:%s" % newsg.getname()) for oldp in Person.query(Person.scoutgroup == oldsg.key).fetch(): oldp.scoutgroup = newsg.key entities_to_put.append(oldp) if move_troops: taskProgress.info("Move all troops to the new ScoutGroup:%s, semester: %s" % (newsg.getname(), convertsemester.getname())) for oldt in Troop.query(Troop.scoutgroup == oldsg.key, Troop.semester_key == convertsemester.key).fetch(): taskProgress.info(" * found old troop for %s, semester=%s" % (str(oldt.key.id()), oldt.semester_key.get().getname())) keys_to_delete.append(oldt.key) newt = Troop.get_by_id(Troop.getid(oldt.scoutnetID, newsg.key, oldt.semester_key), use_memcache=True) if newt is None: taskProgress.info(" * * creating new troop for %s, semester=%s" % (str(oldt.key.id()), oldt.semester_key.get().getname())) newt = Troop.create(oldt.name, oldt.scoutnetID, newsg.key, oldt.semester_key) entities_to_put_first.append(newt) # put first to be able to reference it else: taskProgress.info(" * * already has new troop for %s, semester=%s" % (str(newt.key.id()), newt.semester_key.get().getname())) taskProgress.info(" * Move all trooppersons to the new group") for oldtp in TroopPerson.query(TroopPerson.troop == oldt.key).fetch(): keys_to_delete.append(oldtp.key) newtp = TroopPerson.get_by_id(TroopPerson.getid(newt.key, oldtp.person), use_memcache=True) if newtp is None: logging.info(" * * creating new TroopPerson for %s:%s" % (newt.getname(), oldtp.getname())) newtp = TroopPerson.create_or_update(newt.key, oldtp.person, oldtp.leader) entities_to_put.append(newtp) else: logging.info(" * * already has TroopPerson for %s:%s" % (newt.getname(), oldtp.getname())) taskProgress.info(" * Move all old meetings to the new troop") for oldm in Meeting.query(Meeting.troop==oldt.key).fetch(): keys_to_delete.append(oldm.key) newm = Meeting.get_by_id(Meeting.getId(oldm.datetime, newt.key), use_memcache=True) if newm is None: logging.info(" * * creating new Meeting for %s:%s" % (newt.getname(), oldm.datetime.strftime("%Y-%m-%d %H:%M"))) newm = Meeting(id=Meeting.getId(oldm.datetime, newt.key), datetime=oldm.datetime, name=oldm.name, troop=newt.key, duration=oldm.duration, ishike=oldm.ishike) newm.attendingPersons = oldm.attendingPersons entities_to_put.append(newm) else: logging.info(" * * merging Meeting %s->%s :%s" % (oldm.getname(), newm.getname(), oldm.datetime.strftime("%Y-%m-%d %H:%M"))) need_to_put = False if len(oldm.name) > len(newm.name): # take the longer name. newm.name = oldm.name need_to_put = True for oldattendingpersonkey in oldm.attendingPersons: if oldattendingpersonkey not in newm.attendingPersons: newm.attendingPersons.append(oldattendingpersonkey) need_to_put = True if need_to_put: entities_to_put.append(newm) if delete_sg: keys_to_delete.append(oldsg.key) taskProgress.info("time before put: %s s" % str(time.time() - start_time)) logging.info("Putting %d entities first" % len(entities_to_put_first)) if commit: ndb.put_multi(entities_to_put_first) logging.info("Putting %d entities" % len(entities_to_put)) if commit: ndb.put_multi(entities_to_put) logging.info("Deleting %d keys" % len(keys_to_delete)) if commit: ndb.delete_multi(keys_to_delete) logging.info("clear memcache") if commit: ndb.get_context().clear_cache() taskProgress.info("Done! time: %s s" % str(time.time() - start_time))
def DoImport(self, data, semester): """ :param data: from scoutnet :type data: str :type semester: data.Semester :rtype bool """ if not self.commit: self.result.append("*** sparar inte, test mode ***") if data == None or len(data) < 80: self.result.error(u"ingen data från scoutnet") return False list = scoutnet.GetScoutnetDataListJson(data) self.result.append("antal personer=%d" % len(list)) if len(list) < 1: self.result.error(u"för få rader: %d st" % len(list)) return False personsToSave = [] troopPersonsToSave = [] activePersons = set() # person.ids that was seen in this import for p in list: id = int(p["id"]) person = Person.get_by_id( id, use_memcache=True ) # need to be an integer due to backwards compatibility with imported data personnr = p["personnr"].replace('-', '') if len(personnr) < 12: self.result.warning( u"%s %s har inte korrekt personnummer: '%s', hoppar över personen" % (p["firstname"], p["lastname"], personnr)) continue if person == None: id = personnr person = Person.get_by_id( id, use_memcache=True ) # attempt to find using personnr, created as a local person if person != None: person.firstname = p["firstname"] person.lastname = p["lastname"] person.setpersonnr(personnr) if person.notInScoutnet != None: person.notInScoutnet = False else: person = Person.create(id, p["firstname"], p["lastname"], p["personnr"]) self.result.append("Ny person:%s %s %s" % (id, p["firstname"], p["lastname"])) activePersons.add(id) person.removed = False person.patrool = p["patrool"] person.email = p["email"] person.member_no = p["member_no"] person.phone = p["phone"] person.mobile = p["mobile"] person.alt_email = p["contact_alt_email"] person.mum_name = p["mum_name"] person.mum_email = p["mum_email"] person.mum_mobile = p["mum_mobile"] person.dad_name = p["dad_name"] person.dad_email = p["dad_email"] person.dad_mobile = p["dad_mobile"] person.street = p["street"] person.zip_code = p["zip_code"] person.zip_name = p["zip_name"] person.troop_roles = p["troop_roles"] person.group_roles = p["group_roles"] if semester.year not in person.member_years: person.member_years.append(semester.year) person._dirty = True scoutgroup = self.GetOrCreateGroup(p["group"], p["group_id"]) person.scoutgroup = scoutgroup.key if len(p["troop"]) == 0: self.result.warning(u"Ingen avdelning vald för %s %s %s" % (id, p["firstname"], p["lastname"])) troop = self.GetOrCreateTroop(p["troop"], p["troop_id"], scoutgroup.key, semester.key) troop_key = troop.key if troop != None else None person.troop = troop_key if person._dirty: self.result.append(u"Sparar ändringar:%s %s %s" % (id, p["firstname"], p["lastname"])) if self.commit: personsToSave.append(person) if troop_key != None: if self.commit: tp = TroopPerson.get_by_id(TroopPerson.getid( person.troop, person.key), use_memcache=True) if tp == None: tp = TroopPerson.create(troop_key, person.key, False) troopPersonsToSave.append(tp) self.result.append( u"Ny avdelning '%s' för:%s %s" % (p["troop"], p["firstname"], p["lastname"])) # check if old persons are still members, mark persons not imported in the pass as removed if len( personsToSave ) > 0: # protect agains a failed import, with no persons maring everyone as removed previousPersons = Person.query(Person.scoutgroup == scoutgroup.key, Person.removed != True) for previousPersonKey in previousPersons.iter(keys_only=True): if previousPersonKey.id() not in activePersons: personToMarkAsRemoved = previousPersonKey.get() personToMarkAsRemoved.removed = True self.result.append( u"%s finns inte i scoutnet, markeras som borttagen" % (personToMarkAsRemoved.getname())) if personToMarkAsRemoved in personsToSave: raise Exception( 'A removed person cannot be in that list') personsToSave.append(personToMarkAsRemoved) if self.commit: ndb.put_multi(personsToSave) ndb.put_multi(troopPersonsToSave) return True
def show(sgroup_url=None, troop_url=None, key_url=None): user = UserPrefs.current() if not user.hasAccess(): return "denied", 403 breadcrumbs = [{'link': '/', 'text': 'Hem'}] section_title = u'Kårer' breadcrumbs.append({'link': '/start', 'text': section_title}) baselink = '/start/' scoutgroup = None if sgroup_url is not None: sgroup_key = ndb.Key(urlsafe=sgroup_url) scoutgroup = sgroup_key.get() baselink += sgroup_url + "/" breadcrumbs.append({'link': baselink, 'text': scoutgroup.getname()}) troop = None semester = user.activeSemester.get() if troop_url is not None and troop_url != 'lagerbidrag': baselink += troop_url + "/" troop_key = ndb.Key(urlsafe=troop_url) troop = troop_key.get() breadcrumbs.append({'link': baselink, 'text': troop.getname()}) semester = troop.semester_key.get() if key_url == "settings": section_title = u'Inställningar' baselink += "settings/" breadcrumbs.append({'link': baselink, 'text': section_title}) if request.method == "POST": troop.defaultstarttime = request.form['defaultstarttime'] troop.defaultduration = int(request.form['defaultduration']) troop.rapportID = int(request.form['rapportID']) troop.put() form = htmlform.HtmlForm('troopsettings') form.AddField('defaultstarttime', troop.defaultstarttime, 'Avdelningens vanliga starttid') form.AddField('defaultduration', troop.defaultduration, u'Avdelningens vanliga mötestid i minuter', 'number') form.AddField('rapportID', troop.rapportID, u'Unik rapport ID för kommunens närvarorapport', 'number') return render_template('form.html', heading=section_title, baselink=baselink, form=str(form), breadcrumbs=breadcrumbs) if key_url == "delete": if troop is None: return "", 404 if request.form and "confirm" in request.form: if not user.isGroupAdmin(): return "", 403 troop.delete() troop = None del breadcrumbs[-1] baselink = breadcrumbs[-1]["link"] else: form = htmlform.HtmlForm( 'deletetroop', submittext="Radera", buttonType="btn-danger", descriptionText= u"Vill du verkligen radera avdelningen och all registrerad närvaro?\n" u"Det går här inte att ångra.") form.AddField('confirm', '', '', 'hidden') return render_template('form.html', heading=section_title, baselink=baselink, form=str(form), breadcrumbs=breadcrumbs) if key_url == "newperson": section_title = "Ny person" baselink += key_url + "/" breadcrumbs.append({'link': baselink, 'text': section_title}) if request.method == "GET": return render_template('person.html', heading=section_title, baselink=baselink, breadcrumbs=breadcrumbs, troop_persons=[], scoutgroup=scoutgroup) elif request.method == "POST": pnr = request.form['personnummer'].replace('-', '') person = Person.createlocal(request.form['firstname'], request.form['lastname'], pnr, request.form['mobile'], request.form['phone'], request.form['email']) person.street = request.form["street"] person.zip_code = request.form["zip_code"] person.zip_name = request.form["zip_name"] if "patrol" in request.form: person.setpatrol(request.form["patrol"]) person.scoutgroup = sgroup_key logging.info("created local person %s", person.getname()) person.put() troop_person = TroopPerson.create(troop_key, person.key, False) troop_person.commit() if scoutgroup.canAddToWaitinglist(): try: if scoutnet.AddPersonToWaitinglist( scoutgroup, person.firstname, person.lastname, person.personnr, person.email, person.street, person.zip_code, person.zip_name, person.phone, person.mobile, troop, request.form['anhorig1_name'], request.form['anhorig1_email'], request.form['anhorig1_mobile'], request.form['anhorig1_phone'], request.form['anhorig2_name'], request.form['anhorig2_email'], request.form['anhorig2_mobile'], request.form['anhorig2_phone']): person.notInScoutnet = False person.put() except scoutnet.ScoutnetException as exp: return render_template('error.html', error=str(exp)) return redirect(breadcrumbs[-2]['link']) if request.method == "GET" and request.args and "action" in request.args: action = request.args["action"] logging.debug("action %s", action) if action == "lookupperson": if scoutgroup is None: raise ValueError('Missing group') name = request.args['name'].lower() if len(name) < 2: return "[]" logging.debug("name=%s", name) json_str = '[' person_counter = 0 for person in Person().query( Person.scoutgroup == sgroup_key).order( Person.removed, Person.firstname, Person.lastname): if person.getname().lower().find(name) != -1: if person_counter != 0: json_str += ', ' json_str += '{"name": "' + person.getnameWithStatus( ) + '", "url": "' + person.key.urlsafe() + '"}' person_counter += 1 if person_counter == 8: break json_str += ']' return json_str elif action == "addperson": if troop is None or key_url is None: raise ValueError('Missing troop or person') person_key = ndb.Key(urlsafe=key_url) person = person_key.get() logging.info("adding person=%s to troop=%d", person.getname(), troop.getname()) troop_person = TroopPerson.create(troop_key, person_key, person.isLeader()) troop_person.commit() return redirect(breadcrumbs[-1]['link']) elif action == "setsemester": if user is None or "semester" not in request.args: raise ValueError('Missing user or semester arg') semester_url = request.args["semester"] user.activeSemester = ndb.Key(urlsafe=semester_url) user.put() elif action == "removefromtroop" or action == "setasleader" or action == "removeasleader": if troop is None or key_url is None: raise ValueError('Missing troop or person') person_key = ndb.Key(urlsafe=key_url) tps = TroopPerson.query(TroopPerson.person == person_key, TroopPerson.troop == troop_key).fetch(1) if len(tps) == 1: troop_person = tps[0] if action == "removefromtroop": troop_person.delete() else: troop_person.leader = (action == "setasleader") troop_person.put() return "ok" else: logging.error('unknown action=%s', action) return "", 404 if request.method == "POST" and request.form and "action" in request.form: action = request.form["action"] if action == "saveattendance": if troop is None or scoutgroup is None or key_url is None: raise ValueError('Missing troop or group') meeting = ndb.Key(urlsafe=key_url).get() meeting.attendingPersons[:] = [] # clear the list for person_url in request.form["persons"].split(","): #logging.debug("person_url=%s", person_url) if len(person_url) > 0: person_key = ndb.Key(urlsafe=person_url) meeting.attendingPersons.append(person_key) meeting.put() return "ok" elif action == "addmeeting" or action == "updatemeeting": mname = request.form['name'] mdate = request.form['date'] mishike = bool(request.form.get('ishike')) mtime = request.form['starttime'].replace('.', ':') dtstring = mdate + "T" + mtime mduration = request.form['duration'] date_str = datetime.datetime.strptime(dtstring, "%Y-%m-%dT%H:%M") if action == "addmeeting": meeting = Meeting.getOrCreate(troop_key, mname, date_str, int(mduration), mishike) else: meeting = ndb.Key(urlsafe=key_url).get() meeting.name = mname meeting.datetime = date_str meeting.duration = int(mduration) meeting.ishike = mishike meeting.commit() return redirect(breadcrumbs[-1]['link']) elif action == "deletemeeting": meeting = ndb.Key(urlsafe=key_url).get() logging.debug("deleting meeting=%s", meeting.getname()) meeting.delete() return redirect(breadcrumbs[-1]['link']) elif action == "addhike": mname = request.form['name'] mdate = request.form['date'] mdays = int(request.form['days']) date_str = datetime.datetime.strptime(mdate, "%Y-%m-%d") for i in range(mdays): day_time = date_str + datetime.timedelta(days=i) meeting = Meeting.getOrCreate( troop_key, mname, day_time, duration=1440, # 24h (needs some value) ishike=True) meeting.commit() return redirect(breadcrumbs[-1]['link']) elif action == "savepatrol": patrolperson = ndb.Key(urlsafe=request.form['person']).get() patrolperson.setpatrol(request.form['patrolName']) patrolperson.put() return "ok" elif action == "newtroop": troopname = request.form['troopname'] troop_id = hash(troopname) conflict = Troop.get_by_id(Troop.getid(troop_id, scoutgroup.key, user.activeSemester), use_memcache=True) if conflict is not None: return "Avdelningen finns redan", 404 troop = Troop.create(troopname, troop_id, scoutgroup.key, user.activeSemester) troop.put() troop_key = troop.key logging.info("created local troop %s", troopname) action = "" return redirect(breadcrumbs[-1]['link']) else: logging.error('unknown action=%s', action) return "", 404 # render main pages if scoutgroup is None: return render_template('index.html', heading=section_title, baselink=baselink, items=ScoutGroup.getgroupsforuser(user), breadcrumbs=breadcrumbs) elif troop_url == "lagerbidrag": return lagerbidrag.render_lagerbidrag(request, scoutgroup, "group", user=user, sgroup_key=sgroup_key) elif troop is None: section_title = 'Avdelningar' return render_template( 'troops.html', heading=section_title, baselink=baselink, scoutgroupinfolink='/scoutgroupinfo/' + sgroup_url + '/', groupsummarylink='/groupsummary/' + sgroup_url + '/', user=user, semester=semester, semesters=sorted(Semester.query(), semester_sort), troops=sorted(Troop.getTroopsForUser(sgroup_key, user), key=attrgetter('name')), lagerplats=scoutgroup.default_lagerplats, breadcrumbs=breadcrumbs) elif key_url is not None and key_url not in ("dak", "sensus", "lagerbidrag", "excel", "excel_sthlm", "json"): meeting = ndb.Key(urlsafe=key_url).get() section_title = meeting.getname() baselink += key_url + "/" breadcrumbs.append({'link': baselink, 'text': section_title}) return render_template('meeting.html', heading=section_title, baselink=baselink, existingmeeting=meeting, breadcrumbs=breadcrumbs, semester=troop.semester_key.get(), troop=troop) else: meeting_count = 0 sum_male_attendance_count = 0 sum_female_attendance_count = 0 sum_male_leader_attendance_count = 0 sum_female_leader_attendance_count = 0 no_leader_meeting_count = 0 too_small_group_meeting_count = 0 age_problem_count = 0 age_problem_desc = [] section_title = troop.getname() troop_persons = TroopPerson.getTroopPersonsForTroop(troop_key) meetings = Meeting.gettroopmeetings(troop_key) attendances = [] # [meeting][person] persons = [] persons_dict = {} for troop_person in troop_persons: person_key = troop_person.person person = troop_person.person.get() persons.append(person) persons_dict[person_key] = person year = semester.year for meeting in meetings: male_attendance_count = 0 female_attendence_count = 0 male_leader_attendance_count = 0 female_leader_attendence_count = 0 meeting_attendance = [] for troop_person in troop_persons: is_attending = troop_person.person in meeting.attendingPersons meeting_attendance.append(is_attending) if is_attending: person = persons_dict[troop_person.person] age = person.getyearsoldthisyear(year) if troop_person.leader: if 13 <= age <= 100: if female_leader_attendence_count + male_leader_attendance_count < 2: if person.isFemale(): female_leader_attendence_count += 1 else: male_leader_attendance_count += 1 else: age_problem_count += 1 age_problem_desc.append(person.getname() + ": " + str(age)) else: if 7 <= age <= 25: if person.isFemale(): female_attendence_count += 1 else: male_attendance_count += 1 else: age_problem_count += 1 age_problem_desc.append(person.getname() + ": " + str(age)) attendances.append(meeting_attendance) total_attendance = male_attendance_count + female_attendence_count # max 40 people if total_attendance > 40: surplus_people = total_attendance - 40 removed_men = min(male_attendance_count, surplus_people) male_attendance_count -= removed_men surplus_people -= removed_men female_attendence_count -= surplus_people max_leaders = 1 if total_attendance <= 10 else 2 total_leaders = female_leader_attendence_count + male_leader_attendance_count if total_attendance < 3: too_small_group_meeting_count += 1 else: if total_leaders == 0: no_leader_meeting_count += 1 else: meeting_count += 1 sum_female_attendance_count += female_attendence_count sum_male_attendance_count += male_attendance_count if total_leaders > max_leaders: if male_leader_attendance_count > max_leaders and female_leader_attendence_count == 0: male_leader_attendance_count = max_leaders elif male_leader_attendance_count == 0 and female_leader_attendence_count > max_leaders: female_leader_attendence_count = max_leaders else: female_leader_attendence_count = max_leaders / 2 max_leaders -= female_leader_attendence_count male_leader_attendance_count = max_leaders sum_female_leader_attendance_count += female_leader_attendence_count sum_male_leader_attendance_count += male_leader_attendance_count if key_url in ("dak", "excel", "excel_sthlm", "json"): dak = DakData() dak.foerenings_namn = scoutgroup.getname() dak.forenings_id = scoutgroup.foreningsID dak.organisationsnummer = scoutgroup.organisationsnummer dak.kommun_id = scoutgroup.kommunID dak.kort.namn_paa_kort = troop.getname() # hack generate an "unique" id, if there is none if troop.rapportID is None or troop.rapportID == 0: troop.rapportID = random.randint(1000, 1000000) troop.put() dak.kort.naervarokort_nummer = str(troop.rapportID) for troop_person in troop_persons: p = persons_dict[troop_person.person] if troop_person.leader: dak.kort.ledare.append( Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), True, p.email, p.mobile, p.zip_code)) else: dak.kort.deltagare.append( Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), False, p.email, p.mobile, p.zip_code)) for m in meetings: if (not scoutgroup.attendance_incl_hike) and m.ishike: continue sammankomst = Sammankomst(str(m.key.id()[:50]), m.datetime, m.duration, m.getname()) for troop_person in troop_persons: is_attending = troop_person.person in m.attendingPersons if is_attending: p = persons_dict[troop_person.person] if troop_person.leader: sammankomst.ledare.append( Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), True, p.email, p.mobile, p.zip_code)) else: sammankomst.deltagare.append( Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), False, p.email, p.mobile, p.zip_code)) dak.kort.sammankomster.append(sammankomst) if key_url in ("excel", "excel_sthlm"): if key_url == "excel": excel_report = ExcelReport(dak, semester) else: dak.kort.lokal = scoutgroup.default_lagerplats excel_report = ExcelReportSthlm(dak, semester) resultbytes = excel_report.getFilledInExcelSpreadsheet() response = make_response(resultbytes) response.headers[ 'Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' response.headers['Content-Disposition'] = ( 'attachment; filename=' + urllib.quote(str(dak.kort.namn_paa_kort), safe='') + '-' + semester.getname() + '.xlsx;') return response elif key_url == "json": json_report = JsonReport(dak, semester) resultbytes = json_report.get_report_string() response = make_response(resultbytes) response.headers['Content-Type'] = json_report.get_mime_type() response.headers[ 'Content-Disposition'] = 'attachment; filename=' + urllib.quote( json_report.get_filename(), safe='') + ';' return response else: result = render_template('dak.xml', dak=dak) response = make_response(result) response.headers['Content-Type'] = 'application/xml' response.headers['Content-Disposition'] = ( 'attachment; filename=' + urllib.quote(str(dak.kort.namn_paa_kort), safe='') + '-' + semester.getname() + '.xml;') return response elif key_url == "sensus": leaders = [] for troop_person in troop_persons: if troop_person.leader: leaders.append(troop_person.getname()) patrols = [] for p in persons: if p.getpatrol() not in patrols: patrols.append(p.getpatrol()) sensusdata = sensus.SensusData() sensusdata.foereningsNamn = scoutgroup.getname() sensusdata.foreningsID = scoutgroup.foreningsID sensusdata.organisationsnummer = scoutgroup.organisationsnummer sensusdata.kommunID = scoutgroup.kommunID sensusdata.verksamhetsAar = semester.getname() for patrol in patrols: sensuslista = sensus.SensusLista() sensuslista.NamnPaaKort = troop.getname() + "/" + patrol for troop_person in troop_persons: p = persons_dict[troop_person.person] if p.getpatrol() != patrol: continue if troop_person.leader: sensuslista.ledare.append( sensus.Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), True, p.email, p.mobile)) else: sensuslista.deltagare.append( sensus.Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), False)) for m in meetings: sammankomst = sensus.Sammankomst(str(m.key.id()[:50]), m.datetime, m.duration, m.getname()) for troop_person in troop_persons: p = persons_dict[troop_person.person] if p.getpatrol() != patrol: continue is_attending = troop_person.person in m.attendingPersons if troop_person.leader: sammankomst.ledare.append( sensus.Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), True, p.email, p.mobile, is_attending)) else: sammankomst.deltagare.append( sensus.Deltagare(p.getReportID(), p.firstname, p.lastname, p.getpersonnr(), False, p.email, p.mobile, is_attending)) sensuslista.Sammankomster.append(sammankomst) sensusdata.listor.append(sensuslista) result = render_template('sensusnarvaro.html', sensusdata=sensusdata) response = make_response(result) return response elif key_url == "lagerbidrag": return lagerbidrag.render_lagerbidrag(request, scoutgroup, "troop", trooppersons=troop_persons, troop_key=troop_key) else: allowance = [] allowance.append({'name': 'Antal möten:', 'value': meeting_count}) allowance.append({'name': 'Deltagartillfällen', 'value': ''}) allowance.append({ 'name': 'Kvinnor:', 'value': sum_female_attendance_count }) allowance.append({ 'name': 'Män:', 'value': sum_male_attendance_count }) allowance.append({ 'name': 'Ledare Kvinnor:', 'value': sum_female_leader_attendance_count }) allowance.append({ 'name': 'Ledare Män:', 'value': sum_male_leader_attendance_count }) if no_leader_meeting_count > 0: allowance.append({ 'name': 'Antal möten utan ledare', 'value': no_leader_meeting_count }) if too_small_group_meeting_count > 0: allowance.append({ 'name': 'Antal möten med för få deltagare', 'value': too_small_group_meeting_count }) if age_problem_count > 0: allowance.append({ 'name': 'Ålder utanför intervall:', 'value': age_problem_count }) if age_problem_desc != "": age_problem_desc_str = ','.join(age_problem_desc[:3]) if len(age_problem_desc) > 3: age_problem_desc_str += "..." allowance.append({'name': '', 'value': age_problem_desc_str}) return render_template('troop.html', heading=section_title, semestername=semester.getname(), baselink='/persons/' + scoutgroup.key.urlsafe() + '/', persons=persons, trooppersons=troop_persons, meetings=meetings, attendances=attendances, breadcrumbs=breadcrumbs, allowance=allowance, troop=troop, user=user, semester=semester, lagerplats=scoutgroup.default_lagerplats)