themes.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # coding:utf-8
  2. from __future__ import print_function
  3. import httplib2
  4. from django.template import loader
  5. from django.core.cache import cache
  6. from django.utils import six
  7. from django.utils.translation import ugettext as _
  8. from xadmin.sites import site
  9. from xadmin.models import UserSettings
  10. from xadmin.views import BaseAdminPlugin, BaseAdminView
  11. from xadmin.util import static, json
  12. import six
  13. if six.PY2:
  14. import urllib
  15. else:
  16. import urllib.parse
  17. THEME_CACHE_KEY = 'xadmin_themes'
  18. class ThemePlugin(BaseAdminPlugin):
  19. enable_themes = False
  20. # {'name': 'Blank Theme', 'description': '...', 'css': 'http://...', 'thumbnail': '...'}
  21. user_themes = None
  22. use_bootswatch = False
  23. default_theme = static('xadmin/css/themes/bootstrap-xadmin.css')
  24. bootstrap2_theme = static('xadmin/css/themes/bootstrap-theme.css')
  25. def init_request(self, *args, **kwargs):
  26. return self.enable_themes
  27. def _get_theme(self):
  28. if self.user:
  29. try:
  30. return UserSettings.objects.get(user=self.user, key="site-theme").value
  31. except Exception:
  32. pass
  33. if '_theme' in self.request.COOKIES:
  34. if six.PY2:
  35. func = urllib.unquote
  36. else:
  37. func = urllib.parse.unquote
  38. return func(self.request.COOKIES['_theme'])
  39. return self.default_theme
  40. def get_context(self, context):
  41. context['site_theme'] = self._get_theme()
  42. return context
  43. # Media
  44. def get_media(self, media):
  45. return media + self.vendor('jquery-ui-effect.js', 'xadmin.plugin.themes.js')
  46. # Block Views
  47. def block_top_navmenu(self, context, nodes):
  48. themes = [
  49. {'name': _(u"Default"), 'description': _(u"Default bootstrap theme"), 'css': self.default_theme},
  50. {'name': _(u"Bootstrap2"), 'description': _(u"Bootstrap 2.x theme"), 'css': self.bootstrap2_theme},
  51. ]
  52. select_css = context.get('site_theme', self.default_theme)
  53. if self.user_themes:
  54. themes.extend(self.user_themes)
  55. if self.use_bootswatch:
  56. ex_themes = cache.get(THEME_CACHE_KEY)
  57. if ex_themes:
  58. themes.extend(json.loads(ex_themes))
  59. else:
  60. ex_themes = []
  61. try:
  62. h = httplib2.Http()
  63. resp, content = h.request("https://bootswatch.com/api/3.json", 'GET', '',
  64. headers={"Accept": "application/json", "User-Agent": self.request.META['HTTP_USER_AGENT']})
  65. if six.PY3:
  66. content = content.decode()
  67. watch_themes = json.loads(content)['themes']
  68. ex_themes.extend([
  69. {'name': t['name'], 'description': t['description'],
  70. 'css': t['cssMin'], 'thumbnail': t['thumbnail']}
  71. for t in watch_themes])
  72. except Exception as e:
  73. print(e)
  74. cache.set(THEME_CACHE_KEY, json.dumps(ex_themes), 24 * 3600)
  75. themes.extend(ex_themes)
  76. nodes.append(loader.render_to_string('xadmin/blocks/comm.top.theme.html', {'themes': themes, 'select_css': select_css}))
  77. site.register_plugin(ThemePlugin, BaseAdminView)