def adminUpdateUpdatePersonIds(): user = UserPrefs.current() if not user.isAdmin(): return "denied", 403 section_title = u'Hem' baselink = '/' breadcrumbs = [{'link':baselink, 'text':section_title}] section_title = u'Admin' baselink += 'admin/' breadcrumbs.append({'link':baselink, 'text':section_title}) section_title = u'Update Person Ids' baselink += 'update_person_ids/' breadcrumbs.append({'link':baselink, 'text':section_title}) if request.method == 'POST': sgroup_key = None if len(request.form.get('groupaccess')) != 0: sgroup_key = ndb.Key(urlsafe=request.form.get('groupaccess')) commit = request.form.get('commit') == 'on' return startAsyncUpdatePersonIds(commit, sgroup_key) else: return render_template('update_person_ids.html', heading=section_title, baselink=baselink, breadcrumbs=breadcrumbs, scoutgroups=ScoutGroup.query().fetch())
def meeting_date_count_deferred(start_cursor): start_time = time.time() # collect meetings per scoutgroup and day, count number of trooppersons and meeting attendees meeting_date_counter_dict = memcache.get("meeting_date_counter_dict") # dict: datetime => number of attendee that day # {"Scoutgroup X": {meetings:[{"date": 2020-01-01, pers:"23"}, {"date": 2020-01-02, pers:"33"}]}, "Scoutgroup Y": {meetings:[{"date": 2020-01-01, pers:"23"}, {"date": 2020-01-02, pers:"33"}]}} there_is_more = true while there_is_more: sgs, start_cursor, there_is_more = ScoutGroup.query().fetch_page(page_size=1, start_cursor=start_cursor): for sg in sgs: logging.info("Updating:%s" % (semester.getname())) for semester in Semester.query().fetch(): for troop_key in Troop.query(Troop.scoutgroup==sg.key, Troop.semester_key==semester.key).fetch(keys_only=True): #troopPersonKeys = TroopPerson.query(TroopPerson.troop == troop.key).fetch(keys_only=True) #numTroopPersons = len(TroopPerson) for meeting in Meeting.query(Meeting.troop == troop_key).fetch(): attending_count = len(meeting.attendingPersons) meeting_day = meeting.datetime.strftime('%Y-%m-%d') if meeting_date_counter_dict.has_key(meeting_day): attending_count += meeting_date_counter_dict[meeting_day] meeting_date_counter_dict[meeting_day] = attending_count if there_is_more: memcache.delete("meeting_date_counter_dict") memcache.add("meeting_date_counter_dict", meeting_date_counter_dict) deferred.defer(meeting_date_count_deferred, start_cursor) meeting_date_counter = [{"date" : day_key, "delt": meeting_date_counter_dict[day_key]} for day_key in sorted(meeting_date_counter_dict)] logging.info("meeting_date_count update done") memcache.delete("meeting_date_count") memcache.add("meeting_date_count", meeting_date_counter) memcache.delete("update_meeting_date_count_is_running")
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 adminaccess(userprefs_url=None): user = UserPrefs.current() if not user.isAdmin(): return "denied", 403 section_title = u'Hem' baselink = '/' breadcrumbs = [{'link':baselink, 'text':section_title}] section_title = u'Admin' baselink += 'admin/' breadcrumbs.append({'link':baselink, 'text':section_title}) section_title = u'Access' baselink += 'access/' breadcrumbs.append({'link':baselink, 'text':section_title}) if userprefs_url != None: userprefs = ndb.Key(urlsafe=userprefs_url).get() if request.method == 'POST': userprefs.hasaccess = request.form.get('hasAccess') == 'on' userprefs.hasadminaccess = request.form.get('hasAdminAccess') == 'on' userprefs.groupadmin = request.form.get('groupadmin') == 'on' userprefs.canimport = request.form.get('canImport') == 'on' sgroup_key = None if len(request.form.get('groupaccess')) != 0: sgroup_key = ndb.Key(urlsafe=request.form.get('groupaccess')) userprefs.groupaccess = sgroup_key userprefs.put() else: section_title = userprefs.getname() baselink += userprefs_url + '/' breadcrumbs.append({'link':baselink, 'text':section_title}) return render_template('userprefs.html', heading=section_title, baselink=baselink, userprefs=userprefs, breadcrumbs=breadcrumbs, scoutgroups=ScoutGroup.query().fetch()) users = UserPrefs().query().fetch() return render_template('userlist.html', heading=section_title, baselink=baselink, users=users, breadcrumbs=breadcrumbs)
def getaccess(): user = UserPrefs.current() breadcrumbs = [{'link': '/', 'text': 'Hem'}] baselink = "/getaccess/" section_title = "Access" breadcrumbs.append({'link': baselink, 'text': section_title}) if request.method == "POST": # anti-spam protection, the user can only ask once. user_request_access_key = "request_access_" + user.getemail() if memcache.get(user_request_access_key) is not None: logging.warning("User is spamming req-access:" + user.getemail()) return "denied", 403 memcache.add(user_request_access_key, True) sgroup = None if len(request.form.get('scoutgroup')) != 0: sgroup = ndb.Key(urlsafe=request.form.get('scoutgroup')).get() if sgroup is not None: groupAdminEmails = UserPrefs.getAllGroupAdminEmails(sgroup.key) if len(groupAdminEmails) > 0: mail.send_mail( sender="*****@*****.**", to=','.join(groupAdminEmails), subject= u"""Användaren: {} vill ha access till närvaroregistrering i Skojjt för scoutkåren {}""".format(user.getemail(), sgroup.getname()), body=u"""Gå till {} för att lägga till {}""".format( request.host_url + "groupaccess/", user.getname())) return redirect('/') else: return render_template('getaccess.html', baselink=baselink, breadcrumbs=breadcrumbs, scoutgroups=ScoutGroup.query().fetch())