def get(self, request, project): """ List a project's filters Retrieve a list of filters for a given project. {method} {path} """ results = [] for flt in inbound_filters.get_all_filter_specs(): results.append({ "id": flt.id, # 'active' will be either a boolean or list for the legacy browser filters # all other filters will be boolean "active": inbound_filters.get_filter_state(flt.id, project), "description": flt.description, "name": flt.name, "hello": flt.id + " - " + flt.name, }) results.sort(key=lambda x: x["name"]) return Response(results)
def put(self, request: Request, project, filter_id) -> Response: """ Update a filter Update a project's filter. {method} {path} """ current_filter = None for flt in inbound_filters.get_all_filter_specs(): if flt.id == filter_id: current_filter = flt break else: raise ResourceDoesNotExist # could not find filter with the requested id serializer = current_filter.serializer_cls(data=request.data, partial=True) if not serializer.is_valid(): return Response(serializer.errors, status=400) current_state = inbound_filters.get_filter_state(filter_id, project) new_state = inbound_filters.set_filter_state(filter_id, project, serializer.validated_data) audit_log_state = AuditLogEntryEvent.PROJECT_ENABLE returned_state = None if filter_id == "legacy-browsers": if isinstance(current_state, bool) or isinstance(new_state, bool): returned_state = new_state if not new_state: audit_log_state = AuditLogEntryEvent.PROJECT_DISABLE elif current_state - new_state: returned_state = current_state - new_state audit_log_state = AuditLogEntryEvent.PROJECT_DISABLE elif new_state - current_state: returned_state = new_state - current_state elif new_state == current_state: returned_state = new_state if filter_id in ("browser-extensions", "localhost", "web-crawlers"): returned_state = filter_id removed = current_state - new_state if removed == 1: audit_log_state = AuditLogEntryEvent.PROJECT_DISABLE self.create_audit_entry( request=request, organization=project.organization, target_object=project.id, event=audit_log_state, data={"state": returned_state}, ) return Response(status=201)