Skip to content

Commit

Permalink
Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurswag committed May 15, 2024
1 parent 08d0ba5 commit 27b3e93
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def adapt_libraries(apps, schema_editor):
"frameworks": library.frameworks.count(),
"threats": library.threats.count(),
"reference_controls": library.reference_controls.count(),
"risk_matrix": library.risk_matrices.count()
"risk_matrix": library.risk_matrices.count(),
}
library.save()

Expand Down
4 changes: 1 addition & 3 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ def store_library_content(cls, library_content: bytes) -> "StoredLibrary | None"
locale = library_data.get("locale", "en")
version = int(library_data["version"])
is_loaded = LoadedLibrary.objects.filter(
urn=urn,
locale=locale,
version=version
urn=urn, locale=locale, version=version
).exists()

library_matches = [*StoredLibrary.objects.filter(urn=urn, locale=locale)]
Expand Down
13 changes: 6 additions & 7 deletions backend/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ def retrieve(self, request, *args, pk, **kwargs):
return Response(data)

def content(self, request, pk):
try :
try:
lib = StoredLibrary.objects.get(urn=pk)
except :
except:
return Response("Library not found.", status=HTTP_404_NOT_FOUND)
return Response(lib.content)

@action(detail=True, methods=["get"])
def content(self, request, pk):
try :
try:
lib = StoredLibrary.objects.get(urn=pk)
except :
except:
return Response("Library not found.", status=HTTP_404_NOT_FOUND)
return Response(lib.content)

Expand Down Expand Up @@ -251,17 +251,16 @@ def destroy(self, request, *args, pk, **kwargs):

@action(detail=True, methods=["get"])
def content(self, request, pk):
try :
try:
lib = LoadedLibrary.objects.get(urn=pk)
except :
except:
return Response("Library not found.", status=HTTP_404_NOT_FOUND)
return Response(lib._objects)

@action(detail=True, methods=["get"])
def tree(
self, request, pk
): # We must ensure that users that are not allowed to read the content of libraries can't have any access to them either from the /api/{URLModel/{library_urn}/tree view or the /api/{URLModel}/{library_urn} view.

try:
lib = LoadedLibrary.objects.get(urn=pk)
except:
Expand Down
30 changes: 12 additions & 18 deletions tools/add_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@
# This is a temporary tool that adds "builtin: true" to all libraries stored as yaml files under ./backend/library/libraries.

current_path = "/".join(__file__.split("/")[:-1])
library_path = os.path.join(current_path,"..","backend","library","libraries")
fnames = [
fname for fname in os.listdir(library_path)
if fname.endswith(".yaml")
]
library_path = os.path.join(current_path, "..", "backend", "library", "libraries")
fnames = [fname for fname in os.listdir(library_path) if fname.endswith(".yaml")]

for fname in fnames :
lib = os.path.join(library_path,fname)
for fname in fnames:
lib = os.path.join(library_path, fname)

with open(lib,"r",encoding="utf-8") as f:
content = f.read()
lines = content.split("\n")
with open(lib, "r", encoding="utf-8") as f:
content = f.read()
lines = content.split("\n")

if not any(
re.match(r"^builtin.*:.*true",line) is not None
for line in lines
) :
lines.insert(1,"builtin: true")
with open(lib,"w",encoding="utf-8") as f :
f.write("\n".join(lines))
print(f"Library '{fname}' updated.")
if not any(re.match(r"^builtin.*:.*true", line) is not None for line in lines):
lines.insert(1, "builtin: true")
with open(lib, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
print(f"Library '{fname}' updated.")

0 comments on commit 27b3e93

Please sign in to comment.