44import hashlib
55from datetime import date , datetime
66from pathlib import Path
7- from typing import Self , Union , List , Optional , Literal
7+ from typing import Self , Union , List , Optional , Literal , Tuple
88import statistics
99
1010from django .contrib .contenttypes .models import ContentType
@@ -306,11 +306,11 @@ def __init_class__(cls):
306306 @classmethod
307307 def store_library_content (
308308 cls , library_content : bytes , builtin : bool = False , dry_run : bool = False
309- ) -> Union ["StoredLibrary" , dict , None ]:
309+ ) -> Tuple [ Optional [ Union ["StoredLibrary" , dict ]], Optional [ str ] ]:
310310 hash_checksum = sha256 (library_content )
311311 if not dry_run and hash_checksum in StoredLibrary .HASH_CHECKSUM_SET :
312312 # We do not store the library if its hash checksum is in the database.
313- return None
313+ return None , "libraryAlreadyLoadedError"
314314 try :
315315 library_data = yaml .safe_load (library_content )
316316 if not isinstance (library_data , dict ):
@@ -344,21 +344,24 @@ def store_library_content(
344344 key : (1 if key == "framework" else len (value ))
345345 for key , value in library_data ["objects" ].items ()
346346 }
347- return {
348- "name" : library_data ["name" ],
349- "urn" : urn ,
350- "locale" : locale ,
351- "version" : version ,
352- "ref_id" : library_data .get ("ref_id" ),
353- "description" : library_data .get ("description" ),
354- "provider" : library_data .get ("provider" ),
355- "packager" : library_data .get ("packager" ),
356- "publication_date" : library_data .get ("publication_date" ),
357- "objects_meta" : objects_meta ,
358- "is_loaded" : is_loaded ,
359- "builtin" : builtin ,
360- "copyright" : library_data .get ("copyright" ),
361- }
347+ return (
348+ {
349+ "name" : library_data ["name" ],
350+ "urn" : urn ,
351+ "locale" : locale ,
352+ "version" : version ,
353+ "ref_id" : library_data .get ("ref_id" ),
354+ "description" : library_data .get ("description" ),
355+ "provider" : library_data .get ("provider" ),
356+ "packager" : library_data .get ("packager" ),
357+ "publication_date" : library_data .get ("publication_date" ),
358+ "objects_meta" : objects_meta ,
359+ "is_loaded" : is_loaded ,
360+ "builtin" : builtin ,
361+ "copyright" : library_data .get ("copyright" ),
362+ },
363+ None ,
364+ )
362365
363366 same_version_lib = StoredLibrary .objects .filter (
364367 urn = urn , locale = locale , version = version
@@ -368,10 +371,13 @@ def store_library_content(
368371 logger .info ("update hash" , urn = urn )
369372 same_version_lib .hash_checksum = hash_checksum
370373 same_version_lib .save ()
371- return None
374+ return None , "libraryAlreadyLoadedError"
372375
373376 if StoredLibrary .objects .filter (urn = urn , locale = locale , version__gte = version ):
374- return None # We do not accept to store outdated libraries
377+ return (
378+ None ,
379+ "libraryOutdatedError" ,
380+ ) # We do not accept to store outdated libraries
375381
376382 with transaction .atomic ():
377383 # This code allows adding outdated libraries in the library store but they will be erased if a greater version of this library is stored.
@@ -425,16 +431,16 @@ def store_library_content(
425431 ), # autoload is true if the library contains requirement mapping sets
426432 )
427433 new_library .filtering_labels .set (filtering_labels )
428- return new_library
434+ return new_library , None
429435
430436 @classmethod
431437 def store_library_file (
432438 cls , fname : Path , builtin : bool = False
433- ) -> "StoredLibrary | None" :
439+ ) -> Tuple [ Optional [ "StoredLibrary" ], Optional [ str ]] :
434440 with open (fname , "rb" ) as f :
435441 library_content = f .read ()
436442
437- return StoredLibrary .store_library_content (library_content , builtin )
443+ return StoredLibrary .store_library_content (library_content , builtin = builtin )
438444
439445 def get_loaded_library (self ) -> Optional ["LoadedLibrary" ]:
440446 if not self .is_loaded :
0 commit comments