def get(self, notice_id): errors = self._check_notice_id(notice_id) if errors: return response.unprocessable_entity(errors=errors) notices_cached = self.cache_get("notices") if notices_cached is None: return response.not_found("No cache for notices.") notices_activated = [n for n in notices_cached.values() if not n["is_muted"]] return response.with_targets(notices_activated)
def get(self, cname): # return list of mirrors errors = self._check_cname(cname) if errors: return response.unprocessable_entity(errors=errors) mirrors_cached = self.cache_get("mirrors") if mirrors_cached is None: return response.not_found("No cache for mirrors.") if cname is None: targets = mirrors_cached.values() targets_sorted = sorted(targets, key=lambda d: d["cname"]) return response.with_targets(targets_sorted) else: if cname not in mirrors_cached.keys(): return response.not_found("No such resource.") return response.with_target(mirrors_cached[cname])
def post(self): json_data = request.get_json(silent=True) if not json_data: return response.bad_request("Problems parsing JSON.") errors = self._check_post_params(json_data) if errors: return response.unprocessable_entity(errors=errors) notices_cached = self.cache_get("notices") if not notices_cached: notices_cached = {} notices_created = notices_cached for notice_meta in json_data["targets"]: id_str = str(notice_meta["id"]) if id_str in notices_cached.keys(): continue notices_created[id_str] = self._build_notice(notice_meta) if notices_created: self.cache_set("notices", notices_created) return response.created() else: return response.ok("Nothing changed.")
def post(self): json_data = request.get_json(silent=True) if not json_data: return response.bad_request("Problems parsing JSON.") errors = self._check_post_params(json_data) if errors: return response.unprocessable_entity(errors=errors) mirrors_cached = self.cache_get("mirrors") if not mirrors_cached: mirrors_cached = {} mirrors_created = mirrors_cached.copy() for mirror_meta in json_data["targets"]: cname = mirror_meta["cname"] # Just pass existed items and don't update it here! if cname in mirrors_cached.keys(): continue mirrors_created[cname] = self._build_mirror(mirror_meta) if mirrors_created: self.cache_set("mirrors", mirrors_created) return response.created() else: return response.ok("Nothing changed.")