Ejemplo n.º 1
0
def import_():
    user = UserPrefs.current()
    if not user.canImport():
        return "denied", 403

    breadcrumbs = [{
        'link': '/',
        'text': 'Hem'
    }, {
        'link': '/import',
        'text': 'Import'
    }]

    currentSemester = Semester.getOrCreateCurrent()
    semesters = [currentSemester]
    semesters.extend(Semester.query(Semester.key != currentSemester.key))
    if request.method != 'POST':
        return render_template('updatefromscoutnetform.html',
                               heading="Import",
                               breadcrumbs=breadcrumbs,
                               user=user,
                               semesters=semesters)

    api_key = request.form.get('apikey').strip()
    groupid = request.form.get('groupid').strip()
    semester_key = ndb.Key(urlsafe=request.form.get('semester'))
    return startAsyncImport(api_key, groupid, semester_key, user, request)
Ejemplo n.º 2
0
def adminMergeScoutGroups():
    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'Merge SG'
    baselink += 'merge_sg/'
    breadcrumbs.append({'link':baselink, 'text':section_title})

    if request.method == 'POST':
        oldname = request.form.get('oldname').strip()
        newname = request.form.get('newname').strip()
        commit = request.form.get('commit') == 'on'
        move_users = request.form.get('move_users') == 'on'
        move_persons = request.form.get('move_persons') == 'on'
        move_troops = request.form.get('move_troops') == 'on'
        delete_sg = request.form.get('delete_sg') == 'on'
        semester_id =  request.form.get('semester')
        return startAsyncMergeSG(oldname, newname, commit, user, move_users, move_persons, move_troops, delete_sg, semester_id)
    else:
        return render_template('merge_sg.html',
            heading=section_title,
            baselink=baselink,
            breadcrumbs=breadcrumbs,
            semesters=Semester.getAllSemestersSorted())
Ejemplo n.º 3
0
def import_():
    user = UserPrefs.current()
    if not user.canImport():
        return "denied", 403

    breadcrumbs = [{
        'link': '/',
        'text': 'Hem'
    }, {
        'link': '/import',
        'text': 'Import'
    }]

    groupid = ""
    apikey = ""
    if user.groupaccess is not None:
        scoutgroup = user.groupaccess.get()
        apikey = scoutgroup.apikey_all_members
        groupid = scoutgroup.scoutnetID

    if request.method == 'POST':
        apikey = request.form.get('apikey').strip()
        groupid = request.form.get('groupid').strip()
        semester_key = Semester.getOrCreateCurrent().key
        return startAsyncImport(apikey, groupid, semester_key, user, request)

    return render_template('updatefromscoutnetform.html',
                           heading="Import",
                           breadcrumbs=breadcrumbs,
                           user=user,
                           groupid=groupid,
                           apikey=apikey)
Ejemplo n.º 4
0
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())
Ejemplo n.º 5
0
def settroopsemester():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403

    dosettroopsemester()
    return redirect('/admin/')
Ejemplo n.º 6
0
def dodelete():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403

    # DeleteAllData() # uncomment to enable this
    return redirect('/admin/')
Ejemplo n.º 7
0
def doupdateschemas():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403

    UpdateSchemas()
    return redirect('/admin/')
Ejemplo n.º 8
0
Archivo: main.py Proyecto: kesse/skojjt
def adminMergeScoutGroups():
    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'Merge SG'
    baselink += 'merge_sg/'
    breadcrumbs.append({'link':baselink, 'text':section_title})

    if request.method == 'POST':
        oldname = request.form.get('oldname').strip()
        newname = request.form.get('newname').strip()
        commit = request.form.get('commit') == 'on'
        merge_sg(oldname, newname, commit)

    return render_template('merge_sg.html',
        heading=section_title,
        baselink=baselink,
        breadcrumbs=breadcrumbs)
Ejemplo n.º 9
0
Archivo: main.py Proyecto: kesse/skojjt
def adminSemester():
    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'Terminer'
    baselink += 'semester/'
    breadcrumbs.append({'link':baselink, 'text':section_title})

    semesters = sorted(Semester.query(), semester_sort, reverse=True)

    if request.method == 'POST':
        locked_semesters = request.form.getlist('locked')

        for s in semesters:
            s.locked = s.key.urlsafe() in locked_semesters
            s.put()

    return render_template('semesters.html',
                            heading="Terminer",
                            baselink=baselink,
                            breadcrumbs=breadcrumbs,
                            semesters=semesters)
