Sin descripción

tracker.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 datetime import datetime
  19. from flask import request
  20. from flask_login import current_user
  21. import app
  22. from app import db
  23. from app.models.models import UserActivity
  24. log = app.app.logger
  25. def track_activity(message, caseid=None, ctx_less=False, user_input=False, display_in_ui=True):
  26. """
  27. Register a user activity in DB.
  28. :param message: Message to save as activity
  29. :return: Nothing
  30. """
  31. ua = UserActivity()
  32. try:
  33. ua.user_id = current_user.id
  34. except:
  35. pass
  36. try:
  37. ua.case_id = caseid if ctx_less is False else None
  38. except Exception:
  39. pass
  40. ua.activity_date = datetime.utcnow()
  41. ua.activity_desc = message.capitalize()
  42. if current_user.is_authenticated:
  43. log.info(f"{current_user.user} [#{current_user.id}] :: Case {caseid} :: {ua.activity_desc}")
  44. else:
  45. log.info(f"Anonymous :: Case {caseid} :: {ua.activity_desc}")
  46. ua.user_input = user_input
  47. ua.display_in_ui = display_in_ui
  48. ua.is_from_api = (request.cookies.get('session') is None if request else False)
  49. db.session.add(ua)
  50. db.session.commit()
  51. return ua