def on_get(self, req: falcon.Request, resp: falcon.Response, ip_list_name: str): ip_list = IPList.get_or_404(IPList.name == ip_list_name) resp.media = { "iplist": ip_list.to_dict(fields=[ "name", "description", "created_by", "is_active", "created_on" ]) }
def test_iplistitemresource_on_delete_remove_all(client, superuser): ip_list = IPList(name="test-list", created_by=User.get_by_token(superuser)) ip_list.save() json = {"ips": ["1.1.1.1", "2.2.2.2"], "note": "test note"} resp = client.simulate_post( "/api/test/iplists/test-list/items", headers={"Authorization": f"Token {superuser}"}, json=json, ) assert IPListItem.select().where((IPListItem.ip_list == ip_list)).count() == 2 json = {"ips": ["2.2.2.2", "1.1.1.1"]} resp = client.simulate_delete( "/api/test/iplists/test-list/items", headers={"Authorization": f"Token {superuser}"}, json=json, ) assert resp.status_code == 200 assert resp.json["count_removed"] == 2 assert IPListItem.select().where((IPListItem.ip_list == ip_list)).count() == 0
def on_delete(self, req: falcon.Request, resp: falcon.Response, ip_list_name: str = None): ip_list = IPList.get_or_404(IPList.name == ip_list_name) IPListItem.delete().where(IPListItem.ip_list == ip_list).execute() ip_list.delete_instance() resp.media = {"status": "Success", "message": "List deleted."}
def on_put(self, req: falcon.Request, resp: falcon.Response, ip_list_name: str = None): ip_list = IPList.get_or_404(IPList.name == ip_list_name) for k in ("description", "is_active", "is_public"): if k in req.media: setattr(ip_list, k, req.media.get(k)) ip_list.save() resp.media = {"status": "Success", "Message": "Updated IP List"}
def on_post(self, req: falcon.Request, resp: falcon.Response, ip_list_name: str = None): try: iplist = IPList( name=req.media.get("name"), description=req.media.get("description", None), is_active=req.media.get("is_active", True), is_public=req.media.get("is_public", True), created_by=req.context["user"], ) iplist.save() resp.status = falcon.HTTP_201 resp.media = { "status": "Success", "message": "Successfully, created iplist.", } except IntegrityError: raise falcon.HTTPBadRequest("Bad Request", "List with name already exists.")
def on_delete(self, req: falcon.Request, resp: falcon.Response, ip_list_name: str): ip_list = IPList.get_or_404(IPList.name == ip_list_name) ips = req.media.get("ips", None) remove_ips = ListItem.select().where(ListItem.ip.in_(ips)) deleted = (IPListItem.delete().where( (IPListItem.ip_list == ip_list), (IPListItem.ip.in_(remove_ips))).execute()) resp.media = {"count_removed": deleted, "requested_ips": ips}
def on_post(self, req: falcon.Request, resp: falcon.Response, ip_list_name: str): ip_list = IPList.get_or_404(IPList.name == ip_list_name) ips = req.media.get("ips") existing_ips = ListItem.select().where(ListItem.ip.in_(ips)) existing_ips_list = [x.ip for x in list(existing_ips)] new_ips_list = list(set(ips) - set(existing_ips_list)) ListItem.insert_many([(x, ) for x in new_ips_list], fields=[ListItem.ip]).execute() list_ips = (ListItem.select().join(IPListItem).join(IPList).where( (IPList.id == ip_list.id), (ListItem.ip.in_(existing_ips_list)))) list_ips_list = [x.ip for x in list(list_ips)] new_list_ips_list = list(set(ips) - set(list_ips_list)) note = req.media.get("note", None) if note: note = note.strip() ip_list_items = [] for list_item in ListItem.select().where( ListItem.ip.in_(new_list_ips_list)): ip_list_items.append( (ip_list, list_item, req.context["user"], note)) IPListItem.insert_many( ip_list_items, fields=[ IPListItem.ip_list, IPListItem.ip, IPListItem.added_by, IPListItem.note, ], ).execute() if len(new_ips_list) > 0 or len(new_list_ips_list) > 0: resp.status = falcon.HTTP_201 resp.media = { "requested_ips": ips, "created_ips": new_ips_list, "ips_added_to_list": new_list_ips_list, }