models.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from importlib import import_module
  2. import json
  3. from django.db import models
  4. from django.utils.encoding import python_2_unicode_compatible
  5. from django.utils.translation import ugettext_lazy as _
  6. from jet.utils import LazyDateTimeEncoder
  7. @python_2_unicode_compatible
  8. class UserDashboardModule(models.Model):
  9. title = models.CharField(verbose_name=_('Title'), max_length=255)
  10. module = models.CharField(verbose_name=_('module'), max_length=255)
  11. app_label = models.CharField(verbose_name=_('application name'), max_length=255, null=True, blank=True)
  12. user = models.PositiveIntegerField(verbose_name=_('user'))
  13. column = models.PositiveIntegerField(verbose_name=_('column'))
  14. order = models.IntegerField(verbose_name=_('order'))
  15. settings = models.TextField(verbose_name=_('settings'), default='', blank=True)
  16. children = models.TextField(verbose_name=_('children'), default='', blank=True)
  17. collapsed = models.BooleanField(verbose_name=_('collapsed'), default=False)
  18. class Meta:
  19. verbose_name = _('user dashboard module')
  20. verbose_name_plural = _('user dashboard modules')
  21. ordering = ('column', 'order')
  22. def __str__(self):
  23. return self.module
  24. def load_module(self):
  25. try:
  26. package, module_name = self.module.rsplit('.', 1)
  27. package = import_module(package)
  28. module = getattr(package, module_name)
  29. return module
  30. except AttributeError:
  31. return None
  32. except ImportError:
  33. return None
  34. def pop_settings(self, pop_settings):
  35. settings = json.loads(self.settings)
  36. for setting in pop_settings:
  37. if setting in settings:
  38. settings.pop(setting)
  39. self.settings = json.dumps(settings, cls=LazyDateTimeEncoder)
  40. self.save()
  41. def update_settings(self, update_settings):
  42. settings = json.loads(self.settings)
  43. settings.update(update_settings)
  44. self.settings = json.dumps(settings, cls=LazyDateTimeEncoder)
  45. self.save()