暂无描述

manage_customers_routes.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. from flask import Blueprint
  19. from flask import redirect
  20. from flask import render_template
  21. from flask import url_for
  22. from flask_wtf import FlaskForm
  23. from app.datamgmt.client.client_db import get_client
  24. from app.datamgmt.client.client_db import get_client_api
  25. from app.datamgmt.client.client_db import get_client_contact
  26. from app.datamgmt.client.client_db import get_client_contacts
  27. from app.datamgmt.manage.manage_attribute_db import get_default_custom_attributes
  28. from app.forms import AddCustomerForm
  29. from app.forms import ContactForm
  30. from app.models.authorization import Permissions
  31. from app.schema.marshables import ContactSchema
  32. from app.blueprints.access_controls import ac_requires
  33. from app.blueprints.access_controls import ac_requires_client_access
  34. from app.blueprints.responses import page_not_found
  35. from app.blueprints.responses import response_error
  36. manage_customers_blueprint = Blueprint(
  37. 'manage_customers',
  38. __name__,
  39. template_folder='templates'
  40. )
  41. @manage_customers_blueprint.route('/manage/customers')
  42. @ac_requires(Permissions.customers_read, no_cid_required=True)
  43. def manage_customers(caseid, url_redir):
  44. if url_redir:
  45. return redirect(url_for('manage_customers.manage_customers', cid=caseid))
  46. form = AddCustomerForm()
  47. # Return default page of case management
  48. return render_template('manage_customers.html', form=form)
  49. @manage_customers_blueprint.route('/manage/customers/<int:client_id>/view', methods=['GET'])
  50. @ac_requires(Permissions.customers_read, no_cid_required=True)
  51. @ac_requires_client_access()
  52. def view_customer_page(client_id, caseid, url_redir):
  53. if url_redir:
  54. return redirect(url_for('manage_customers.manage_customers', cid=caseid))
  55. customer = get_client_api(client_id)
  56. if not customer:
  57. return page_not_found(None)
  58. form = FlaskForm()
  59. contacts = get_client_contacts(client_id)
  60. contacts = ContactSchema().dump(contacts, many=True)
  61. return render_template('manage_customer_view.html', customer=customer, form=form, contacts=contacts)
  62. @manage_customers_blueprint.route('/manage/customers/<int:client_id>/contacts/add/modal', methods=['GET'])
  63. @ac_requires(Permissions.customers_write, no_cid_required=True)
  64. @ac_requires_client_access()
  65. def customer_add_contact_modal(client_id, caseid, url_redir):
  66. if url_redir:
  67. return redirect(url_for('manage_customers.manage_customers', cid=caseid))
  68. form = ContactForm()
  69. return render_template('modal_customer_add_contact.html', form=form, contact=None)
  70. @manage_customers_blueprint.route('/manage/customers/<int:client_id>/contacts/<int:contact_id>/modal', methods=['GET'])
  71. @ac_requires(Permissions.customers_read, no_cid_required=True)
  72. @ac_requires_client_access()
  73. def customer_edit_contact_modal(client_id, contact_id, caseid, url_redir):
  74. if url_redir:
  75. return redirect(url_for('manage_customers.manage_customers', cid=caseid))
  76. contact = get_client_contact(client_id, contact_id)
  77. if not contact:
  78. return response_error(f"Invalid Contact ID {contact_id}")
  79. form = ContactForm()
  80. form.contact_name.render_kw = {'value': contact.contact_name}
  81. form.contact_email.render_kw = {'value': contact.contact_email}
  82. form.contact_mobile_phone.render_kw = {'value': contact.contact_mobile_phone}
  83. form.contact_work_phone.render_kw = {'value': contact.contact_work_phone}
  84. form.contact_note.data = contact.contact_note
  85. form.contact_role.render_kw = {'value': contact.contact_role}
  86. return render_template('modal_customer_add_contact.html', form=form, contact=contact)
  87. @manage_customers_blueprint.route('/manage/customers/update/<int:client_id>/modal', methods=['GET'])
  88. @ac_requires(Permissions.customers_read, no_cid_required=True)
  89. @ac_requires_client_access()
  90. def view_customer_modal(client_id, caseid, url_redir):
  91. if url_redir:
  92. return redirect(url_for('manage_customers.manage_customers', cid=caseid))
  93. form = AddCustomerForm()
  94. customer = get_client(client_id)
  95. if not customer:
  96. return response_error("Invalid Customer ID")
  97. form.customer_name.render_kw = {'value': customer.name}
  98. form.customer_description.data = customer.description
  99. form.customer_sla.data = customer.sla
  100. return render_template("modal_add_customer.html", form=form, customer=customer,
  101. attributes=customer.custom_attributes)
  102. @manage_customers_blueprint.route('/manage/customers/add/modal', methods=['GET'])
  103. @ac_requires(Permissions.customers_read, no_cid_required=True)
  104. def add_customers_modal(caseid, url_redir):
  105. if url_redir:
  106. return redirect(url_for('manage_customers.manage_customers', cid=caseid))
  107. form = AddCustomerForm()
  108. attributes = get_default_custom_attributes('client')
  109. return render_template("modal_add_customer.html", form=form, customer=None, attributes=attributes)