def home_view(request): if request.user.is_authenticated: ctx = {'solved': get_accept_problem_count(request.user.pk), 'bulletin': site_settings_get('BULLETIN', ''), 'global_rating': User.objects.filter(rating__gt=0).order_by("-rating")[:10], 'update_log': UpdateLog.objects.all().order_by("-pk")[:10], 'submission_count': Submission.objects.all().count() } if not is_site_closed(request): LIMIT, LIMIT_BLOG = 20, 15 ctx['blog_list'] = Blog.objects.with_likes().with_likes_flag(request.user).select_related( "author").order_by("-create_time").filter(visible=True, recommend=True)[:LIMIT_BLOG] comment_list, blog_list = XtdComment.objects.filter(is_public=True, is_removed=False).order_by( "-submit_date").select_related("user", "content_type").prefetch_related('content_object').all()[:LIMIT], \ Blog.objects.order_by("-create_time").select_related("author").filter( visible=True)[:LIMIT] ctx['comment_list'] = [] i, j = 0, 0 for k in range(LIMIT): if i < len(comment_list) and (j == len(blog_list) or ( j < len(blog_list) and comment_list[i].submit_date > blog_list[j].create_time)): ctx['comment_list'].append(comment_list[i]) i += 1 elif j < len(blog_list): ctx['comment_list'].append(blog_list[j]) j += 1 else: break for comment in ctx['comment_list']: if isinstance(comment, XtdComment) and len(comment.comment) > LIMIT: comment.comment = comment.comment[:LIMIT] + '...' return render(request, 'home_logged_in.jinja2', context=ctx) else: return render(request, 'home.jinja2', context={'bg': '/static/image/bg/%d.jpg' % randint(1, 14), })
def process_view(request, view_func, view_args, view_kwargs): force_closed = view_kwargs.pop('force_closed', False) if force_closed and is_site_closed() and not is_admin_or_root( request.user): return render(request, 'error/closed.jinja2') else: return view_func(request, *view_args, **view_kwargs)
def dispatch(self, request, *args, **kwargs): self.contest = get_object_or_404(Contest, pk=kwargs.get('cid')) self.site_closed = is_site_closed(request) if self.site_closed: if self.contest.always_running: raise CloseSiteException if not self.contest.start_time - timedelta(minutes=30) <= timezone.now() \ <= self.contest.end_time + timedelta(minutes=10): raise CloseSiteException if self.contest.access_level >= 30: raise CloseSiteException self.user = request.user self.privileged = is_contest_manager(self.user, self.contest) self.volunteer = is_contest_volunteer(self.user, self.contest) self.registered = False if self.user.is_authenticated: try: participant = self.contest.contestparticipant_set.get(user=self.user) if self.contest.ip_sensitive: current_ip = get_ip(request) if participant.ip_address is None: participant.ip_address = current_ip participant.save(update_fields=['ip_address']) self.registered = current_ip == participant.ip_address else: self.registered = True except ContestParticipant.DoesNotExist: pass if not self.registered and (self.contest.access_level >= 30 or (self.contest.access_level >= 20 and self.contest.status > 0)): self.registered = True return super(BaseContestMixin, self).dispatch(request, *args, **kwargs)
def process_view(request, view_func, view_args, view_kwargs): force_closed = view_kwargs.pop('force_closed', False) try: if force_closed and is_site_closed(request): raise CloseSiteException return view_func(request, *view_args, **view_kwargs) except CloseSiteException: return render(request, 'error/closed.jinja2')
def dispatch(self, request, *args, **kwargs): self.contest = get_object_or_404(Contest, pk=kwargs.get('cid')) self.site_closed = is_site_closed(request) if self.site_closed: if self.contest.contest_type == 1: raise CloseSiteException if not self.contest.start_time - timedelta(minutes=30) <= timezone.now() \ <= self.contest.end_time + timedelta(minutes=10): raise CloseSiteException if self.contest.access_level >= 30: raise CloseSiteException self.user = request.user self.privileged = is_contest_manager(self.user, self.contest) self.volunteer = is_contest_volunteer(self.user, self.contest) self.registered, self.vp_available = False, False self.virtual_progress, self.participant = None, None # virtual participation undergoing self.participate_start_time = self.contest.start_time # the start time for the participant self.participate_end_time = self.contest.end_time # the end time for the participant self.participate_contest_status = self.contest.status # the contest status for the participant if self.user.is_authenticated: try: self.participant = self.contest.contestparticipant_set.get( user=self.user) self.participate_start_time = self.participant.start_time( self.contest) self.participate_end_time = self.participant.end_time( self.contest) self.participate_contest_status = self.participant.status( self.contest) if self.participant.join_time is not None and self.participate_contest_status == 0: self.virtual_progress = datetime.now( ) - self.participate_start_time if self.contest.ip_sensitive: current_ip = get_ip(request) if self.participant.ip_address is None: self.participant.ip_address = current_ip self.participant.save(update_fields=['ip_address']) self.registered = current_ip == self.participant.ip_address else: self.registered = True except ContestParticipant.DoesNotExist: pass if not self.registered and (self.contest.access_level >= 30 or (self.contest.access_level >= 20 and self.contest.status > 0)): self.registered = True if self.participant is None and self.user.is_authenticated and self.contest.access_level >= 15 and \ self.contest.contest_type == 0 and self.contest.status > 0: self.vp_available = True return super(BaseContestMixin, self).dispatch(request, *args, **kwargs)
def home_view(request): if request.user.is_authenticated: ctx = { 'solved': get_accept_problem_count(request.user.pk), 'bulletin': site_settings_get('BULLETIN', '') } if not is_site_closed(): ctx['blog_list'] = Blog.objects.with_likes().with_likes_flag( request.user).select_related("author").order_by( "-create_time").filter(visible=True, recommend=True)[:15] return render(request, 'home_logged_in.jinja2', context=ctx) else: return render(request, 'home.jinja2', context={ 'bg': '/static/image/bg/%d.jpg' % randint(1, 14), })
def home_view(request): if request.user.is_authenticated: ctx = {'solved': get_accept_problem_count(request.user.pk), 'bulletin': site_settings_get('BULLETIN', ''), 'global_rating': User.objects.filter(rating__gt=0).order_by("-rating")[:10], } if is_site_closed(request): return redirect(reverse("contest:list")) else: LIMIT, LIMIT_BLOG = 20, 15 ctx['blog_list'] = Blog.objects.with_likes().with_likes_flag(request.user).select_related( "author").order_by("-create_time").filter(visible=True, recommend=True, is_reward=False)[:LIMIT_BLOG] # prefetch more comments comment_list = XtdComment.objects.filter(is_public=True, is_removed=False).order_by( "-submit_date").select_related("user", "content_type").prefetch_related('content_object').all()[:LIMIT * 2] blog_list = Blog.objects.order_by("-create_time").select_related("author").filter( visible=True, is_reward=False)[:LIMIT] ctx['comment_list'] = [] i, j = 0, 0 for _ in range(LIMIT): while i < len(comment_list) and \ comment_list[i].content_type == ContentType.objects.get_for_model(Blog) and \ Blog.objects.filter(pk=comment_list[i].object_pk, is_reward=True).exists(): # Skip this comment i += 1 # Merge comments and blogs in time order if i < len(comment_list) and (j == len(blog_list) or ( j < len(blog_list) and comment_list[i].submit_date > blog_list[j].create_time)): ctx['comment_list'].append(comment_list[i]) i += 1 elif j < len(blog_list): ctx['comment_list'].append(blog_list[j]) j += 1 else: break for comment in ctx['comment_list']: if isinstance(comment, XtdComment) and len(comment.comment) > LIMIT: comment.comment = comment.comment[:LIMIT] + '...' return render(request, 'home_logged_in.jinja2', context=ctx) else: return render(request, 'home.jinja2', context={'bg': '/static/image/bg/%d.jpg' % randint(1, 14), })
def get_context_data(self, **kwargs): data = super(BaseContestMixin, self).get_context_data(**kwargs) data['contest'] = self.contest data['contest_status'] = self.contest.status data['current_time'] = timezone.now() if not self.contest.always_running: data['time_remaining'] = 0 if data['contest_status'] < 0: data['time_remaining'] = (self.contest.start_time - data['current_time']).total_seconds() elif data['contest_status'] == 0: data['time_remaining'] = (self.contest.end_time - data['current_time']).total_seconds() data['time_all'] = 0 if data['contest_status'] == 0: data['time_all'] = (self.contest.end_time - self.contest.start_time).total_seconds() data['contest_problem_list'] = self.contest.contest_problem_list data['has_permission'] = self.test_func() data['is_privileged'] = self.privileged data['is_volunteer'] = self.volunteer data['show_percent'] = self.contest.scoring_method == 'oi' data['site_closed'] = is_site_closed() and not self.privileged return data
def dispatch(self, request, *args, **kwargs): blogs = Blog.objects.with_likes().with_dislikes().with_likes_flag(request.user) self.blog = get_object_or_404(blogs, pk=kwargs.get('pk')) self.user = request.user if self.blog.is_reward and self.blog.contest: self.site_closed = is_site_closed(request) self.contest = self.blog.contest if self.site_closed: if self.contest.contest_type == 1: raise CloseSiteException if not self.contest.start_time - timedelta(minutes=30) <= timezone.now() \ <= self.contest.end_time + timedelta(minutes=10): raise CloseSiteException if self.contest.access_level >= 30: raise CloseSiteException self.privileged = is_contest_manager(self.user, self.contest) self.volunteer = is_contest_volunteer(self.user, self.contest) self.registered = False self.virtual_progress, self.participant = None, None self.participate_start_time = self.contest.start_time self.participate_end_time = self.contest.end_time self.participate_contest_status = self.contest.status if self.user.is_authenticated: try: self.participant = self.contest.contestparticipant_set.get(user=self.user) self.participate_start_time = self.participant.start_time(self.contest) self.participate_end_time = self.participant.end_time(self.contest) self.participate_contest_status = self.participant.status(self.contest) if self.participant is not None: self.registered = True except ContestParticipant.DoesNotExist: pass if not self.registered and (self.contest.access_level >= 30 or (self.contest.access_level >= 20 and self.contest.status > 0)): self.registered = True return super(BlogMixin, self).dispatch(request, *args, **kwargs)