Ejemplo n.º 10
0
Archivo: main.py Proyecto: kesse/skojjt
def groupaccess(userprefs_url=None):
    user = UserPrefs.current()
    if not user.isGroupAdmin():
        return "denied", 403

    section_title = u'Hem'
    baselink = '/'
    breadcrumbs = [{'link':baselink, 'text':section_title}]

    section_title = u'Kåraccess'
    baselink += 'groupaccess/'
    breadcrumbs.append({'link':baselink, 'text':section_title})

    if userprefs_url != None:
        userprefs = ndb.Key(urlsafe=userprefs_url).get()
        groupaccessurl = request.args["setgroupaccess"]
        if groupaccessurl == 'None':
            userprefs.groupaccess = None
        else:
            userprefs.groupaccess = ndb.Key(urlsafe=groupaccessurl)
            userprefs.hasaccess = True
        userprefs.put()

    users = UserPrefs().query(UserPrefs.groupaccess == None).fetch()
    users.extend(UserPrefs().query(UserPrefs.groupaccess == user.groupaccess).fetch())
    return render_template('groupaccess.html',
        heading=section_title,
        baselink=baselink,
        users=users,
        breadcrumbs=breadcrumbs,
        mygroupurl=user.groupaccess.urlsafe(),
        mygroupname=user.groupaccess.get().getname())
Ejemplo n.º 11
0
def admin_():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403

    breadcrumbs = [{'link':'/', 'text':'Hem'},
                   {'link':'/admin', 'text':'Admin'}]
    return render_template('admin.html', heading="Admin", breadcrumbs=breadcrumbs, username=user.getname())
Ejemplo n.º 12
0
def render_badge_for_user(request, person_url, badge_url, baselink, breadcrumbs):
    person_key = ndb.Key(urlsafe=person_url)
    person = person_key.get()
    badge_key = ndb.Key(urlsafe=badge_url)
    badge = badge_key.get()
    parts_done = BadgePartDone.parts_done(person_key, badge_key)
    parts_scout_done_map = {pd.idx: pd for pd in parts_done if pd.is_scout_part}
    parts_admin_done_map = {pd.idx: pd for pd in parts_done if not pd.is_scout_part}
    # logging.info("scout_map %s" % parts_scout_done_map)
    # logging.info("admin_map %s" % parts_admin_done_map)
    if request.method == "POST":
        update_scout = request.form['update_scout']
        update_admin = request.form['update_admin']
        scout_indices = []
        admin_indices = []
        if update_scout != "":
            scout_indices = [int(idx) for idx in update_scout.split(",")]
        if update_admin != "":
            admin_indices = [int(idx) for idx in update_admin.split(",")]
        if len(scout_indices) + len(admin_indices) == 0:
            return "ok"
        examiner_name = UserPrefs.current().name
        badge.update_for_person(person_key, scout_indices, admin_indices, examiner_name)
        return "ok" # TODO add render_template

    # logging.info("Badge %s for %s" % (badge.name, person.getname()))
    BadgePart = namedtuple('BadgePart', 'idx short_desc long_desc')
    scout_parts = []
    for i in range(len(badge.parts_scout_short)):
        scout_parts.append(BadgePart(i, badge.parts_scout_short[i], badge.parts_scout_long[i]))
    admin_parts = []
    for i in range(len(badge.parts_admin_short)):
        admin_parts.append(BadgePart(i, badge.parts_admin_short[i], badge.parts_admin_long[i]))
    Done = namedtuple('Done', 'idx approved done')
    scout_done = []
    for sp in scout_parts:
        if sp.idx in parts_scout_done_map:
            pd = parts_scout_done_map[sp.idx]
            scout_done.append(Done(sp.idx, pd.date.strftime("%Y-%m-%d") + " " + pd.examiner_name, True))
        else:
            scout_done.append(Done(sp.idx, "- - -", False))
    admin_done = []
    for ap in admin_parts:
        if ap.idx in parts_admin_done_map:
            pd = parts_admin_done_map[ap.idx]
            admin_done.append(Done(ap.idx, pd.date.strftime("%Y-%m-%d") + " " + pd.examiner_name, True))
        else:
            admin_done.append(Done(ap.idx, "- - -", False))

    return render_template('badgeparts_person.html',
                           person=person,
                           baselink=baselink,
                           breadcrumbs=breadcrumbs,
                           badge=badge,
                           scout_parts=scout_parts,
                           admin_parts=admin_parts,
                           scout_done=scout_done,
                           admin_done=admin_done)
