models.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from importlib import import_module
  2. import json
  3. from django.db import models
  4. from django.utils import timezone
  5. from django.utils.encoding import python_2_unicode_compatible
  6. from django.utils.translation import ugettext_lazy as _
  7. from jet.utils import DateTimeEncoder
  8. @python_2_unicode_compatible
  9. class Bookmark(models.Model):
  10. url = models.URLField(verbose_name=_('URL'))
  11. title = models.CharField(verbose_name=_('title'), max_length=255)
  12. user = models.PositiveIntegerField(verbose_name=_('user'))
  13. date_add = models.DateTimeField(verbose_name=_('date created'), default=timezone.now)
  14. class Meta:
  15. verbose_name = _('bookmark')
  16. verbose_name_plural = _('bookmarks')
  17. ordering = ('date_add',)
  18. def __str__(self):
  19. return self.title
  20. @python_2_unicode_compatible
  21. class PinnedApplication(models.Model):
  22. app_label = models.CharField(verbose_name=_('application name'), max_length=255)
  23. user = models.PositiveIntegerField(verbose_name=_('user'))
  24. date_add = models.DateTimeField(verbose_name=_('date created'), default=timezone.now)
  25. class Meta:
  26. verbose_name = _('pinned application')
  27. verbose_name_plural = _('pinned applications')
  28. ordering = ('date_add',)
  29. def __str__(self):
  30. return self.app_label
  31. @python_2_unicode_compatible
  32. class UserDashboardModule(models.Model):
  33. title = models.CharField(verbose_name=_('Title'), max_length=255)
  34. module = models.CharField(verbose_name=_('module'), max_length=255)
  35. app_label = models.CharField(verbose_name=_('application name'), max_length=255, null=True, blank=True)
  36. user = models.PositiveIntegerField(verbose_name=_('user'))
  37. column = models.PositiveIntegerField(verbose_name=_('column'))
  38. order = models.IntegerField(verbose_name=_('order'))
  39. settings = models.TextField(verbose_name=_('settings'), default='', blank=True)
  40. children = models.TextField(verbose_name=_('children'), default='', blank=True)
  41. collapsed = models.BooleanField(verbose_name=_('collapsed'), default=False)
  42. class Meta:
  43. verbose_name = _('user dashboard module')
  44. verbose_name_plural = _('user dashboard modules')
  45. ordering = ('column', 'order')
  46. def __str__(self):
  47. return self.module
  48. def load_module(self):
  49. try:
  50. package, module_name = self.module.rsplit('.', 1)
  51. package = import_module(package)
  52. module = getattr(package, module_name)
  53. return module
  54. except AttributeError:
  55. return None
  56. def pop_settings(self, pop_settings):
  57. settings = json.loads(self.settings)
  58. for setting in pop_settings:
  59. if setting in settings:
  60. settings.pop(setting)
  61. self.settings = json.dumps(settings, cls=DateTimeEncoder)
  62. self.save()
  63. def update_settings(self, update_settings):
  64. settings = json.loads(self.settings)
  65. settings.update(update_settings)
  66. self.settings = json.dumps(settings, cls=DateTimeEncoder)
  67. self.save()