def process_request(self, request): path = request.path if not path.startswith('/'): path = "/" + path acknowledged_announcements = [] announcements = amodels.Announcement.objects.filter(\ Q(url__isnull=True)|Q(url__exact=path)).filter(\ Q(start_time__isnull=True)|Q(start_time__lte=datetime.datetime.now())).filter(\ Q(expire_time__isnull=True)|Q(expire_time__gte=datetime.datetime.now())) if not request.user.is_authenticated(): announcements = announcements.exclude(auth_only=True) acknowledged_announcements.extend(decode_cookie(request.COOKIES)) else: use_auth = True for i in getattr(settings,"DATABASE_ROUTERS") or []: if "AnnouncementsRouter" in i: use_auth = False break if use_auth: announcements = announcements.exclude(auth_acknowledgments=request.user) acknowledged_announcements.extend(request.user.announcement_set.values_list('pk',flat=True).distinct()) acknowledged_announcements.extend(decode_cookie(request.COOKIES)) announcements = announcements.exclude(Q(acknowledge=True)&Q(pk__in=acknowledged_announcements)) request._announcements = announcements
def process_request(self, request): path = request.path if not path.startswith('/'): path = "/" + path acknowledged_announcements = [] now = datetime.datetime.now() # Use timezone aware datetime.now() method for django>=1.4 try: from django.utils.timezone import utc now = datetime.datetime.utcnow().replace(tzinfo=utc) except ImportError: pass announcements = amodels.Announcement.objects.filter(\ Q(url__isnull=True)|Q(url__exact=path)).filter(\ Q(start_time__isnull=True)|Q(start_time__lte=now)).filter(\ Q(expire_time__isnull=True)|Q(expire_time__gte=now)) if not request.user.is_authenticated(): announcements = announcements.exclude(auth_only=True) acknowledged_announcements.extend(decode_cookie(request.COOKIES)) else: use_auth = True for i in getattr(settings,"DATABASE_ROUTERS") or []: if "AnnouncementsRouter" in i: use_auth = False break if use_auth: announcements = announcements.exclude(auth_acknowledgments=request.user) acknowledged_announcements.extend(request.user.announcement_set.values_list('pk',flat=True).distinct()) acknowledged_announcements.extend(decode_cookie(request.COOKIES)) announcements = announcements.exclude(Q(acknowledge=True)&Q(pk__in=acknowledged_announcements)) request._announcements = announcements
def acknowledge(request,id,return_url=None): announcement = get_object_or_404(amodels.Announcement,pk=id) if request.user.is_authenticated(): use_auth = True for i in getattr(settings,"DATABASE_ROUTERS") or []: if "AnnouncementsRouter" in i: use_auth = False break if use_auth: announcement.auth_acknowledgments.add(request.user) return_url = request.GET.get('return_url') or return_url or '/' response = redirect(return_url) cookie_acknowledgments = [str(i) for i in decode_cookie(request.COOKIES)] cookie_acknowledgments.append(str(announcement.pk)) response.set_cookie(get_cookie_varname(),",".join(cookie_acknowledgments)) return response