예제 #1
0
    def get(self):
        query = Rating.query(ancestor=ndb.Key('Rating', ANNOTATION_NAME))
        
        counts = defaultdict(int)
        for rating in query.fetch():
            counts[rating.user] += 1

        values = {"counts": sorted(counts.iteritems(), key=lambda x: x[1], reverse=True)}
        self.template_response('leaderboard.html', values)
예제 #2
0
        def post(self):
                user = users.get_current_user()
                movie = self.request.get('movietitle')
                rating = self.request.get('rating')
                ratingBefore = 0

                myresponse = {}

                query = Rating.query(Rating.movietitle == movie, Rating.username == user)
                currentRating = None

                for q in query:
                    currentRating = q

                if currentRating == None:
                    currentRating = Rating(username = user, movietitle = movie, movierating = int(rating))
                else:
                    ratingBefore = currentRating.movierating
                    currentRating.movierating = int(rating)

                query = UserRecord.query(UserRecord.username == user)
                userRecord = None

                for q in query:
                    userRecord = q

                if userRecord == None:
                    userRecord = UserRecord(username = user, totalratings = int(rating), numofratings = 1, averagerating = float(rating))
                else:
                    userRecord.totalratings = userRecord.totalratings - ratingBefore + int(rating)
                    if ratingBefore == 0:
                        userRecord.numofratings += 1

                    userRecord.averagerating = float(userRecord.totalratings) / float(userRecord.numofratings)

                logging.debug("Rating: " + rating)
                logging.debug("Username: "******"Movie: " + movie)
                currentRating.put()
                logging.debug("Total Ratings: " + str(userRecord.totalratings))
                logging.debug("Number of Ratings: " + str(userRecord.numofratings))
                logging.debug("Average Rating: " + str(userRecord.averagerating))
                userRecord.put()

                self.response.out.write(json.dumps(myresponse))
예제 #3
0
 def __call__(self, user=None):
     (query, count) = query_content(self.ancestor)
     choices = range(count)
     while count > 0 and len(choices):
         choice = random.choice(choices)
         content = query.fetch(offset=choice, limit=1)[0]
         filters = Rating.content == content.key
         if user:
             filters = ndb.AND(Rating.user == user, filters)
             
         if self.unrated and \
             Rating.query(filters, \
                          ancestor=ndb.Key('Rating', self.ancestor)).count():
             choices.remove(choice)
         else:
             return content
     else:
         return None
예제 #4
0
    def __call__(self, user=None):
        query = Content.query(ancestor=ndb.Key('Content', self.ancestor))
        count = query.count()
        invalid_choices = memcache.get("invalid_content:" + user.email()) or []
        choices = filter(lambda choice: choice not in invalid_choices, range(count))
        while count > 0 and len(choices):
            choice = random.choice(choices)
            content = query.fetch(offset=choice, limit=1)[0]

            if self.unrated or self.sample_subcontent:
                subquery = SubContent.query(ancestor=content.key)
                subcontent_keys = subquery.fetch(keys_only=True)

            if self.unrated:
                if len(subcontent_keys) == 0:
                    filters = Rating.content == content.key
                else:
                    filters = Rating.content.IN(subcontent_keys)
                if user:
                    filters = ndb.AND(Rating.user == user, filters)
                ratings = Rating.query(filters, \
                              ancestor=ndb.Key('Rating', self.ancestor)).count()

                invalid  = self.sample_subcontent and ratings > 0
                invalid |= len(subcontent_keys) == 0 and ratings == 1
                invalid |= len(subcontent_keys) > 0 and ratings == len(subcontent_keys)
                if invalid:
                    choices.remove(choice)
                    invalid_choices.append(choice)
                    memcache.set("invalid_content:" + user.email(), invalid_choices)
                    continue
            
            if len(subcontent_keys) and self.sample_subcontent:
                content = random.choice(subcontent_keys).get()

            return content
        else:
            return None
예제 #5
0
        def get(self):
                user = users.get_current_user()
                title = self.request.get('movietitle')
                movieQuery = Movie.query(Movie.movietitle == title)
                ratingQuery = Rating.query(Rating.movietitle == title, Rating.username == user)
                currentRating = 0

                for q in ratingQuery:
                    currentRating = q.movierating

                if user:
                        template_values = {'movies' : movieQuery, 'rating' : currentRating}
                        header_values = {'logout' : users.create_logout_url('/')}


                        template = JINJA_ENVIRONMENT.get_template('header.html')
                        self.response.write(template.render(header_values))         
                        template = JINJA_ENVIRONMENT.get_template('movie.html')
                        self.response.write(template.render(template_values))
                        template = JINJA_ENVIRONMENT.get_template('footer.html')
                        self.response.write(template.render())
                else:
                    self.redirect(users.create_login_url(self.request.uri))
예제 #6
0
    def get_contents(self, get_subcontents=True, get_ratings=False, get_ratings_count=True):
        contents = Content.query(ancestor=ndb.Key('Content', ANNOTATION_NAME)) \
                          .order(Content.date).fetch()

        subcontents = defaultdict(list)
        if get_subcontents:
            query = SubContent.query(ancestor=ndb.Key('Content', ANNOTATION_NAME))
            for subcontent in query.fetch():
                subcontents[subcontent.key.parent().urlsafe()] += subcontent,
                
        ratings = defaultdict(list)
        ratings_counts = defaultdict(int)
        if get_ratings or get_ratings_count:
            query = Rating.query(ancestor=ndb.Key('Rating', ANNOTATION_NAME))
        
            if get_ratings:
                for rating in query.fetch():
                    ratings[rating.content.urlsafe()].append(rating)            

            if get_ratings_count:
                for rating in query.fetch(projection=['content']):
                    ratings_counts[rating.content.urlsafe()] += 1

        return contents, subcontents, ratings, ratings_counts