def _make_activities(self, activity_ids, earlier_than=None, later_than=None): from apps.activity.models import Activity, LegacyActivity def filter_by_ts(query): if earlier_than is not None: query = query.filter(timestamp__lt=earlier_than) if later_than is not None: query = query.filter(timestamp__gt=later_than) return query activity_ids = [int(id_) for id_ in activity_ids] activities = Activity.objects.filter(id__in=activity_ids).order_by('-timestamp') activities = filter_by_ts(activities) activities = CachedCall.queryset_details(activities) if len(activities) < len(activity_ids): legacy_ids = set(activity_ids) - set(int(activity['id']) for activity in activities) legacy_activities = LegacyActivity.objects.filter(id__in=legacy_ids).order_by('-timestamp') legacy_activities = filter_by_ts(legacy_activities) legacy_activities = CachedCall.queryset_details(legacy_activities) activities.extend(legacy_activities) ret = [] for activity_data in activities: try: ret.append(self._activity_types[activity_data['activity_type']](activity_data)) except KeyError as e: continue return ret
def search_stamps(request, query, start): """ Searches the special "stamps" group for stamps that match the search query. Returns {comments: [list of comment details]} """ qs = query try: start = int(start) except TypeError: raise ServiceError('Invalid "start" parameter.') stamps = models.Category.objects.get(name="stamps") if qs: ids = [x for x in models.Comment.objects.filter(category=stamps).filter( Q(reply_text__icontains=qs) | Q(title__icontains=qs) ).values_list('id', flat=True)] ids = ids[start:start+32] comments = models.Comment.curated.exclude(reply_content__id=None).in_bulk(ids) details = CachedCall.multicall([comments[id].details for id in ids if id in comments]) else: comments = models.Comment.curated.filter(category=stamps).exclude(reply_content__id=None).order_by('-id') comments = comments[start:start+32] details = CachedCall.queryset_details(comments) return {'comments': details}
def gallery_comments(quest, offset='top', direction='next', force_comment=None, viewer=None, include_reactions=True): """ Returns comments, pagination. Each comment is itself a dict. """ if force_comment is not None: newer_comments = QuestComment.objects.filter(parent_comment=quest, id__gt=force_comment.id).order_by('id').values_list('id', flat=True) try: offset = list(newer_comments[:knobs.COMMENTS_PER_PAGE / 2])[-1] except IndexError: offset = force_comment.id pagination = Paginator(_exclude_flagged(_all_gallery_comments(quest), viewer), knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction) comments = pagination.items promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if force_comment is not None and force_comment.id not in [cmt['id'] for cmt in comments]: if force_comment.visibility != Visibility.CURATED: raise Http404() comments.append(force_comment.details()) comments = sorted(comments, key=lambda cmt: -cmt['id']) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments, pagination
def search_stamps(request, query, start): """ Searches the special "stamps" group for stamps that match the search query. Returns {comments: [list of comment details]} """ qs = query try: start = int(start) except TypeError: raise ServiceError('Invalid "start" parameter.') stamps = models.Category.objects.get(name="stamps") if qs: ids = [ x for x in models.Comment.objects.filter( category=stamps).filter(Q( title__icontains=qs)).values_list('id', flat=True) ] ids = ids[start:start + 32] comments = models.Comment.curated.exclude( reply_content__id=None).in_bulk(ids) details = CachedCall.multicall( [comments[id].details for id in ids if id in comments]) else: comments = models.Comment.curated.filter(category=stamps).exclude( reply_content__id=None).order_by('-id') comments = comments[start:start + 32] details = CachedCall.queryset_details(comments) return {'comments': details}
def user_comments(user, viewer, offset='top', include_ugq=True, include_reactions=True): comments = QuestComment.by_author(user) if not include_ugq: comments = comments.filter(parent_comment__ugq=False) if viewer.id != user.id: comments = comments.exclude(visibility=Visibility.CURATED) if viewer.is_authenticated(): comments = comments.exclude(flags__user=viewer) pagination = Paginator(comments, knobs.COMMENTS_PER_PAGE, offset=offset) comments = pagination.items promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) return comments, pagination
def test_queryset_details(self): comments = [create_comment(reply_content=create_content()) for _ in xrange(10)] details1 = CachedCall.multicall([cmt.details for cmt in comments]) queryset = Comment.objects.filter(id__in=[cmt.id for cmt in comments]) details2 = CachedCall.queryset_details(queryset) self.assertEquals(details1, details2)
def test_queryset_details(self): comments = [ create_comment(reply_content=create_content()) for _ in xrange(10) ] details1 = CachedCall.multicall([cmt.details for cmt in comments]) queryset = Comment.objects.filter(id__in=[cmt.id for cmt in comments]) details2 = CachedCall.queryset_details(queryset) self.assertEquals(details1, details2)
def staff_pick_stamps(request, page): page = int(page) page_size = 20 b36_ids = knobs.REMIX_IMAGES_STAFF_PICKS[page*page_size:(page+1)*page_size] ids = [util.base36decode(b36_id) for b36_id in b36_ids] details = CachedCall.queryset_details(Comment.objects.in_bulk_list(ids)) return {'comments': details}
def _make_activities(self, activity_ids, earlier_than=None, later_than=None): from apps.activity.models import Activity, LegacyActivity def filter_by_ts(query): if earlier_than is not None: query = query.filter(timestamp__lt=earlier_than) if later_than is not None: query = query.filter(timestamp__gt=later_than) return query activity_ids = [int(id_) for id_ in activity_ids] activities = Activity.objects.filter( id__in=activity_ids).order_by('-timestamp') activities = filter_by_ts(activities) activities = CachedCall.queryset_details(activities) if len(activities) < len(activity_ids): legacy_ids = set(activity_ids) - set( int(activity['id']) for activity in activities) legacy_activities = LegacyActivity.objects.filter( id__in=legacy_ids).order_by('-timestamp') legacy_activities = filter_by_ts(legacy_activities) legacy_activities = CachedCall.queryset_details(legacy_activities) activities.extend(legacy_activities) ret = [] for activity_data in activities: try: ret.append(self._activity_types[activity_data['activity_type']] (activity_data)) except KeyError as e: continue return ret
def staff_pick_stamps(request, page): page = int(page) page_size = 20 b36_ids = knobs.REMIX_IMAGES_STAFF_PICKS[page * page_size:(page + 1) * page_size] ids = [util.base36decode(b36_id) for b36_id in b36_ids] details = CachedCall.queryset_details(Comment.objects.in_bulk_list(ids)) return {'comments': details}
def ugq_by_user(user, offset='top', direction='next', viewer=None): quests = Quest.objects.filter(author=user, ugq=True) if viewer is not None and viewer.is_authenticated(): quests = quests.exclude(flags__user=viewer) quests = quests.order_by('-id') pagination = Paginator(quests, knobs.QUESTS_PER_PAGE, offset=offset, direction=direction) quests = pagination.items quests = CachedCall.queryset_details(quests) return quests, pagination
def user_comments(request, username, page='top'): user = get_object_or_404(User, username=username) comments = QuestComment.by_author(user) if request.user.id != user.id: comments = comments.exclude(visibility=Visibility.CURATED) comments = comments[:knobs.COMMENTS_PER_PAGE] comments = CachedCall.queryset_details(comments) return { 'comments': comments, }
def gallery_comments(quest, offset='top', direction='next', force_comment=None, viewer=None, include_reactions=True): """ Returns comments, pagination. Each comment is itself a dict. """ if force_comment is not None: newer_comments = QuestComment.objects.filter( parent_comment=quest, id__gt=force_comment.id).order_by('id').values_list('id', flat=True) try: offset = list(newer_comments[:knobs.COMMENTS_PER_PAGE / 2])[-1] except IndexError: offset = force_comment.id pagination = Paginator(_exclude_flagged(_all_gallery_comments(quest), viewer), knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction) comments = pagination.items promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if force_comment is not None and force_comment.id not in [ cmt['id'] for cmt in comments ]: if force_comment.visibility != Visibility.CURATED: raise Http404() comments.append(force_comment.details()) comments = sorted(comments, key=lambda cmt: -cmt['id']) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments, pagination
def ugq_by_user(user, offset='top', direction='next', viewer=None): quests = Quest.objects.filter(author=user, ugq=True) if viewer is not None and viewer.is_authenticated(): quests = quests.exclude(flags__user=viewer) quests = quests.order_by('-id') pagination = Paginator(quests, knobs.QUESTS_PER_PAGE, offset=offset, direction=direction) quests = pagination.items quests = CachedCall.queryset_details(quests) return quests, pagination
def top_gallery_comments(quest, viewer=None, include_reactions=False): comment_ids = top_gallery_comment_ids(quest) comments = QuestComment.objects.filter(id__in=comment_ids).order_by('-star_count') comments = _exclude_flagged(comments, viewer) promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments
def _profile(request, user, template='profiles/profile.html'): comments = QuestComment.by_author(user) top_comments = models.top_comments(user) if top_comments is None: comments = CachedCall.queryset_details(comments) else: comments, top_comments = CachedCall.many_queryset_details(comments, top_comments) follow_counts = following_models.counts(user) return r2r_jinja(template, { 'target_user': user, 'comments': comments, 'top_comments': top_comments, 'follower_count': follow_counts['followers'], 'following_count': follow_counts['following'], }, request)
def top_gallery_comments(quest, viewer=None, include_reactions=False): comment_ids = top_gallery_comment_ids(quest) comments = QuestComment.objects.filter( id__in=comment_ids).order_by('-star_count') comments = _exclude_flagged(comments, viewer) promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments
def _profile(request, user, template='profiles/profile.html'): comments = QuestComment.by_author(user) top_comments = models.top_comments(user) if top_comments is None: comments = CachedCall.queryset_details(comments) else: comments, top_comments = CachedCall.many_queryset_details( comments, top_comments) follow_counts = following_models.counts(user) return r2r_jinja( template, { 'target_user': user, 'comments': comments, 'top_comments': top_comments, 'follower_count': follow_counts['followers'], 'following_count': follow_counts['following'], }, request)
def user_comments(user, viewer, offset="top", include_ugq=True, include_reactions=True): comments = QuestComment.by_author(user) if not include_ugq: comments = comments.filter(parent_comment__ugq=False) if viewer.id != user.id: comments = comments.exclude(visibility=Visibility.CURATED) if viewer.is_authenticated(): comments = comments.exclude(flags__user=viewer) pagination = Paginator(comments, knobs.COMMENTS_PER_PAGE, offset=offset) comments = pagination.items promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) return comments, pagination
def quest_comments(request, quest_id, force_comment_id=None): quest = get_object_or_404(Quest, id=quest_id) # Exclude curated comments here since we ignore curation status for forced comments, below. comments = QuestComment.objects.filter(parent_comment=quest).exclude(visibility=Visibility.CURATED) comments = comments.order_by('-id') comments = comments[:knobs.COMMENTS_PER_PAGE] comments = CachedCall.queryset_details(comments) forced_comment = None if force_comment_id: for comment in comments: if str(comment.id) == str(force_comment_id): forced_comment = comment break if force_comment_id and forced_comment is None: forced_comment = QuestComment.details_by_id(force_comment_id)() if forced_comment is not None and str(forced_comment.id) not in [str(comment.id) for comment in comments]: comments.append(forced_comment) return {'comments': comments}
def from_ids(cls, comment_ids): from drawquest.apps.quest_comments.models import QuestComment return CachedCall.queryset_details( QuestComment.objects.in_bulk_list(comment_ids), promoter=cls)
def from_ids(cls, quest_ids): from drawquest.apps.quests.models import Quest return CachedCall.queryset_details(Quest.objects.in_bulk_list(quest_ids))
def from_ids(cls, quest_ids): from drawquest.apps.quests.models import Quest return CachedCall.queryset_details( Quest.objects.in_bulk_list(quest_ids))
def from_ids(cls, comment_ids): from drawquest.apps.quest_comments.models import QuestComment return CachedCall.queryset_details(QuestComment.objects.in_bulk_list(comment_ids), promoter=cls)