def random_images(self, number=3): photo_keys = list(Photo.query().fetch(keys_only=True)) shuffle(photo_keys) photos = [] for key in photo_keys: photo = key.get() if photo is None: # on the dev server, after a photo delete, the key is still in # the database when the homepage loads. So need this check to # make sure this is not the recently deleted photo. # TODO: fix this... continue if photo.competition and photo.competition.get().status == COMPLETED: # only view photos belonging to completed competition - title = photo.title if not title: title = 'Untitled' user = photo.user.get().username photos.append((key.id(), photo.url(size=800), title, user)) if len(photos) == number: # Once we have the required number of photos, we can quit the # loop break #logging.info('random photos: %s', photos) return photos
def _delete_all(self): for base in (Competition, UserComp, Scores, Comment): for item in base.query(): item.key.delete() for photo in Photo.query(): delete_blob(photo.blob) photo.key.delete() for user in User.gql('WHERE username != :1', 'test'): user.key.delete()
def response(): if config.memcache.db: clearmem() action = cgi_get("action", choices=["post", "videopost", "comment", "photo", "photoset", "md"]) if action == "md": for dn, dz, fz in os.walk("md"): succeed([f[:-3] for f in fz]) user = cgi_get("user") if action == "comment": ent = Comment(user=user, post=cgi_get("post"), body=cgi_get("body")) elif action == "photo": pkey = cgi_get("key", required=False) if pkey: ent = Photo.query(Photo.key == pkey).get() else: ent = Photo() capt = cgi_get("caption", required=False) # imgs uploaded separately if capt: ent.caption = capt else: blurb = cgi_get("blurb", required=False) pkey = cgi_get("key", required=False) tags = cgi_get("tags", required=False) pmod = db.get_model(action) if pkey: ent = pmod.query(pmod.key == pkey).get() else: ent = pmod(user=user) ent.title = cgi_get("title") ent.live = cgi_get("live") if blurb: ent.blurb = blurb if tags: ent.tags = tags if action == "post": ent.body = cgi_get("body") ent.put() if action == "photo": # get photoset psk = cgi_get("photoset", required=False) if psk: ps = db.get(psk) # these are hacky -- fix list ops in ct if cgi_get("remove", default=False): # ps.photos.remove(ent.key) ps.photos = [p for p in ps.photos if p != ent.key] else: # ps.photos.append(ent.key) ps.photos = ps.photos + [ent.key] ps.put() succeed(ent.id())
def _photo_with_high_score(self, data): '''Find the photograph(s) with the highest score. photo_score / (photos_in_comp - 1) Note: only need to consider first placed photos. ''' results = defaultdict(list) for photo in Photo.query(Photo.position == 1): comp = photo.competition.get() photo_count = comp.users().count() # max score for photo in a competition: 10 * (photo_count - 1) percent_score = photo.total_score / (10.0 * (photo_count - 1)) results[percent_score].append(photo) max_score = max(results.keys()) for photo in results[max_score]: data[photo.user.id()].high_score_photo += 1
def get(self): user_id, user = self.get_user() if not user or not user.admin: self.redirect('/') return photos = list(Photo.query().fetch()) for photo in photos: comment_count = len(list(photo.comments())) photo.comment_count = comment_count photo.put() data = { 'user': user, 'page_title': 'Helps', 'photos': photos, } self.render('help/comments.html', **data)
def get(self, photo_id=0): user_id, user = self.get_user() if not user or not user.admin: self.redirect('/') return photo_id = int(photo_id) if photo_id == 0: photos = list(Photo.query().fetch()) else: photo = Photo.get_by_id(photo_id) if not photo: data = { 'user': user, 'page_title': 'Exif data - no such photo', 'message': 'no photo exists with this id', } self.render('error.html', **data) photos = [photo] results = [] for photo in photos: exif = blob_exif(photo.blob) results.append(( photo, exif, )) photo.populate(**exif) photo.put() data = { 'user': user, 'page_title': 'Exif data extractor', 'photos': results, } self.render('help/exif.html', **data)
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')