Ejemplo n.º 13
0
def autoGroupAccess():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403
    users = UserPrefs().query().fetch()
    for u in users:
        u.attemptAutoGroupAccess()

    return "done"
Ejemplo n.º 14
0
def dobackup():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403

    response = make_response(GetBackupXML())
    response.headers['Content-Type'] = 'application/xml'
    thisdate = datetime.datetime.now()
    response.headers['Content-Disposition'] = 'attachment; filename=skojjt-backup-' + str(thisdate.isoformat()) + '.xml'
    return response
Ejemplo n.º 15
0
def setcurrentsemester():
    user = UserPrefs.current()
    if not user.isAdmin():
        return "denied", 403

    semester = Semester.getOrCreateCurrent()
    for u in UserPrefs.query().fetch():
        u.activeSemester = semester.key
        u.put()

    return redirect('/admin/')
Ejemplo n.º 16
0
def show(sgroup_url):
    user = UserPrefs.current()
    if not user.canImport():
        return "denied", 403
    breadcrumbs = [{'link': '/', 'text': 'Hem'}]
    baselink = "/scoutgroupinfo/"
    section_title = "Kårinformation"
    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()})
    if request.method == "POST":
        logging.info("POST, %s" % str(request.form))
        scoutgroup.organisationsnummer = request.form[
            'organisationsnummer'].strip()
        scoutgroup.foreningsID = request.form['foreningsID'].strip()
        scoutgroup.scoutnetID = request.form['scoutnetID'].strip()
        scoutgroup.kommunID = request.form['kommunID'].strip()
        scoutgroup.apikey_waitinglist = request.form[
            'apikey_waitinglist'].strip()
        scoutgroup.apikey_all_members = request.form[
            'apikey_all_members'].strip()
        scoutgroup.bankkonto = request.form['bankkonto'].strip()
        scoutgroup.adress = request.form['adress'].strip()
        scoutgroup.postadress = request.form['postadress'].strip()
        scoutgroup.epost = request.form['epost'].strip()
        scoutgroup.telefon = request.form['telefon'].strip()
        scoutgroup.default_lagerplats = request.form['lagerplats'].strip()
        scoutgroup.firmatecknare = request.form['firmatecknare'].strip()
        scoutgroup.firmatecknartelefon = request.form[
            'firmatecknartelefon'].strip()
        scoutgroup.firmatecknaremail = request.form['firmatecknaremail'].strip(
        )
        scoutgroup.attendance_min_year = request.form.get(
            'attendance_min_year', type=int)
        scoutgroup.attendance_incl_hike = request.form.get(
            'attendance_incl_hike') == 'on'
        scoutgroup.put()
        if "import" in request.form:
            return startAsyncImport(scoutgroup.apikey_all_members,
                                    scoutgroup.scoutnetID,
                                    Semester.getOrCreateCurrent().key, user,
                                    request)
        else:
            return redirect(breadcrumbs[-1]['link'])
    else:
        return render_template('scoutgroupinfo.html',
                               heading=section_title,
                               baselink=baselink,
                               scoutgroup=scoutgroup,
                               breadcrumbs=breadcrumbs)
Ejemplo n.º 17
0
def sendRegistrationQueueInformationEmail(scoutgroup):
    try:
        message = mail.EmailMessage(sender="*****@*****.**",
                                    subject=u"Ny person i scoutnets kölista",
                                    body=render_template(
                                        "email_queueinfo.txt",
                                        scoutgroup=scoutgroup))
        user = UserPrefs.current()
        message.to = user.getemail()
        message.send()

    except apiproxy_errors.OverQuotaError as message:
        # Log the error.
        logging.error(message)
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
Archivo: main.py Proyecto: kesse/skojjt
def home():
    breadcrumbs = [{'link':'/', 'text':'Hem'}]
    user=UserPrefs.current()
    user.attemptAutoGroupAccess()
    starturl = '/start/'
    personsurl = '/persons/'
    if user.groupaccess != None:
        starturl += user.groupaccess.urlsafe() + '/'
        personsurl += user.groupaccess.urlsafe() + '/'
    return render_template('start.html',
                           heading='Hem',
                           items=[],
                           breadcrumbs=breadcrumbs,
                           user=user,
                           starturl=starturl,
                           personsurl=personsurl,
                           logouturl=users.create_logout_url('/')
                           )
