Geen omschrijving

case_rfiles_db.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. import datetime
  19. from flask_login import current_user
  20. from sqlalchemy import and_
  21. from sqlalchemy import desc
  22. from app import db
  23. from app.datamgmt.manage.manage_attribute_db import get_default_custom_attributes
  24. from app.datamgmt.states import update_evidences_state
  25. from app.models.models import CaseReceivedFile
  26. from app.models.models import Comments
  27. from app.models.models import EvidencesComments
  28. from app.models.authorization import User
  29. def get_rfiles(caseid):
  30. crf = CaseReceivedFile.query.filter(
  31. CaseReceivedFile.case_id == caseid
  32. ).order_by(
  33. desc(CaseReceivedFile.date_added)
  34. ).all()
  35. return crf
  36. def add_rfile(evidence, caseid, user_id):
  37. evidence.date_added = datetime.datetime.now()
  38. evidence.case_id = caseid
  39. evidence.user_id = user_id
  40. evidence.custom_attributes = get_default_custom_attributes('evidence')
  41. db.session.add(evidence)
  42. update_evidences_state(caseid=caseid, userid=user_id)
  43. db.session.commit()
  44. return evidence
  45. def get_rfile(rfile_id, caseid):
  46. return CaseReceivedFile.query.filter(
  47. CaseReceivedFile.id == rfile_id,
  48. CaseReceivedFile.case_id == caseid
  49. ).first()
  50. def update_rfile(evidence, user_id, caseid):
  51. evidence.user_id = user_id
  52. update_evidences_state(caseid=caseid, userid=user_id)
  53. db.session.commit()
  54. return evidence
  55. def delete_rfile(rfile_id, caseid):
  56. with db.session.begin_nested():
  57. com_ids = EvidencesComments.query.with_entities(
  58. EvidencesComments.comment_id
  59. ).filter(
  60. EvidencesComments.comment_evidence_id == rfile_id
  61. ).all()
  62. com_ids = [c.comment_id for c in com_ids]
  63. EvidencesComments.query.filter(EvidencesComments.comment_id.in_(com_ids)).delete()
  64. Comments.query.filter(Comments.comment_id.in_(com_ids)).delete()
  65. CaseReceivedFile.query.filter(and_(
  66. CaseReceivedFile.id == rfile_id,
  67. CaseReceivedFile.case_id == caseid,
  68. )).delete()
  69. update_evidences_state(caseid=caseid)
  70. db.session.commit()
  71. def get_case_evidence_comments(evidence_id):
  72. return Comments.query.filter(
  73. EvidencesComments.comment_evidence_id == evidence_id
  74. ).join(
  75. EvidencesComments,
  76. Comments.comment_id == EvidencesComments.comment_id
  77. ).order_by(
  78. Comments.comment_date.asc()
  79. ).all()
  80. def add_comment_to_evidence(evidence_id, comment_id):
  81. ec = EvidencesComments()
  82. ec.comment_evidence_id = evidence_id
  83. ec.comment_id = comment_id
  84. db.session.add(ec)
  85. db.session.commit()
  86. def get_case_evidence_comments_count(evidences_list):
  87. return EvidencesComments.query.filter(
  88. EvidencesComments.comment_evidence_id.in_(evidences_list)
  89. ).with_entities(
  90. EvidencesComments.comment_evidence_id,
  91. EvidencesComments.comment_id
  92. ).group_by(
  93. EvidencesComments.comment_evidence_id,
  94. EvidencesComments.comment_id
  95. ).all()
  96. def get_case_evidence_comment(evidence_id, comment_id):
  97. return EvidencesComments.query.filter(
  98. EvidencesComments.comment_evidence_id == evidence_id,
  99. EvidencesComments.comment_id == comment_id
  100. ).with_entities(
  101. Comments.comment_id,
  102. Comments.comment_text,
  103. Comments.comment_date,
  104. Comments.comment_update_date,
  105. Comments.comment_uuid,
  106. User.name,
  107. User.user
  108. ).join(
  109. EvidencesComments.comment
  110. ).join(
  111. Comments.user
  112. ).first()
  113. def delete_evidence_comment(evidence_id, comment_id):
  114. comment = Comments.query.filter(
  115. Comments.comment_id == comment_id,
  116. Comments.comment_user_id == current_user.id
  117. ).first()
  118. if not comment:
  119. return False, "You are not allowed to delete this comment"
  120. EvidencesComments.query.filter(
  121. EvidencesComments.comment_evidence_id == evidence_id,
  122. EvidencesComments.comment_id == comment_id
  123. ).delete()
  124. db.session.delete(comment)
  125. db.session.commit()
  126. return True, "Comment deleted"