Ejemplo n.º 1
0
    def post(self):
        """Create a new competition."""
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")

        title = self.request.get("comp-title")
        description = self.request.get("comp-description")
        month = int(self.request.get("comp-month"))
        year = int(self.request.get("comp-year"))

        errors = []

        if not title:
            errors.append("You forgot to give this competition a title.")

        comp = Competition.get_by_title_date(title, month, year)
        if comp:
            errors.append(
                "A competition already exists with this title (%s), month (%s), "
                "and year (%d)" % (title, MONTHS[month], year)
            )

        if errors:
            data = {"errors": errors, "page_title": "New Competition", "user": user, "months": MONTHS}
            self.render("competition-new.html", **data)
            return

        # no errors so create new competiton
        start = date(year, month, 1)
        end = date(year, month, monthrange(year, month)[1])
        new_comp = Competition(title=title, description=description, month=month, year=year, start=start, end=end)
        new_comp.put()
        self.redirect("/competition/admin")
Ejemplo n.º 2
0
    def post(self, comp_id=0):
        """A user is submitting scores."""
        user_id, user = self.get_user()
        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)

        if not user or not comp:
            # stop some unauthorised post submissions.
            self.redirect("/competitions")
            return

        results = self.parse_scores(self.request.POST)

        for photo_id, score in results.iteritems():
            photo = Photo.get_by_id(photo_id)
            # photo = self.get_photo(photo_id)
            new_score = Scores(photo=photo.key, user_from=user.key, score=score)
            new_score.put()

        # record that user has submitted scores for this comp
        usercomp = self.get_usercomp(user, comp)
        usercomp.submitted_scores = True
        usercomp.put()

        self.redirect("/competition/%d" % (comp_id))
Ejemplo n.º 3
0
    def get(self):
        """Show the competitions page."""
        user_id, user = self.get_user()

        comps = []

        for c in Competition.all():
            month = c.month
            month_word = MONTHS[month]
            user_photo = False
            if user:
                user_photo = Photo.competition_user(c, user) is not None
            comps.append(
                (
                    # month,
                    c.key.id(),
                    month_word,
                    c.year,
                    c.title,
                    c.description,
                    c.get_status(),
                    user_photo,
                )
            )
        data = {"page_title": "Competitions", "user": user, "comps": comps, "months": MONTHS}
        self.render("competitions.html", **data)
Ejemplo n.º 4
0
 def _comp_photo(self, user):
     photo_title = self.request.get('photo-title')
     comp_id = int(self.request.get('comp-id'))
     comp = Competition.get_by_id(comp_id)
     usercomp = UserComp(user=user.key, comp=comp.key)
     usercomp.put()
     return {'competition': comp.key, 'title': photo_title}
Ejemplo n.º 5
0
    def get(self, comp_id=0):
        """Show the competition page."""
        user_id, user = self.get_user()
        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)

        if comp is None:
            self.redirect("/competitions")
            return

        month_str = MONTHS[comp.month]
        data = {
            "user": user,
            "comp": comp,
            "year": comp.year,
            "month": month_str,
            "page_title": comp.title,
            "page_subtitle": comp.get_status(),
            "description": comp.description,
        }

        if comp.status == OPEN:
            self.view_open(user, comp_id, comp, data)
        elif comp.status == SCORING:
            user_comp = self.get_usercomp(user, comp)
            self.view_scoring(user, comp_id, comp, user_comp, data)
        else:  # completed
            self.view_complete(user, comp_id, comp, data)
Ejemplo n.º 6
0
 def _competitions_need_photos(self, user):
     '''Return a list of all competitions for which the user can submit a
     photograph.'''
     submissions = []
     for comp in Competition.get_by_status(OPEN):
         usercomp = UserComp.get_usercomp(user, comp)
         if not usercomp:
             submissions.append(comp)
     return submissions
Ejemplo n.º 7
0
 def _last_positions(self, data):
     '''Update UserStat records for all users who have a photo that was last
     in a competition.'''
     for comp in Competition.get_by_status(COMPLETED):
         photos = list(Photo.competition_photos(comp))
         last_position = max(photos, key=lambda x: x.position).position
         #logging.info('%s: last: %d' % (comp, last_position))
         for photo in filter(lambda x: x.position == last_position, photos):
             user_stat = data[photo.user.id()]
             user_stat.last_place += 1
             user_stat.medals += 1
Ejemplo n.º 8
0
    def get(self, comp_id):
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")
            return

        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)

        data = {"user": user, "page_title": "Delete Competition", "comp_id": comp_id, "comp": comp}
        self.render("competition-delete.html", **data)
Ejemplo n.º 9
0
 def competitions_in_progress(self):
     competition_data = []
     for comp in Competition.in_progress():
         competition_data.append((
             comp.key.id(),
             comp.title,
             comp.description,
             comp.year,
             MONTHS[comp.month],
             comp.get_status()
         ))
     return competition_data
Ejemplo n.º 10
0
def to_competition(json_dict: dict) -> Competition:
    """
    Converts json dictionary into Competition object
    :param json_dict:
    :return:
    """
    if json_dict is None:
        return None

    result = Competition(json_dict['id'], json_dict['caption'],
                         json_dict['year'])

    return result