Ejemplo n.º 20
0
Archivo: main.py Proyecto: kesse/skojjt
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":
        adminEmails = [u.email for u in UserPrefs.query(UserPrefs.hasadminaccess==True).fetch()]
        if len(adminEmails) > 0:
            scoutgroup_name = request.form.get('sg').strip()
            mail.send_mail(sender=user.email,
            to=','.join(adminEmails),
            subject="Användren: " + user.getname() + " vill ha access.\nKår: " + scoutgroup_name,
            body="""""")
        return redirect('/')
    else:
        return render_template('getaccess.html',
            baselink=baselink,
            breadcrumbs=breadcrumbs)
Ejemplo n.º 21
0
def cleanup():
    user = UserPrefs.current()
    if not user.isGroupAdmin():
        return "", 403

    heading="Cleanup"
    baselink="/admin/cleanup/"
    breadcrumbs = [{'link':'/', 'text':'Hem'},
                   {'link':'/admin', 'text':'Admin'},
                   {'link':'/admin/cleanup', 'text':'Cleanup'}]

    if request.method == 'POST' and request.form is not None:
        commit = "commit" in request.form and request.form['commit'] == 'on'

        tps = TroopBadge.query().fetch()
        tp_keys_to_remove = []
        items = []
        if not commit:
            items.append('testmode')
        for tp in tps:
            badge = Badge.get_by_id(tp.badge_key.id())
            if badge is None:
                tp_keys_to_remove.append(tp.key)
                items.append(str(tp.key))

        if commit:
            ndb.delete_multi(tp_keys_to_remove)

        return render_template('table.html',
                                heading=heading,
                                baselink=baselink,
                                tabletitle="Items to remove",
                                items=items,
                                breadcrumbs=breadcrumbs)
    else:
        form = htmlform.HtmlForm('cleanup', submittext="Radera", buttonType="btn-danger",
                                    descriptionText=u"Cleanup of bad TroopBadge records")
        form.AddField('commit', 'on', u'Commit to database', 'checkbox', False)
        return render_template('form.html',
                                heading=heading,
                                baselink=baselink,
                                form=str(form),
                                breadcrumbs=breadcrumbs)
Ejemplo n.º 22
0
def show(badge_url=None):
    logging.info("badgetemplates: badge_url=%s", badge_url)
    user = UserPrefs.current()
    if not user.hasAccess():
        return "denied badges", 403

    breadcrumbs = [{'link': '/', 'text': 'Hem'}]
    section_title = u'Märkesmallar'
    breadcrumbs.append({'link': '/badgetemplates', 'text': section_title})
    baselink = '/badgetemplates/'

    if badge_url is None:
        # logging.info("Render list of all badges for scout_group")
        section_title = 'Märken för kår'
        badges = Badge.get_badges('general')

        return render_template('badgelist.html',
                                heading=section_title,
                                baselink=baselink,
                                badges=badges,
                                breadcrumbs=breadcrumbs)
