Coverage for src/api/apps.py: 81%

27 statements  

« prev     ^ index     » next       coverage.py v7.11.1, created at 2025-11-08 10:41 +0000

1import os 

2from django.apps import AppConfig 

3from drf_spectacular.generators import SchemaGenerator 

4import yaml 

5import json 

6 

7class ApiConfig(AppConfig): 

8 default_auto_field = 'django.db.models.BigAutoField' 

9 name = 'src.api' 

10 

11 def ready(self): 

12 os.makedirs('docs/api/', exist_ok=True) 

13 generator = SchemaGenerator() 

14 new_schema = generator.get_schema(request=None, public=True) 

15 file_path = 'docs/api/openapi-generated.yaml' 

16 

17 if os.path.exists(file_path): 

18 with open(file_path, 'r') as f: 

19 try: 

20 old_schema = yaml.safe_load(f) 

21 # Convert to JSON for comparison (to avoid YAML formatting issues) 

22 new_schema_json = json.dumps(new_schema, sort_keys=True) 

23 old_schema_json = json.dumps(old_schema, sort_keys=True) 

24 if new_schema_json != old_schema_json: 

25 with open(file_path, 'w') as f: 

26 yaml.dump(new_schema, f) 

27 except Exception: 

28 # In case of an error (for example, a corrupted file), overwrite it 

29 with open(file_path, 'w') as f: 

30 yaml.dump(new_schema, f) 

31 else: 

32 # If the file doesn’t exist, create a new one 

33 with open(file_path, 'w') as f: 

34 yaml.dump(new_schema, f)