def add_problem_i18n_name(self, key, language, name_field=None): queryset = self._clone() if name_field is None else self.annotate(_name=F(name_field)) alias = unique_together_left_join(queryset, ProblemTranslation, 'problem', 'language', language, parent_model=Problem) # You must specify name_field if Problem is not yet joined into the QuerySet. kwargs = {key: Coalesce(RawSQL('%s.name' % alias, ()), F(name_field) if name_field else RawSQLColumn(Problem, 'name'), output_field=models.CharField())} return queryset.annotate(**kwargs)
def get_context_data(self, **kwargs): context = super(CommentedDetailView, self).get_context_data(**kwargs) queryset = Comment.objects.filter(page=self.get_comment_page()) context['has_comments'] = queryset.exists() queryset = queryset.select_related('author__user').defer('author__about').annotate(revisions=Count('versions')) if self.request.user.is_authenticated: queryset = queryset.annotate(vote_score=Coalesce(RawSQLColumn(CommentVote, 'score'), Value(0))) profile = self.request.user.profile unique_together_left_join(queryset, CommentVote, 'comment', 'voter', profile.id) context['is_new_user'] = (not self.request.user.is_staff and not profile.submission_set.filter(points=F('problem__points')).exists()) context['comment_list'] = queryset return context
def get_context_data(self, **kwargs): context = super(CommentedDetailView, self).get_context_data(**kwargs) queryset = Comment.objects.filter(hidden=False, page=self.get_comment_page()) context['has_comments'] = queryset.exists() context['comment_lock'] = self.is_comment_locked() queryset = queryset.select_related('author__user').defer( 'author__about').annotate(revisions=Count('versions')) if self.request.user.is_authenticated: queryset = queryset.annotate(vote_score=Coalesce( RawSQLColumn(CommentVote, 'score'), Value(0))) profile = self.request.profile unique_together_left_join(queryset, CommentVote, 'comment', 'voter', profile.id) context[ 'is_new_user'] = not self.request.user.is_staff and not profile.has_any_solves context['comment_list'] = queryset context[ 'vote_hide_threshold'] = settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD return context
def get_context_data(self, **kwargs): context = super(CommentedDetailView, self).get_context_data(**kwargs) queryset = Comment.objects.filter(page=self.get_comment_page()) context['has_comments'] = queryset.exists() queryset = queryset.select_related('author__user').defer( 'author__about').annotate(revisions=Count('versions')) # This version uses public Django interface, but it requires support on the model. #if self.request.user.is_authenticated(): # votes = CommentVote.objects.filter(voter=self.request.user.profile) #else: # votes = CommentVote.objects.none() #context['comment_list'] = queryset.prefetch_related(Prefetch('votes', queryset=votes)) # This version digs into django internals. if self.request.user.is_authenticated(): queryset = queryset.annotate(vote_score=Coalesce( RawSQLColumn(CommentVote, 'score'), Value(0))) unique_together_left_join(queryset, CommentVote, 'comment', 'voter', self.request.user.profile.id) context['comment_list'] = queryset return context