Ejemplo n.º 23
0
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())
Ejemplo n.º 24
0
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)
Ejemplo n.º 25
0
def show(sgroup_url=None, badge_url=None, troop_url=None, person_url=None, action=None, template_url=None):
    # logging.info("badges.py: sgroup_url=%s, badge_url=%s, troop_url=%s, person_url=%s, action=%s",
    #             sgroup_url, badge_url, troop_url, person_url, action)
    user = UserPrefs.current()  # TODO. Check right scout_group + troop_admin
    if not user.hasAccess():
        return "denied badges", 403

    breadcrumbs = [{'link': '/', 'text': 'Hem'}]
    section_title = u'Märken'
    breadcrumbs.append({'link': '/badges', 'text': section_title})
    baselink = '/badges/'

    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()})

    if scoutgroup is None:
        return render_template(
            'index.html',
            heading=section_title,
            baselink=baselink,
            items=ScoutGroup.getgroupsforuser(user),
            breadcrumbs=breadcrumbs,
            username=user.getname())

    if troop_url is None and person_url is None:  # scoutgroup level
        if badge_url is None:
            # logging.info("Render list of all badges for scout_group")
            section_title = 'Märken för kår'
            badges = Badge.get_badges(sgroup_key)
            counts = []
            for badge in badges:
                counts.append(BadgeCompleted.query(BadgeCompleted.badge_key == badge.key).count())

            return render_template('badgelist.html',
                                   heading=section_title,
                                   baselink=baselink,
                                   badges=badges,
                                   counts=counts,
                                   breadcrumbs=breadcrumbs,
                                   user=user)
        # Specific badge or new badge
        if request.method == "GET":
            if template_url is not None:
                template = ndb.Key(urlsafe=template_url).get()
            else:
                template = None
            if badge_url == "newbadge":  # Get form for or create new
                section_title = "Nytt märke"
                badge = None
                if template:
                    name = template.name
                    description = template.description
                else:
                    name = "Nytt"
                    description = ""
            else:
                section_title = "Märke"
                badge_key = ndb.Key(urlsafe=badge_url)
                badge = badge_key.get()
                name = badge.name
                description = badge.description

            baselink += 'badge/' + badge_url + "/"
            if action is not None:
                baselink += action + '/'

            breadcrumbs.append({'link': baselink, 'text': name})

            if action == "showcompleted":
                badgeCompleted = BadgeCompleted.query(BadgeCompleted.badge_key == badge_key).fetch()
                Compl = namedtuple("Compl", "name date examiner")
                completed = [Compl(bc.person_key.get().getname(),
                                   bc.date.strftime("%Y-%m-%d"),
                                   bc.examiner) for bc in badgeCompleted]
                # logging.info("Completed: %s" % completed)
                return render_template('badge_completed_list.html',
                                       breadcrumbs=breadcrumbs,
                                       badge=badge,
                                       badge_completed=completed)
            other_badges = Badge.get_badges(sgroup_key)
            parts_scout = []
            parts_admin = []
            img_url = DEFAULT_IMG_URL
            if badge:
                parts_scout = zip(badge.parts_scout_short, badge.parts_scout_long)
                parts_admin = zip(badge.parts_admin_short, badge.parts_admin_long)
                other_badges = filter(lambda bdg: bdg.name != badge.name, other_badges)
                if badge.img_url:
                    img_url = badge.img_url
            elif template:
                parts_scout = zip(template.parts_scout_short, template.parts_scout_long)
                parts_admin = zip(template.parts_admin_short, template.parts_admin_long)
                if template.img_url:
                    img_url = template.img_url
            other_badge_names = ",".join([bdg.name for bdg in other_badges])
            # logging.info(other_badge_names)
            # logging.info(img_url)
            return render_template('badge.html',
                                   name=name,
                                   heading=section_title,
                                   baselink=baselink,
                                   breadcrumbs=breadcrumbs,
                                   description=description,
                                   parts_scout=parts_scout,
                                   parts_admin=parts_admin,
                                   img_url=img_url,
                                   action=action,
                                   other_badge_names=other_badge_names,
                                   scoutgroup=scoutgroup)
        if request.method == "POST":
            name = request.form['name']
            description = request.form['description']
            parts_scout = request.form['parts_scout'].split("::")
            parts_scout = [p.split("|") for p in parts_scout]
            parts_admin = request.form['parts_admin'].split("::")
            parts_admin = [p.split("|") for p in parts_admin]
            img_url = request.form['img_url']
            # logging.info('img_data=%s' % img_data[:32])
            # logging.info('description=%s' % description)
            # logging.info("name: %s, parts: %s", name, parts)
            if badge_url == "newbadge":
                badge = Badge.create(name, sgroup_key, description, parts_scout, parts_admin, img_url)
                return "ok"  # TODO add render_template
            else:  # Update an existing badge
                badge_key = ndb.Key(urlsafe=badge_url)
                badge = badge_key.get()
                badge.update(name, description, parts_scout, parts_admin, img_url)
                return "ok" # TODO add render_template
        else:
            return "Unsupported method %s" % request.method, 500

    if troop_url is not None and badge_url is None:
        # logging.info("TROOP_URL without BADGE_URL")
        troop_key = ndb.Key(urlsafe=troop_url)
        troop = troop_key.get()
        if request.method == "GET":
            # Since we come from /start/... instead of /badges/... replace part links
            for bc in breadcrumbs:
                bc['link'] = bc['link'].replace('badges', 'start')
                bc['text'] = bc['text'].replace('Märken', 'Kårer')
            baselink += "troop/" + troop_url + "/"
            badges = Badge.get_badges(sgroup_key)
            semester_key = troop.semester_key
            semester = semester_key.get()
            semester_name = semester.getname()
            section_title = "Märken för %s %s" % (troop.name, semester_name)
            breadcrumbs.append({'link': baselink, 'text': "Märken %s" % troop.name})
            troop_badges = TroopBadge.get_badges_for_troop(troop)
            # logging.info("Nr troop_badges is %d" % len(troop_badges))
            troop_badge_names = [tb.name for tb in troop_badges]
            return render_template('badges_for_troop.html',
                                   name=troop.name,
                                   heading=section_title,
                                   baselink=baselink,
                                   breadcrumbs=breadcrumbs,
                                   badges=badges,
                                   troop_badge_names=troop_badge_names,
                                   scoutgroup=scoutgroup)
        # POST
        new_badge_names = request.form['badges'].split("|")
        new_badge_names = [n for n in new_badge_names if n != ""]
        # logging.info(new_badge_names)
        TroopBadge.update_for_troop(troop, new_badge_names)
        return "ok"  # TODO add render_template

    if troop_url is not None and badge_url is not None:
        troop_key = ndb.Key(urlsafe=troop_url)
        troop = troop_key.get()
        badge_key = ndb.Key(urlsafe=badge_url)
        badge = badge_key.get()
        if request.method == "POST":
            if person_url is not None:
                return render_badge_for_user(request, person_url, badge_url, baselink, breadcrumbs)
            # logging.info("POST %s %s" % (troop.name, badge.name))
            update_scout = request.form['update_scout']
            update_admin = request.form['update_admin']
            if update_scout == "" and update_admin == "":
                return "ok"  # Return ok to Ajax call
            progress_scout = []
            if update_scout != "":
                progress_scout = update_scout.split(",")
            progress_admin = []
            if update_admin != "":
                progress_admin = update_admin.split(",")
            examiner_name = UserPrefs.current().name
            # logging.info("progress_scout: %s" % progress_scout)
            # logging.info("progress_admin: %s" % progress_admin)
            update_badge_progress(badge, progress_scout, "scout", examiner_name)
            update_badge_progress(badge, progress_admin, "admin", examiner_name)
            return "ok"  # Return ok to Ajax call # TODO add render_template
        if request.method == "GET":
            # logging.info("GET %s %s" % (troop.name, badge.name))
            # Since we come from /start/... instead of /badges/... replace part links
            for bc in breadcrumbs:
                bc['link'] = bc['link'].replace('badges', 'start')
                bc['text'] = bc['text'].replace('Märken', 'Kårer')
            baselink += "troop/" + troop_url + "/" + badge_url + "/"
            breadcrumbs.append({'link': baselink, 'text': "%s %s" % (troop.name, badge.name)})
            if person_url is not None:
                baselink += person_url + '/'
                person_key = ndb.Key(urlsafe=person_url)
                person = person_key.get()
                breadcrumbs.append({'link': baselink, 'text': person.getname()})
                return render_badge_for_user(request, person_url, badge_url, baselink, breadcrumbs)
            return render_badge_for_troop(sgroup_url, badge_key, badge, troop_key, troop, baselink, breadcrumbs)

    if person_url is not None:
        person_key = ndb.Key(urlsafe=person_url)
        person = person_key.get()
        baselink = "/badges/" + sgroup_url + '/person/' + person_url + '/'
        breadcrumbs.append({'link': baselink, 'text': "%s" % person.getname()})
        if badge_url is None:
            # logging.info("Badges for %s" % person.getname())
            badge_parts_done = BadgePartDone.query(BadgePartDone.person_key == person_key).fetch()
            badge_keys = {part.badge_key for part in badge_parts_done}
            badges = [bkey.get() for bkey in badge_keys]
            badges.sort(key=lambda x: x.name)
            badges_completed = BadgeCompleted.query(BadgeCompleted.person_key == person_key).fetch()
            # logging.info(badges_completed)
            completed_keys = {bc.badge_key for bc in badges_completed}
            completed = [b.key in completed_keys for b in badges]
            # logging.info("badges %s completed %s" % ([b.name for b in badges], completed))

            return render_template('badgelist_person.html',
                                   heading="Märken för %s" % person.getname(),
                                   baselink=baselink,
                                   breadcrumbs=breadcrumbs,
                                   badges=badges,
                                   completed=completed,
                                   badge_parts_done=badge_parts_done)
        # badge_url is not none

    if person_url is not None and badge_url is not None:
        baselink += badge_url + "/"
        badge_key = ndb.Key(urlsafe=badge_url)
        badge = badge_key.get()
        breadcrumbs.append({'link': baselink, 'text': "%s" % badge.name})
        return render_badge_for_user(request, person_url, badge_url, baselink, breadcrumbs)

    # note that we set the 404 status explicitly
    return "Page not found", 404
