views.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from django.views.generic import View
  5. from libs import json_response, JsonParser, Argument
  6. from apps.alarm.models import Alarm, Group, Contact
  7. from apps.monitor.models import Detection
  8. import json
  9. class AlarmView(View):
  10. def get(self, request):
  11. alarms = Alarm.objects.all()
  12. return json_response(alarms)
  13. class GroupView(View):
  14. def get(self, request):
  15. groups = Group.objects.all()
  16. return json_response(groups)
  17. def post(self, request):
  18. form, error = JsonParser(
  19. Argument('id', type=int, required=False),
  20. Argument('name', help='请输入组名'),
  21. Argument('contacts', type=list, help='请选择联系人'),
  22. Argument('desc', required=False)
  23. ).parse(request.body)
  24. if error is None:
  25. form.contacts = json.dumps(form.contacts)
  26. if form.id:
  27. Group.objects.filter(pk=form.id).update(**form)
  28. else:
  29. form.created_by = request.user
  30. Group.objects.create(**form)
  31. return json_response(error=error)
  32. def delete(self, request):
  33. form, error = JsonParser(
  34. Argument('id', type=int, help='请指定操作对象')
  35. ).parse(request.GET)
  36. if error is None:
  37. detection = Detection.objects.filter(notify_grp__regex=fr'[^0-9]{form.id}[^0-9]').first()
  38. if detection:
  39. return json_response(error=f'监控任务【{detection.name}】正在使用该报警组,请解除关联后再尝试删除该联系组')
  40. Group.objects.filter(pk=form.id).delete()
  41. return json_response(error=error)
  42. class ContactView(View):
  43. def get(self, request):
  44. contacts = Contact.objects.all()
  45. return json_response(contacts)
  46. def post(self, request):
  47. form, error = JsonParser(
  48. Argument('id', type=int, required=False),
  49. Argument('name', help='请输入联系人姓名'),
  50. Argument('phone', required=False),
  51. Argument('email', required=False),
  52. Argument('ding', required=False),
  53. Argument('wx_token', required=False),
  54. Argument('qy_wx', required=False),
  55. ).parse(request.body)
  56. if error is None:
  57. if form.id:
  58. Contact.objects.filter(pk=form.id).update(**form)
  59. else:
  60. form.created_by = request.user
  61. Contact.objects.create(**form)
  62. return json_response(error=error)
  63. def delete(self, request):
  64. form, error = JsonParser(
  65. Argument('id', type=int, help='请指定操作对象')
  66. ).parse(request.GET)
  67. if error is None:
  68. group = Group.objects.filter(contacts__contains=f'\"{form.id}\"').first()
  69. if group:
  70. return json_response(error=f'报警联系组【{group.name}】包含此联系人,请解除关联后再尝试删除该联系人')
  71. Contact.objects.filter(pk=form.id).delete()
  72. return json_response(error=error)