models.py 2.7 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.db import models
  5. from libs import ModelMixin, human_datetime
  6. from apps.account.models import User
  7. import json
  8. class Alarm(models.Model, ModelMixin):
  9. MODES = (
  10. ('1', '微信'),
  11. ('2', '短信'),
  12. ('3', '钉钉'),
  13. ('4', '邮件'),
  14. ('5', '企业微信'),
  15. )
  16. STATUS = (
  17. ('1', '报警发生'),
  18. ('2', '故障恢复'),
  19. )
  20. name = models.CharField(max_length=50)
  21. type = models.CharField(max_length=50)
  22. target = models.CharField(max_length=100)
  23. notify_mode = models.CharField(max_length=255)
  24. notify_grp = models.CharField(max_length=255)
  25. status = models.CharField(max_length=2, choices=STATUS)
  26. duration = models.CharField(max_length=50)
  27. created_at = models.CharField(max_length=20, default=human_datetime)
  28. def to_dict(self, *args, **kwargs):
  29. tmp = super().to_dict(*args, **kwargs)
  30. tmp['notify_mode'] = ','.join(dict(self.MODES)[x] for x in json.loads(self.notify_mode))
  31. tmp['notify_grp'] = json.loads(self.notify_grp)
  32. tmp['status_alias'] = self.get_status_display()
  33. return tmp
  34. def __repr__(self):
  35. return '<Alarm %r>' % self.name
  36. class Meta:
  37. db_table = 'alarms'
  38. ordering = ('-id',)
  39. class Group(models.Model, ModelMixin):
  40. name = models.CharField(max_length=50)
  41. desc = models.CharField(max_length=255, null=True)
  42. contacts = models.TextField(null=True)
  43. created_at = models.CharField(max_length=20, default=human_datetime)
  44. created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
  45. def to_dict(self, *args, **kwargs):
  46. tmp = super().to_dict(*args, **kwargs)
  47. tmp['contacts'] = json.loads(self.contacts)
  48. return tmp
  49. def __repr__(self):
  50. return '<AlarmGroup %r>' % self.name
  51. class Meta:
  52. db_table = 'alarm_groups'
  53. ordering = ('-id',)
  54. class Contact(models.Model, ModelMixin):
  55. name = models.CharField(max_length=50)
  56. phone = models.CharField(max_length=20, null=True)
  57. email = models.CharField(max_length=255, null=True)
  58. ding = models.CharField(max_length=255, null=True)
  59. wx_token = models.CharField(max_length=255, null=True)
  60. qy_wx = models.CharField(max_length=255, null=True)
  61. created_at = models.CharField(max_length=20, default=human_datetime)
  62. created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
  63. def __repr__(self):
  64. return '<AlarmContact %r>' % self.name
  65. class Meta:
  66. db_table = 'alarm_contacts'
  67. ordering = ('-id',)