Ejemplo n.º 26
0
def show_template(badge_url=None, action=None, sgroup_url=None):
    """Common templates for badges that scout groups can use."""
    baselink = "/badges/templates/"
    breadcrumbs = [{'link': '/', 'text': 'Hem'}]
    if badge_url is None:
        # logging.info("Render list of all badges for scout_group")
        user = UserPrefs.current()
        section_title = 'Märkesmallar'
        templates = BadgeTemplate.get_templates()
        if sgroup_url is None:
            return render_template('badge_template_list.html',
                                   heading=section_title,
                                   baselink=baselink,
                                   badges=templates,
                                   breadcrumbs=breadcrumbs,
                                   user=user)
        else:
            breadcrumbs.append({'link': '/badges/', 'text': 'Märken'})
            sgroup_key = ndb.Key(urlsafe=sgroup_url)
            scoutgroup = sgroup_key.get()
            baselink = '/badges/' + sgroup_url + '/'
            breadcrumbs.append({'link': baselink, 'text': scoutgroup.getname()})
            return render_template('badge_template_choices.html',
                                   heading=section_title,
                                   baselink=baselink,
                                   badges=templates,
                                   breadcrumbs=breadcrumbs,
                                   user=user)
    # Specific badge or new badge
    if request.method == "GET":
        if badge_url == "newbadge":  # Get form for or create new
            section_title = "Ny mall"
            badge = None
            name = "Ny mall"
            description = "märkesbeskrivning"
        else:
            section_title = "Mall"
            tmpl_key = ndb.Key(urlsafe=badge_url)
            badge = tmpl_key.get()
            name = badge.name
            description = badge.description

        baselink += badge_url + "/"
        if action is not None:
            baselink += action + '/'
        breadcrumbs.append({'link': baselink, 'text': name})

        if badge is not None:
            parts_scout = zip(badge.parts_scout_short, badge.parts_scout_long)
            parts_admin = zip(badge.parts_admin_short, badge.parts_admin_long)
        else:
            parts_scout = []
            parts_admin = []
        img_url = DEFAULT_IMG_URL
        other_templates = BadgeTemplate.get_templates()
        if badge is not None:
            other_templates = filter(lambda t: t.name != badge.name, other_templates)
            if badge.img_url:
                img_url = badge.img_url
        other_template_names = ",".join([t.name for t in other_templates])
        return render_template('badge.html',
                               name=name,
                               heading=section_title,
                               baselink=baselink,
                               breadcrumbs=breadcrumbs,
                               description=description,
                               parts_scout=parts_scout,
                               parts_admin=parts_admin,
                               img_url=img_url,
                               action=action,
                               other_badge_names=other_template_names)
    if request.method == "POST":
        name = request.form['name']
        description = request.form['description']
        parts_scout = request.form['parts_scout'].split("::")
        parts_scout = [p.split("|") for p in parts_scout]
        parts_adm_str = request.form['parts_admin']
        if parts_adm_str != "":
            parts_admin = parts_adm_str.split("::")
            parts_admin = [p.split("|") for p in parts_admin]
        else:
            parts_admin = []
        img_url = request.form['img_url']
        # logging.info("name: %s, img_url: %s", name, img_url)
        if badge_url == "newbadge":
            badge = BadgeTemplate.create(name, description, parts_scout, parts_admin, img_url)
            return "ok"
        else:  # Update an existing badge
            badge_key = ndb.Key(urlsafe=badge_url)
            badge = badge_key.get()
            badge.update(name, description, parts_scout, parts_admin, img_url)
            return "ok"  # TODO add render_template
    else:
        return "Unsupported method %s" % request.method, 500
Ejemplo n.º 27
0
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)
Ejemplo n.º 28
0
def adminTestEmail():
    user = UserPrefs.current()
    scoutnet.sendRegistrationQueueInformationEmail(user.groupaccess.get())
    return "ok"
Ejemplo n.º 29
0
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)