Ejemplo n.º 11
0
    def get(self):
        """Show the competition admin page."""
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")

        comps = Competition.all()

        data = {
            "page_title": "Competition Admin",
            "user": user,
            "comps": [(c.key.id(), c) for c in comps],
            "months": MONTHS,
        }
        self.render("competitions-admin.html", **data)
Ejemplo n.º 12
0
    def get(self, comp_id):
        # should check for logged in user cookie
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")
            return

        self.response.content_type = "text/csv"

        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)

        logging.info(comp)

        self.write(csv_scores(comp))
Ejemplo n.º 13
0
    def get(self, comp_id):
        "Show the competition modification page for a particular competition."
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")
            return

        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)
        if not comp:
            self.redirect("/competition/admin")
            return

        data = self._data(comp, user)
        self.render("competition-modify.html", **data)
Ejemplo n.º 14
0
    def post(self, comp_id):
        """
        Delete a competition, photographs, comments
        """
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")
            return

        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)
        logging.info("Deleting comp: %s" % comp)

        comp.delete()

        self.redirect("/competition/admin")
Ejemplo n.º 15
0
    def post(self, comp_id):
        """Modify a competition."""
        user_id, user = self.get_user()
        if not user or not user.admin:
            self.redirect("/")
            return

        new_title = self.request.get("comp-title")
        new_description = self.request.get("comp-description")
        new_status = int(self.request.get("comp-status"))
        comp_id = int(comp_id)
        comp = Competition.get_by_id(comp_id)

        if not new_title:
            self.report_error(comp, "Error - blank title.")
            return

        logging.info("updating competition: status %d, new status: %d", comp.status, new_status)

        successful_update, error = self._successful_update(comp, new_status)
        if successful_update:
            self._update_competition(comp, new_title, new_description, new_status)
        else:
            self._report_error(comp, user, error)
Ejemplo n.º 16
0
 def _create_competitions(self):
     comp1 = Competition(
         title='May photographs',
         description='',
         year=2012,
         month=5,
         start=date(2012, 5, 1),
         end=date(2012, 5, 31),
         finished=True,
         status=2
     )
     comp1.put()
     comp2 = Competition(
         title='June photographs',
         description='',
         year=2012,
         month=6,
         start=date(2012, 6, 1),
         end=date(2012, 6, 30),
         finished=False,
         status=1
     )
     comp2.put()
     return (comp1, comp2)
Ejemplo n.º 17
0
def create_competition(name):
    """Create a competition"""
    competition = Competition(name)
    DB.session.add(competition)
    DB.session.commit()
    return jsonify(competition=competition.competitionId)
Ejemplo n.º 18
0
    def get(self):
        logging.info('stats calculator...starting')
        # create a UserStat object for all Users in the db
        users = list(User.query().fetch())
        data = dict(
            (user.key.id(), UserStats(user=user.key))
            for user in users
        )

        for user in users:
            user_stat = data[user.key.id()]
            user_stat.logins = user.login_count
            user_stat.logouts = user.logout_count
            user_stat.bio = 1 if user.bio else 0

        for photo in Photo.query().fetch():
            user_id = photo.user.id()
            user_stat = data[user_id]
            if photo.competition is None:
                user_stat.extra_photos += 1
            else:
                if photo.competition.get().status != COMPLETED:
                    # not interested in competition photos for incomplete
                    # competitions
                    continue
                user_stat.comp_photos += 1
                user_stat.total_points += photo.total_score
                if photo.position == 1:
                    user_stat.first_place += 1
                    user_stat.medals += 1
                elif photo.position == 2:
                    user_stat.second_place += 1
                    user_stat.medals += 1
                elif photo.position == 3:
                    user_stat.third_place += 1
                    user_stat.medals += 1

        completed_comp_count = Competition.count()
        for user_stat in data.values():
            if user_stat.comp_photos == completed_comp_count:
                user_stat.all_comps = 1

        for comment in Comment.query().fetch():
            # give
            data[comment.user.id()].comments_give += 1
            # receive
            receiver = comment.photo.get().user.id()
            data[receiver].comments_receive += 1

        for score in Scores.query().fetch():
            receiver = score.photo.get().user.id()
            if score.score == 10:
                # give 10
                data[score.user_from.id()].score_10_give += 1
                # receive 10
                data[receiver].score_10_receive += 1
            elif score.score == 0:
                # give 0
                data[score.user_from.id()].score_0_give += 1
                # receive 0
                data[receiver].score_0_recieve += 1

        for note in Note.query().fetch():
            data[note.user.id()].notes += 1

        # is this person a GIVER
        for user in data.values():
            if user.comments_give > user.comments_receive:
                user.giver += 1
            if user.score_10_give > user.score_10_receive:
                user.giver += 1

        # last place finishers
        self._last_positions(data)

        self._photo_with_most_comments(data)
        self._photo_with_high_score(data)
        self._houses(data)

        self._most(data, 'comments_give')
        self._most(data, 'comments_receive')
        self._most(data, 'notes')
        self._most(data, 'logins')
        self._most(data, 'logouts')
        self._most(data, 'medals')
        self._most(data, 'first_place')
        self._most(data, 'second_place')
        self._most(data, 'third_place')
        self._most(data, 'last_place')

        UserStats.delete_all()
        for stat in data.values():
            stat.put()

        logging.info(data)
        logging.info('stats calculator...finished')