Exemple #1
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}
Exemple #2
0
    def _upload_photos(self, users, comps):
        d = os.getcwd()
        d = os.path.join(d, 'test', '*.jpg')
        photos = glob(d)
        titles = ('Mars', 'Finnish Flag', 'Hospital in the distance', '', '', '')
        comp1 = comps[0]

        # collect Photo instances here
        p = []
        all_data = zip(product(users, comps), photos, titles)
        for (user, comp), photo_path, title in all_data:
            file_name = files.blobstore.create(mime_type='image/jpeg')
            with files.open(file_name, 'a') as f:
                f.write(open(photo_path, 'rb').read())
            files.finalize(file_name)
            blob_key = files.blobstore.get_blob_key(file_name)

            photo = Photo(
                user=user.key,
                competition=comp.key,
                blob=blob_key,
                title=title
            )
            photo.put()
            p.append(photo)
            user_comp = UserComp(user=user.key, comp=comp.key)
            if comp == comp1:
                user_comp.submitted_scores = True
            user_comp.put()
        return p
    def _calculate_scores(self, comp):
        """Calculate the scores for a completed competition."""
        all_scores = UserComp.all_scores_submitted(comp)
        if not all_scores:
            return False

        results = []
        for photo in Photo.competition_photos(comp):
            total_score = Scores.photo_score(photo)
            results.append((total_score, photo))
        results.sort(reverse=True)

        # calculate positions
        position = 1
        prev_score = 1000000
        # full_results = []
        for i, (score, photo) in enumerate(results, start=1):
            if score != prev_score:
                position = i
            # full_results.append((position, score, photo))
            photo.position = position
            photo.total_score = score
            photo.put()
            prev_score = score

        return True
Exemple #4
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
 def get_usercomp(self, user, comp):
     """Return UserComp object for user and competition."""
     if user and comp:
         return UserComp.get_usercomp(user, comp)