Django Library Mycreole
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.conf import settings
  2. from django.utils.translation import gettext as _
  3. import logging
  4. import importlib
  5. from mycreole import url_upload
  6. from themes import color_icon_url
  7. try:
  8. from config import APP_NAME as ROOT_LOGGER_NAME
  9. except ImportError:
  10. ROOT_LOGGER_NAME = 'root'
  11. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  12. NEW_ATTACHMENT_UID = 'new_attachment'
  13. def context_adaption(context, request, title, rel_path, **kwargs):
  14. def get_method(import_string):
  15. class_data = import_string.split(".")
  16. module_path = ".".join(class_data[:-1])
  17. class_str = class_data[-1]
  18. #
  19. module = importlib.import_module(module_path)
  20. return getattr(module, class_str)
  21. def log_warning(method_name, e):
  22. logger.warning('Could not import %s method for extending bars: %s - %s', method_name, type(e).__name__, e)
  23. context.set_additional_title(title)
  24. for key in ["menubar", "navibar"]:
  25. try:
  26. method_name = settings.MYCREOLE_BAR[key]
  27. except (AttributeError, KeyError) as e:
  28. log_warning(key, e)
  29. else:
  30. method = get_method(method_name)
  31. method(context, request, caller_name="attachments")
  32. add_new(request, context[context.ACTIONBAR], rel_path, kwargs.get('next'))
  33. for key in kwargs:
  34. context[key] = kwargs[key]
  35. logger.debug("context adapted: %s", repr(context))
  36. def add_new(request, bar, rel_path, nxt):
  37. bar.append_entry(
  38. NEW_ATTACHMENT_UID, # uid
  39. _('New Attachment'), # name
  40. color_icon_url(request, 'plus.png'), # icon
  41. url_upload(request, rel_path, nxt), # url
  42. True, # left
  43. False # active
  44. )