Browse Source

add relative filepath to tasks and add argument to cronjob

Nikos Atlas 9 months ago
parent
commit
caadfe238e

+ 3 - 2
src/telecaster/Clients/CosmohomeClient.py

@@ -273,15 +273,16 @@ def generate_xml():
 def generate_xml_task(filepath=None):
     if not filepath:
         raise RuntimeError("No filepath provided")
+
     xml = generate_xml()
-    with open(filepath, 'w') as file:
+    with open(settings.BASE_DIR / filepath, 'w') as file:
         file.write(xml)
 
 
 def generate_stats(filepath=None):
     if not filepath:
         raise RuntimeError("No filepath provided")
-    with open(filepath, 'r') as file:
+    with open(settings.BASE_DIR / filepath, 'r') as file:
         xml_string = file.read()
 
     data_dict = xmltodict.parse(xml_string)

+ 1 - 1
src/telecaster/settings.py

@@ -127,5 +127,5 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
 
 CRONTAB_COMMAND_PREFIX = 'PYTHONPATH=/app/src'  # Required for crontabs inside docker.
 CRONJOBS = [
-    ('40 * * * *', 'src.telecaster.Clients.CosmohomeClient.generate_xml_task'),
+    ('40 * * * *', 'src.telecaster.Clients.CosmohomeClient.generate_xml_task', ['artifacts/cosmohome_products.xml']),
 ]

+ 2 - 2
src/telecaster/views/CosmohomeView.py

@@ -11,14 +11,14 @@ class CosmohomeView(viewsets.ViewSet):
     @classmethod
     @action(methods=['get'], detail=False, url_path='generate/xml')
     def generate_xml(cls, request: Request, *args, **kwargs) -> HttpResponse:
-        filepath = settings.BASE_DIR / "artifacts/cosmohome_products.xml"
+        filepath = "artifacts/cosmohome_products.xml"
         final_xml = generate_xml_task(filepath)
         return HttpResponse('ok')
 
     @classmethod
     @action(methods=['get'], detail=False, url_path='stats')
     def generate_xml(cls, request: Request, *args, **kwargs) -> JsonResponse:
-        filepath = settings.BASE_DIR / "artifacts/cosmohome_products.xml"
+        filepath = "artifacts/cosmohome_products.xml"
         stats = generate_stats(filepath)
         return JsonResponse(stats)