فهرست منبع

Added length check for nomenclature list in get_nomens_rests

Vsevolod Levitan 1 سال پیش
والد
کامیت
5701834ff5
4فایلهای تغییر یافته به همراه100 افزوده شده و 3 حذف شده
  1. 7 2
      main.py
  2. 87 0
      src/export/csv_exporter.py
  3. 6 0
      src/export/decorators/csv_export.py
  4. 0 1
      src/storage/storage_service.py

+ 7 - 2
main.py

@@ -78,7 +78,12 @@ def get_nomens_rests(nomenclature_name):
         nomen
         for nomen in start.storage.data[storage.nomenclature_key()]
         if nomen.name == nomenclature_name
-    ][0]
+    ]
+
+    if len(nomen) == 0:
+        return ""
+
+    nomen = nomen[0]
 
     service = storage_service(start.storage.data[storage.transaction_key()])
     result = service.create_turns_nomen(strg, nomen)
@@ -133,7 +138,7 @@ def debits_recipe(recipe_name):
             )
         )
 
-    start.storage.data[storage.journal_key()] += transactions
+    start.storage.data[storage.transaction_key()] += transactions
 
     return app.response_class(
         response=json.dumps({"success": True}),

+ 87 - 0
src/export/csv_exporter.py

@@ -0,0 +1,87 @@
+class csv_exporter:
+    def __init__(self, separator=",", line_separator="\n"):
+        self.__separator = separator
+        self.__line_separator = line_separator
+
+    def __retrieve_properties(self, model):
+        properties = [
+            attr for attr in dir(model) if hasattr(getattr(model, attr), "csv_export")
+        ]
+        csv_data = []
+        for prop in properties:
+            value = prop()
+            header = prop(True)
+            csv_data.append((header, value))
+        return csv_data
+
+    def generate_line(self, model):
+        """
+            Сгенерировать строку CSV-файла для модели
+        Args:
+            model (any): модель, для которой нужно сгенерировать CSV строку
+        Returns:
+            (str) строка CSV файла
+        Raises:
+            argument_exception
+            TODO
+        """
+
+        props = list(self.__retrieve_properties(model))
+        return self.__separator.join([prop[1] for prop in props])
+
+    def generate_header(self, model):
+        """
+            Сгенерировать заголовок CSV-файла для модели
+        Args:
+            model (any): модель, для которой нужно сгенерировать CSV заголовок
+        Returns:
+            (str) заголовок CSV файла
+        Raises:
+            argument_exception
+            TODO
+        """
+
+        props = list(self.__retrieve_properties(model))
+        return self.__separator.join([prop[0] for prop in props])
+
+    def generate_csv_content(self, models: list):
+        """
+            Сгенерировать содержимое CSV файла для списка моделей
+        Args:
+            models (list): список моделей, которые нужно включить в CSV-файл
+        Returns:
+            (str) содержимое CSV файла
+        Raises:
+            argument_exception
+            TODO
+        """
+
+        lines = []
+
+        lines.append(self.generate_header(models[0]))
+
+        for model in models:
+            lines.append(self.generate_line(model))
+
+        return self.__line_separator.join(lines)
+
+    def export_to_file(self, models: list, path: str):
+        """
+            Сгенерировать CSV и экспортировать в файл
+        Args:
+            models (list): список моделей, которые нужно включить в CSV-файл
+        Returns:
+            (bool) результат сохранения
+        Raises:
+            file_exception
+            argument_exception
+            TODO
+        """
+
+        with open(path, "w", encoding="UTF-8") as file:
+            file.write(self.generate_header(models[0]))
+
+            for model in models:
+                file.write(self.__line_separator + self.generate_line(model))
+
+        return True

+ 6 - 0
src/export/decorators/csv_export.py

@@ -0,0 +1,6 @@
+class csv_export:
+    def __init__(self, header):
+        self.header = header
+
+    def __call__(self, func):
+        return func

+ 0 - 1
src/storage/storage_service.py

@@ -3,7 +3,6 @@ from src.export.exporter_factory import exporter_factory
 from src.logic.process_factory import process_factory
 from src.storage.storage import storage
 from src.validation.validator import validator
-from datetime import datetime
 import json