models.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 django.core.cache import cache
  6. from libs import ModelMixin, human_datetime
  7. from libs.channel import Channel
  8. import time
  9. class Notify(models.Model, ModelMixin):
  10. TYPES = (
  11. ('1', '通知'),
  12. ('2', '待办'),
  13. )
  14. SOURCES = (
  15. ('monitor', '监控中心'),
  16. ('schedule', '任务计划'),
  17. ('flag', '应用发布'),
  18. )
  19. title = models.CharField(max_length=255)
  20. source = models.CharField(max_length=10, choices=SOURCES)
  21. type = models.CharField(max_length=2, choices=TYPES)
  22. content = models.CharField(max_length=255, null=True)
  23. unread = models.BooleanField(default=True)
  24. link = models.CharField(max_length=255, null=True)
  25. created_at = models.CharField(max_length=20, default=human_datetime)
  26. @classmethod
  27. def make_notify(cls, source, type, title, content=None, with_quiet=True):
  28. if not with_quiet or time.time() - cache.get('spug:notify_quiet', 0) > 3600:
  29. cache.set('spug:notify_quiet', time.time())
  30. cls.objects.create(source=source, title=title, type=type, content=content)
  31. Channel.send_notify(title, content)
  32. def __repr__(self):
  33. return '<Notify %r>' % self.title
  34. class Meta:
  35. db_table = 'notifies'
  36. ordering = ('-id',)