cms.plugins.link.cms_plugins: 43 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/cms/plugins/link/cms_plugins.py

Stats: 0 executed, 36 missed, 7 excluded, 25 ignored

  1. from django.utils.translation import ugettext_lazy as _
  2. from django.contrib.sites.models import Site
  3. from django.conf import settings
  4. from cms.plugin_pool import plugin_pool
  5. from cms.plugin_base import CMSPluginBase
  6. from cms.plugins.link.forms import LinkForm
  7. from models import Link
  8. class LinkPlugin(CMSPluginBase):
  9. model = Link
  10. form = LinkForm
  11. name = _("Link")
  12. render_template = "cms/plugins/link.html"
  13. text_enabled = True
  14. def render(self, context, instance, placeholder):
  15. if instance.mailto:
  16. link = u"mailto:%s" % instance.mailto
  17. elif instance.url:
  18. link = instance.url
  19. elif instance.page_link:
  20. link = instance.page_link.get_absolute_url()
  21. else:
  22. link = ""
  23. context.update({
  24. 'name': instance.name,
  25. 'link': link,
  26. 'target':instance.target,
  27. 'placeholder': placeholder,
  28. 'object': instance
  29. })
  30. return context
  31. def get_form(self, request, obj=None, **kwargs):
  32. Form = super(LinkPlugin, self).get_form(request, obj, **kwargs)
  33. # this is bit tricky, since i don't wont override add_view and
  34. # change_view
  35. class FakeForm(object):
  36. def __init__(self, Form, site):
  37. self.Form = Form
  38. self.site = site
  39. # base fields are required to be in this fake class, this may
  40. # do some troubles, with new versions of django, if there will
  41. # be something more required
  42. self.base_fields = Form.base_fields
  43. def __call__(self, *args, **kwargs):
  44. # instanciate the form on call
  45. form = self.Form(*args, **kwargs)
  46. # tell form we are on this site
  47. form.for_site(self.site)
  48. return form
  49. # TODO: Make sure this works
  50. if self.cms_plugin_instance.page and self.cms_plugin_instance.page.site:
  51. site = self.cms_plugin_instance.page.site
  52. elif self.page and self.page.site:
  53. site = self.page.site
  54. else:
  55. # this might NOT give the result you expect
  56. site = Site.objects.get_current()
  57. return FakeForm(Form, site)
  58. def icon_src(self, instance):
  59. return settings.STATIC_URL + u"cms/images/plugins/link.png"
  60. plugin_pool.register_plugin(LinkPlugin)