コード例 #1
0
    def percentage(self):
        """
        Calculate what percentage of the badge has been completed
        """

        # load metadata for badge
        toc = loader.toc()
        psets = loader.psets()
        metadata = loader.badges()[self.id]

        # iterate over each chapter required for the badge
        total = 0
        earned = 0
        for chapter in metadata['chapters']:
            # check which sections in the chapter have been read
            for section in toc[chapter]['sections']:
                if chapter in self.reads and section['id'] in self.reads[chapter]:
                    earned += settings.POINTS['section']
                total += settings.POINTS['section']

            # add on points for pset
            earned += self.points[chapter]
            total += psets['points'][chapter]

        return float(earned) / float(total) * 100
コード例 #2
0
    def milestones(self):
        """
        Determine which milestones have been reached
        """

        # load metadata for badge
        toc = loader.toc()
        psets = loader.psets()
        metadata = loader.badges()[self.id]
        milestones = {}

        # iterate over each chapter required for the badge
        for chapter in metadata['chapters']:
            # pset milestone reached if all points have been earned
            milestone = {
                'chapter': True,
                'pset': self.points[chapter] == psets['points'][chapter]
            }

            # chapter milestone not reached if some section has not been read
            for section in toc[chapter]['sections']:
                if not chapter in self.reads or not section['id'] in self.reads[chapter]:
                    milestone['chapter'] = False
                    break

            # save milestone
            milestones[chapter] = milestone

        return milestones
コード例 #3
0
def before_request():
    # TODO temporary until logins work
    user = db.session.query(User).filter_by(id=1).one()
    session['user'] = user

    # get info for all badges
    g.all_badges = loader.badges()

    # get currently logged-in user
    g.user = None
    if 'user' in session:
        g.user = session['user']
        g.user.badges = g.user.get_badges()