def post(self, *args, **kwargs): """Resets today's cache (refresh button mobile click event)""" if self.kwargs.get("slug") == "today": manager = TopicListManager("today", self.request.user) manager.delete_cache(flush=True, delimiter=True) return redirect(self.request.path) return HttpResponseBadRequest()
def resolve_topics(_parent, info, slug, **kwargs): # Convert string query parameters to actual dicitonary to use it in TopicListHandler search_keys = dict(parse_qsl( kwargs.get("search_keys"))) if kwargs.get("search_keys") else {} manager = TopicListManager(slug, info.context.user, kwargs.get("year"), search_keys, kwargs.get("tab"), kwargs.get("exclusions")) if kwargs.get("refresh"): manager.delete_cache() frame = LeftFrame(manager, kwargs.get("page")) page = frame.page # May raise PermissionDenied or Http404 object_list = [ Topic(title=t["title"], slug=t["slug"], count=t.get("count")) for t in page["object_list"] ] paginator = { "num_pages": page["paginator"]["num_pages"], "page_range": page["paginator"]["page_range"] } tabs = (Tabs( current=frame.tabs["current"], available=[ Tab(name=key, safename=value) for key, value in frame.tabs["available"].items() ], ) if frame.tabs else None) exclusions = (Exclusions(active=frame.exclusions["active"], available=frame.exclusions["available"]) if frame.exclusions else None) page_data = { "has_next": page.get("has_next"), "has_other_pages": page.get("has_other_pages"), "has_previous": page.get("has_previous"), "number": page.get("number"), "object_list": object_list, "paginator": Paginator(**paginator), } data = { "page": Page(**page_data), "parameters": frame.parameters, "refresh_count": frame.refresh_count, "year": frame.year, "year_range": frame.year_range, "safename": frame.safename, "slug": frame.slug, "slug_identifier": frame.slug_identifier, "tabs": tabs, "exclusions": exclusions, } return TopicList(**data)
def _get_context(self, manager=None): try: handler = manager or TopicListManager( self.slug, self.user, self._year, self._search_keys, self._tab, self._exclusions, self._extra ) context = LeftFrame(handler, page=self._page).as_context() except (Http404, PermissionDenied): self.set_cookie("lfac", settings.DEFAULT_CATEGORY) return self._get_context(manager=TopicListManager(settings.DEFAULT_CATEGORY)) return context
def get_context_data(self, **kwargs): params = self.request.GET exclusions = self.request.COOKIES.get("lfex") if exclusions is not None: try: parsed = json.loads(unquote(exclusions)) if isinstance(parsed, list) and all(isinstance(s, str) for s in parsed): exclusions = parsed else: raise ValueError except (JSONDecodeError, ValueError): exclusions = None if exclusions is None: self.cookies = {"lfex": json.dumps(settings.DEFAULT_EXCLUSIONS)} query = ( self.kwargs.get("slug"), self.request.user, params.get("year"), params, # search keys params.get("tab"), exclusions, {"user": params.get("user"), "channel": params.get("channel")}, # extras ) manager = TopicListManager(*query) frame = LeftFrame(manager, params.get("page")) return frame.as_context()
def _get_context(self, manager=None, attempt=1): if attempt > 3: return { "safename": _("something went wrong. try reloading the page.") } try: handler = manager or TopicListManager( self.slug, self.user, self._year, self._search_keys, self._tab, self._exclusions, self._extra) context = LeftFrame(handler, page=self._page).as_context() except (Http404, PermissionDenied): self.set_cookie("lfac", settings.DEFAULT_CATEGORY) return self._get_context(manager=TopicListManager( settings.DEFAULT_CATEGORY), attempt=attempt + 1) return context