Sin descripción

client_db.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 marshmallow
  19. from sqlalchemy import func, and_
  20. from typing import List
  21. from app import db
  22. from app.datamgmt.exceptions.ElementExceptions import ElementInUseException
  23. from app.datamgmt.exceptions.ElementExceptions import ElementNotFoundException
  24. from app.models.cases import Cases
  25. from app.models.models import Client
  26. from app.models.models import Contact
  27. from app.models.authorization import User, UserClient
  28. from app.schema.marshables import ContactSchema
  29. from app.schema.marshables import CustomerSchema
  30. def get_client_list(current_user_id: int = None,
  31. is_server_administrator: bool = False) -> List[dict]:
  32. if not is_server_administrator:
  33. filter = and_(
  34. Client.client_id == UserClient.client_id,
  35. UserClient.user_id == current_user_id
  36. )
  37. else:
  38. filter = and_()
  39. client_list = Client.query.with_entities(
  40. Client.name.label('customer_name'),
  41. Client.client_id.label('customer_id'),
  42. Client.client_uuid.label('customer_uuid'),
  43. Client.description.label('customer_description'),
  44. Client.sla.label('customer_sla'),
  45. Client.custom_attributes
  46. ).filter(
  47. filter
  48. ).all()
  49. output = [c._asdict() for c in client_list]
  50. return output
  51. def get_client(client_id: int) -> Client:
  52. client = Client.query.filter(Client.client_id == client_id).first()
  53. return client
  54. def get_client_api(client_id: str) -> Client:
  55. client = Client.query.with_entities(
  56. Client.name.label('customer_name'),
  57. Client.client_id.label('customer_id'),
  58. Client.client_uuid.label('customer_uuid'),
  59. Client.description.label('customer_description'),
  60. Client.sla.label('customer_sla'),
  61. Client.custom_attributes
  62. ).filter(Client.client_id == client_id).first()
  63. output = None
  64. if client:
  65. output = client._asdict()
  66. return output
  67. def get_client_cases(client_id: int):
  68. cases_list = Cases.query.with_entities(
  69. Cases.case_id.label('case_id'),
  70. Cases.case_uuid.label('case_uuid'),
  71. Cases.name.label('case_name'),
  72. Cases.description.label('case_description'),
  73. Cases.status_id.label('case_status'),
  74. User.name.label('case_owner'),
  75. Cases.open_date,
  76. Cases.close_date
  77. ).filter(
  78. Cases.client_id == client_id,
  79. ).join(
  80. Cases.user
  81. ).all()
  82. return cases_list
  83. def create_client(data) -> Client:
  84. client_schema = CustomerSchema()
  85. client = client_schema.load(data)
  86. db.session.add(client)
  87. db.session.commit()
  88. return client
  89. def get_client_contacts(client_id: int) -> List[Contact]:
  90. contacts = Contact.query.filter(
  91. Contact.client_id == client_id
  92. ).order_by(
  93. Contact.contact_name
  94. ).all()
  95. return contacts
  96. def get_client_contact(client_id: int, contact_id: int) -> Contact:
  97. contact = Contact.query.filter(
  98. Contact.client_id == client_id,
  99. Contact.id == contact_id
  100. ).first()
  101. return contact
  102. def delete_contact(contact_id: int) -> None:
  103. contact = Contact.query.filter(
  104. Contact.id == contact_id
  105. ).first()
  106. if not contact:
  107. raise ElementNotFoundException('No Contact found with this uuid.')
  108. try:
  109. db.session.delete(contact)
  110. db.session.commit()
  111. except Exception:
  112. raise ElementInUseException('A currently referenced contact cannot be deleted')
  113. def create_contact(data, customer_id) -> Contact:
  114. data['client_id'] = customer_id
  115. contact_schema = ContactSchema()
  116. contact = contact_schema.load(data)
  117. db.session.add(contact)
  118. db.session.commit()
  119. return contact
  120. def update_contact(data, contact_id, customer_id) -> Contact:
  121. contact = get_client_contact(customer_id, contact_id)
  122. data['client_id'] = customer_id
  123. contact_schema = ContactSchema()
  124. contact_schema.load(data, instance=contact)
  125. db.session.commit()
  126. return contact
  127. def update_client(client_id: int, data) -> Client:
  128. # TODO: Possible reuse somewhere else ...
  129. client = get_client(client_id)
  130. if not client:
  131. raise ElementNotFoundException('No Customer found with this uuid.')
  132. exists = Client.query.filter(
  133. Client.client_id != client_id,
  134. func.lower(Client.name) == data.get('customer_name').lower()
  135. ).first()
  136. if exists:
  137. raise marshmallow.exceptions.ValidationError(
  138. "Customer already exists",
  139. field_name="customer_name"
  140. )
  141. client_schema = CustomerSchema()
  142. client_schema.load(data, instance=client)
  143. db.session.commit()
  144. return client
  145. def delete_client(client_id: int) -> None:
  146. client = Client.query.filter(
  147. Client.client_id == client_id
  148. ).first()
  149. if not client:
  150. raise ElementNotFoundException('No Customer found with this uuid.')
  151. try:
  152. db.session.delete(client)
  153. db.session.commit()
  154. except Exception:
  155. raise ElementInUseException('A currently referenced customer cannot be deleted')
  156. def get_case_client(case_id: int) -> Client:
  157. client = Cases.query.filter(case_id == case_id).with_entities(
  158. Cases.client_id
  159. ).first()
  160. return client