Skip to content
Snippets Groups Projects

Feature/pregenerate anonymous views

Merged Benoit Alessandroni requested to merge feature/pregenerate-anonymous-views into master
Files
4
@@ -11,7 +11,7 @@ max_depth = getattr(settings, 'MAX_RECURSION_DEPTH', 5)
request_timeout = getattr(settings, 'SSR_REQUEST_TIMEOUT', 10)
class Command(BaseCommand):
help = 'Generate static content for models with a specific meta attribute'
help = 'Generate static content for models having the static_version meta attribute set to 1/true'
def handle(self, *args, **kwargs):
output_dir = 'ssr'
@@ -21,19 +21,19 @@ class Command(BaseCommand):
for model in apps.get_models():
if hasattr(model._meta, 'static_version'):
print(f"model: {model}")
print(f"Generating content for model: {model}")
container_path = model.get_container_path()
url = f'{base_uri}{container_path}'
print(f"current request url before adding params: {url}")
print(f"Current request url before adding params: {url}")
if hasattr(model._meta, 'static_params'):
# static_params which is a json must be decomposed and added to the url as query parameters, first with ? then with &
# static_params are added to the url as query parameters
url += '?'
for key, value in model._meta.static_params.items():
url += f'{key}={value}&'
url = url[:-1]
print(f"current request url after adding params: {url}")
print(f"Current request url after adding params: {url}")
response = requests.get(url, timeout=request_timeout)
if response.status_code == 200:
@@ -41,9 +41,9 @@ class Command(BaseCommand):
content = self.update_ids_and_fetch_associated(content, base_uri, output_dir, 0, max_depth)
filename = container_path[1:-1]
file_path = os.path.join(output_dir, f'{filename}.json')
file_path = os.path.join(output_dir, f'{filename}.jsonld')
print(f"file_path: {file_path}")
print(f"Output file_path: {file_path}")
with open(file_path, 'w') as f:
f.write(content)
self.stdout.write(self.style.SUCCESS(f'Successfully fetched and saved content for {model._meta.model_name} from {url}'))
@@ -74,7 +74,7 @@ class Command(BaseCommand):
item['@id'] = urlunparse((parsed_url.scheme, parsed_url.netloc, path, parsed_url.params, parsed_url.query, parsed_url.fragment))
associated_url = urlunparse((parsed_url.scheme, parsed_url.netloc, parsed_url.path, parsed_url.params, parsed_url.query, parsed_url.fragment))
associated_file_path = path[1:-1] + '.json'
associated_file_path = path[1:-1] + '.jsonld'
associated_file_dir = os.path.dirname(associated_file_path)
if not os.path.exists(associated_file_dir):
Loading