def addition_values_func(obj, values): user_profile = dao.get_user_profile(obj.user_email) if user_profile: values['favoriter'] = convert.convert_user_profile( user_profile) else: values['favoriter'] = convert.convert_user(obj.user_email)
def get(self, *arg): profile_id = int(arg[0]) user_profile = dao.get_user_profile_by_id(profile_id) if hasattr(user_profile, 'self_block'): self.response.set_status(404) return def artworks_query_func(): all_artworks = db.Favorite.all() all_artworks = all_artworks.filter('user_email =', user_profile.email) return all_artworks.order('-date') def href_create_func(offset): return '/profiles/' + str(profile_id) + '/favorites?offset=' + str( offset) def memcache_cursor_key_func(offset): return cache.MC_ARTWORK_LIST + 'user_favorites_' + str( profile_id) + '_' + str(offset) model = create_gallery_model(self.request.get('offset'), artworks_query_func, href_create_func, memcache_cursor_key_func) model['user_page_title'] = 'Favorites of' model['profile'] = convert.convert_user_profile(user_profile) if self.user_info.user and self.user_info.profile_id == profile_id: model['this_user_profile'] = True self.write_template('templates/user-favorites.html', model)
def create_user_model(offset_param, user_query_func, href_create_func, memcache_cursor_key_func): if offset_param: offset = int(offset_param) else: offset = 0 if offset < 0: offset = 0 fetch_count = users_page_size query = user_query_func() if offset == 0: all_users = query.run(limit=fetch_count + 1) else: cursor = cache.get(memcache_cursor_key_func(offset)) if cursor: query = query.with_cursor(start_cursor=cursor) all_users = query.run(limit=fetch_count + 1) else: all_users = query.run(limit=fetch_count + 1, offset=offset) cache.add(memcache_cursor_key_func(offset), query.cursor()) import logging has_prev_page = (offset > 0) has_next_page = False logging.error('offset={0} has_prev_page={1}'.format(offset, has_prev_page)) index = 0 users = [] for u in all_users: index = index + 1 if index > fetch_count: has_next_page = True else: converted_user = convert.convert_user_profile(u) users.append(converted_user) if index == fetch_count: cache.add(memcache_cursor_key_func(offset + fetch_count), query.cursor()) next_page_href = href_create_func(offset + fetch_count) if offset - users_page_size <= 1: prev_page_href = href_create_func(0) else: prev_page_href = href_create_func(offset - users_page_size) return { 'has_next_page': has_next_page, 'has_prev_page': has_prev_page, 'next_page_href': next_page_href, 'prev_page_href': prev_page_href, 'first_page_href': href_create_func(0), 'users': users }
def get(self, *arg): profile_id = int(arg[0]) user_profile = dao.get_user_profile_by_id(profile_id) if hasattr(user_profile, 'self_block'): self.response.set_status(404) return model = { 'user_page_title': 'Leaders of', 'profile': convert.convert_user_profile(user_profile) } self.write_template('templates/user-leaders.html', model)
def get(self, *args): profile_id = int(args[0]) tag_name = args[1] user = dao.get_user_profile_by_id(profile_id) if not user: self.response.set_status(404) return if hasattr(user, 'self_block'): self.response.set_status(404) return def artworks_query_func(): all_artworks = db.Artwork.all() all_artworks = all_artworks.filter('author_email', user.email) all_artworks = all_artworks.filter('tags =', tags.tag_url_name(tag_name)) return all_artworks.order('-date') def href_create_func(offset): return '/profiles/{}/tags/{}/images?offset={}'.format( profile_id, tag_name, offset) def memcache_cursor_key_func(offset): return cache.MC_ARTWORK_LIST + 'gallery_' + str( profile_id) + '_' + tag_name + '_' + str(offset) model = create_gallery_model(self.request.get('offset'), artworks_query_func, href_create_func, memcache_cursor_key_func) user_tag = db.UserTag.all().filter('user_id', profile_id).filter( 'url_name', tag_name).get() if user_tag: tag_title = user_tag.title model['tag'] = convert.convert_tag_for_page(user_tag) else: tag_title = tag_name model['search_query'] = tag_name model['user_page_title'] = 'Images by tag "{}" of'.format(tag_title) model['profile'] = convert.convert_user_profile(user) self.write_template('templates/user-images-by-tag.html', model)
def get(self, *arg): try: profile_id = int(arg[0]) except ValueError: self.response.set_status(404) return user_profile = dao.get_user_profile_by_id(profile_id) if not user_profile: self.response.set_status(404) return if hasattr(user_profile, 'self_block'): self.response.set_status(404) return def artworks_query_func(): return db.Artwork.all().filter('author_email =', user_profile.email).order('-date') def href_create_func(offset): return '/profiles/' + str(profile_id) + '/images?offset=' + str( offset) def memcache_cursor_key_func(offset): return cache.MC_ARTWORK_LIST + 'profile_' + str( profile_id) + '_' + str(offset) model = create_gallery_model(self.request.get('offset'), artworks_query_func, href_create_func, memcache_cursor_key_func) model['profile'] = convert.convert_user_profile(user_profile) if self.user_info.user: model['following'] = dao.is_follower(user_profile.email, self.user_info.user_email) if self.user_info.user and profile_id == self.user_info.profile_id: model['this_user_profile'] = True model['user_page_title'] = 'Images of' self.write_template('templates/user-images.html', model)
def get(self, *arg): try: profile_id = int(arg[0]) except ValueError: self.response.set_status(404) return user_profile = dao.get_user_profile_by_id(profile_id) if not user_profile: self.response.set_status(404) return recent_db_images = db.Artwork.all().filter( 'author_email', user_profile.email).order('-date').fetch(3, 0) recent_user_images = [ convert.convert_artwork_for_page(a, 200, 150) for a in recent_db_images ] recent_db_group_images = db.ArtworkCollaborator.all().filter( 'user_id =', profile_id).order('-last_date').fetch(3, 0) recent_group_images = [ convert.convert_artwork_for_page(a.artwork, 200, 150) for a in recent_db_group_images ] recent_db_tags = db.UserTag.all().filter( 'user_id', user_profile.key().id()).order('-last_date').fetch(3, 0) recent_user_tags = [ convert.convert_tag_for_page(t) for t in recent_db_tags ] for t in recent_user_tags: t['url'] = '/profiles/{}/tags/{}/images'.format( profile_id, t['url_name']) model = { 'profile': convert.convert_user_profile(user_profile), 'recent_images': recent_user_images, 'has_any_recent_images': len(recent_user_images) > 0, 'has_more_recent_images': len(recent_user_images) >= 3, 'group_images': recent_group_images, 'has_any_group_images': len(recent_group_images) > 0, 'has_more_group_images': len(recent_group_images) >= 3, 'recent_tags': recent_user_tags, 'has_any_recent_tags': len(recent_user_tags) > 0, 'has_more_recent_tags': len(recent_user_tags) >= 3, } if self.user_info.user: model['following'] = dao.is_follower(user_profile.email, self.user_info.user_email) if self.user_info.superadmin: model['profile']['email'] = user_profile.email model['profile']['alternative_emails'] = getattr( user_profile, 'alternative_emails', []) if self.user_info.user and profile_id == self.user_info.profile_id: model['this_user_profile'] = True self.write_template('templates/profile.html', model)
def get(self, *arg): artwork_id = arg[0] artwork = dao.get_artwork(artwork_id) if not artwork: self.response.set_status(404) return favorite_count = dao.get_artwork_favorite_count(artwork) favorite = dao.is_artwork_favorite_by_user(artwork, self.user_info.user_email) db_comments = db.Comment.all().filter( 'artwork_ref =', artwork).order('-date').fetch(const.MAX_COMMENT_PACK + 1) all_db_comments = [ convert.convert_comment_for_page(c) for c in db_comments ] comments = all_db_comments[:const.MAX_COMMENT_PACK] has_more_comments = len(all_db_comments) > const.MAX_COMMENT_PACK converted_artwork = convert.convert_artwork_for_page(artwork, 600, 400) author_user_id = converted_artwork.get('author', {}).get('profile_id') if 'tags' in converted_artwork: converted_artwork['tags_merged'] = ','.join([ tags.tag_by_url_name(t.title).title for t in converted_artwork['tags'] ]) if 'self_block' in converted_artwork['author']: self.write_template('templates/artwork-details-blocked.html', {'artwork': converted_artwork}) return user_is_author = artwork.author_email == self.user_info.user_email db_collaborators = db.ArtworkCollaborator.all().filter( 'artwork =', artwork).order('-last_date') collaborators = [ convert.convert_user_profile(dao.get_user_profile_by_id(c.user_id)) for c in db_collaborators if c.user_id != author_user_id ] user_is_collaborator = False if self.user_info.user: user_is_collaborator = self.user_info.profile_id in [ c['profile_id'] for c in collaborators ] can_edit_artwork = self.user_info.superadmin or user_is_author or user_is_collaborator if ('block' in converted_artwork or 'copyright_block' in converted_artwork) and not can_edit_artwork: self.write_template('templates/artwork-details-blocked.html', {'artwork': converted_artwork}) return if self.user_info.user: following = dao.is_follower(artwork.author_email, self.user_info.user_email) else: following = None settings = get_settings() template_name = 'templates/artwork-details.html' if 'block' in converted_artwork or 'copyright_block' in converted_artwork: template_name = 'templates/artwork-details-limited.html' self.write_template( template_name, { 'artwork': converted_artwork, 'collaborators': collaborators, 'can_edit_artwork': can_edit_artwork, 'user_is_author': user_is_author, 'user_is_collaborator': user_is_collaborator, 'comments_offset': len(comments), 'comments': comments, 'has_more_comments': has_more_comments, 'favorite_count': favorite_count, 'favorite': favorite, 'following': following, 'og_title': converted_artwork['name'], 'og_image': 'https://grid-paint.com/images/png/' + artwork_id + '.png', 'og_image_width': converted_artwork['full_image_width'], 'og_image_height': converted_artwork['full_image_height'], 'og_url': 'https://grid-paint.com/images/details/' + artwork_id, 'og_description': u'Created by {} in Grid Paint'.format( converted_artwork['author']['nickname']), 'settings': settings })
def get(self): newsfeed = None if self.user_info.user: newsfeed_artworks = db.NewsFeed.all().filter( 'user_email =', self.user_info.user_email).order('-date').fetch(7) newsfeed = [ convert.convert_artwork_for_page(a, 200, 150) for a in newsfeed_artworks ] if 0 < len(newsfeed) < 3: for i in range(len(newsfeed), 3): newsfeed.append({'not_found': True}) elif len(newsfeed) < 7: newsfeed = newsfeed[0:3] recent_artworks = cache.get(cache.MC_MAIN_PAGE_RECENT_IMAGES_KEY) if not recent_artworks: all_artworks = db.Artwork.all() all_artworks = all_artworks.order('-date').fetch(3, 0) recent_artworks = [ convert.convert_artwork_for_page(a, 200, 150) for a in all_artworks ] cache.add(cache.MC_MAIN_PAGE_RECENT_IMAGES_KEY, recent_artworks) editor_choice = cache.get(cache.MC_MAIN_PAGE_RECENT_EDITOR_CHOICE) if not editor_choice: choice_artworks = db.Artwork.all().filter('editor_choice =', True) choice_artworks = choice_artworks.order( '-editor_choice_date').fetch(3, 0) editor_choice = [ convert.convert_artwork_for_page(a, 200, 150) for a in choice_artworks ] cache.add(cache.MC_MAIN_PAGE_RECENT_EDITOR_CHOICE, editor_choice) top_favorites = cache.get(cache.MC_MAIN_PAGE_TOP_FAVORITES) if not top_favorites: top_favorite_artworks = db.FavoriteCounter.all().order( '-count').order('-date') top_favorite_artworks = top_favorite_artworks.fetch(3, 0) top_favorites = [ convert.convert_artwork_for_page(a, 200, 150) for a in top_favorite_artworks ] cache.add(cache.MC_MAIN_PAGE_TOP_FAVORITES, top_favorites) recent_favorites = cache.get(cache.MC_MAIN_PAGE_RECENT_FAVORITES) if not recent_favorites: recent_favorites_artworks = db.Favorite.all().order('-date') recent_favorites_artworks = recent_favorites_artworks.fetch(3, 0) recent_favorites = [ convert.convert_artwork_for_page(a, 200, 150) for a in recent_favorites_artworks ] cache.add(cache.MC_MAIN_PAGE_RECENT_FAVORITES, recent_favorites) recent_comments = cache.get(cache.MC_MAIN_PAGE_RECENT_COMMENTS) if not recent_comments: comments = db.Comment.all().order('-date').fetch(5, 0) recent_comments = [ convert.convert_comment_for_page_rich(c) for c in comments ] cache.add(cache.MC_MAIN_PAGE_RECENT_COMMENTS, recent_comments) productive_artists = cache.get(cache.MC_MAIN_PAGE_PRODUCTIVE_ARTISTS) if not productive_artists: p_artists = db.UserProfile.all().order('-artworks_count').fetch(5) productive_artists = [ convert.convert_user_profile(a) for a in p_artists ] cache.add(cache.MC_MAIN_PAGE_PRODUCTIVE_ARTISTS, productive_artists) top_rated_artists = cache.get(cache.MC_MAIN_PAGE_TOP_RATED_ARTISTS) if not top_rated_artists: r_artists = db.UserProfile.all().order('-favorite_count').fetch(5) top_rated_artists = [ convert.convert_user_profile(a) for a in r_artists ] cache.add(cache.MC_MAIN_PAGE_TOP_RATED_ARTISTS, top_rated_artists) last_week_favorites = cache.get(cache.MC_MAIN_PAGE_LAST_WEEK_FAVORITES) if not last_week_favorites: last_week_favorites = db.LastWeekFavoriteCounters.all().order( '-count') last_week_favorites = last_week_favorites.fetch(3, 0) last_week_favorites = [ convert.convert_artwork_for_page(a, 200, 150) for a in last_week_favorites ] cache.add(cache.MC_MAIN_PAGE_LAST_WEEK_FAVORITES, last_week_favorites) last_month_favorites = cache.get( cache.MC_MAIN_PAGE_LAST_MONTH_FAVORITES) if not last_month_favorites: last_month_favorites = db.LastMonthFavoriteCounters.all().order( '-count') last_month_favorites = last_month_favorites.fetch(3, 0) last_month_favorites = [ convert.convert_artwork_for_page(a, 200, 150) for a in last_month_favorites ] cache.add(cache.MC_MAIN_PAGE_LAST_MONTH_FAVORITES, last_month_favorites) self.write_template( 'templates/index.html', { 'artworks': recent_artworks, 'editor_choice': editor_choice, 'top_favorites': top_favorites, 'last_week_favorites': last_week_favorites, 'last_month_favorites': last_month_favorites, 'recent_favorites': recent_favorites, 'comments': recent_comments, 'productive_artists': productive_artists, 'top_rated_artists': top_rated_artists, 'newsfeed': newsfeed, 'og_title': 'Grid Paint', 'og_description': 'An online tool for pixel drawing with different shapes of pixels.', 'og_url': 'https://grid-paint.com', 'og_image': 'https://grid-paint.com/img/grid-paint-poster.png' })
def get(self, *args): profile_id = int(args[0]) if self.request.get('offset'): offset = int(self.request.get('offset')) else: offset = 0 user_profile = dao.get_user_profile_by_id(profile_id) if not user_profile: self.response.set_status(404) return if hasattr(user_profile, 'self_block'): self.response.set_status(404) return limit = 11 if offset == 0 else 10 fetched_tags = db.UserTag.all().filter( 'user_id', profile_id).order('-last_date').fetch(limit + 1, offset) query_tags = [convert.convert_tag_for_page(t) for t in fetched_tags] for t in query_tags: t['url'] = '/profiles/{}/tags/{}/images'.format( profile_id, t['url_name']) if offset == 11: prev_offset = 0 else: prev_offset = offset - limit if prev_offset < 0: prev_offset = 0 has_prev_page = offset > 0 if len(query_tags) > limit: query_tags = query_tags[:-1] next_offset = offset + limit has_next_page = True else: next_offset = -1 has_next_page = False self.write_template( 'templates/user-tags.html', { 'user_page_title': 'Tags of', 'profile': convert.convert_user_profile(user_profile), 'tags': query_tags, 'limit': limit, 'offset': offset, 'next_offset': next_offset, 'prev_offset': prev_offset, 'has_next_page': has_next_page, 'has_prev_page': has_prev_page, 'next_page_href': '/profiles/{}/tags?offset={}'.format(profile_id, next_offset), 'prev_page_href': '/profiles/{}/tags?offset={}'.format(profile_id, prev_offset) })