No Description

manage_case_classifications_db.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # IRIS Source Code
  2. # contact@dfir-iris.org
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 3 of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. from sqlalchemy import func
  18. from typing import List
  19. from app.models.models import CaseClassification
  20. def get_case_classifications_list() -> List[dict]:
  21. """Get a list of case classifications
  22. Returns:
  23. List[dict]: List of case classifications
  24. """
  25. case_classifications = CaseClassification.query.with_entities(
  26. CaseClassification.id,
  27. CaseClassification.name,
  28. CaseClassification.name_expanded,
  29. CaseClassification.description,
  30. CaseClassification.creation_date
  31. ).all()
  32. c_cl = [row._asdict() for row in case_classifications]
  33. return c_cl
  34. def get_case_classification_by_id(cur_id: int) -> CaseClassification:
  35. """Get a case classification
  36. Args:
  37. cur_id (int): case classification id
  38. Returns:
  39. CaseClassification: Case classification
  40. """
  41. case_classification = CaseClassification.query.filter_by(id=cur_id).first()
  42. return case_classification
  43. def get_case_classification_by_name(cur_name: str) -> CaseClassification:
  44. """Get a case classification
  45. Args:
  46. cur_name (str): case classification name
  47. Returns:
  48. CaseClassification: Case classification
  49. """
  50. case_classification = CaseClassification.query.filter_by(name=cur_name).first()
  51. return case_classification
  52. def search_classification_by_name(name: str, exact_match: bool = False) -> List[dict]:
  53. """Search for a case classification by name
  54. Args:
  55. name (str): case classification name
  56. exact_match (bool, optional): Exact match. Defaults to False.
  57. Returns:
  58. List[dict]: List of case classifications
  59. """
  60. if exact_match:
  61. return CaseClassification.query.filter(func.lower(CaseClassification.name) == name.lower()).all()
  62. return CaseClassification.query.filter(CaseClassification.name.ilike(f'%{name}%')).all()