cms.forms.widgets: 105 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/forms/widgets.py

Stats: 0 executed, 91 missed, 14 excluded, 115 ignored

  1. # -*- coding: utf-8 -*-
  2. from cms.forms.utils import get_site_choices, get_page_choices
  3. from cms.models import Page, PageUser, Placeholder
  4. from cms.plugin_pool import plugin_pool
  5. from cms.utils import get_language_from_request, cms_static_url
  6. from django.conf import settings
  7. from django.contrib.sites.models import Site
  8. from django.forms.widgets import Select, MultiWidget, Widget
  9. from django.template.context import RequestContext
  10. from django.template.loader import render_to_string
  11. from django.utils.encoding import force_unicode
  12. from django.utils.safestring import mark_safe
  13. from django.utils.translation import ugettext as _
  14. import copy
  15. from cms.templatetags.cms_admin import CMS_ADMIN_ICON_BASE
  16. class PageSelectWidget(MultiWidget):
  17. """A widget that allows selecting a page by first selecting a site and then
  18. a page on that site in a two step process.
  19. """
  20. def __init__(self, site_choices=None, page_choices=None, attrs=None):
  21. if attrs is not None:
  22. self.attrs = attrs.copy()
  23. else:
  24. self.attrs = {}
  25. if site_choices is None or page_choices is None:
  26. site_choices, page_choices = get_site_choices(), get_page_choices()
  27. self.site_choices = site_choices
  28. self.choices = page_choices
  29. widgets = (Select(choices=site_choices ),
  30. Select(choices=[('', '----')]),
  31. Select(choices=self.choices, attrs={'style': "display:none;"} ),
  32. )
  33. super(PageSelectWidget, self).__init__(widgets, attrs)
  34. def decompress(self, value):
  35. """
  36. receives a page_id in value and returns the site_id and page_id
  37. of that page or the current site_id and None if no page_id is given.
  38. """
  39. if value:
  40. page = Page.objects.get(pk=value)
  41. site = page.site
  42. return [site.pk, page.pk, page.pk]
  43. site = Site.objects.get_current()
  44. return [site.pk,None,None]
  45. def _has_changed(self, initial, data):
  46. # THIS IS A COPY OF django.forms.widgets.Widget._has_changed()
  47. # (except for the first if statement)
  48. """
  49. Return True if data differs from initial.
  50. """
  51. # For purposes of seeing whether something has changed, None is
  52. # the same as an empty string, if the data or inital value we get
  53. # is None, replace it w/ u''.
  54. if data is None or (len(data)>=2 and data[1] in [None,'']):
  55. data_value = u''
  56. else:
  57. data_value = data
  58. if initial is None:
  59. initial_value = u''
  60. else:
  61. initial_value = initial
  62. if force_unicode(initial_value) != force_unicode(data_value):
  63. return True
  64. return False
  65. def render(self, name, value, attrs=None):
  66. # THIS IS A COPY OF django.forms.widgets.MultiWidget.render()
  67. # (except for the last line)
  68. # value is a list of values, each corresponding to a widget
  69. # in self.widgets.
  70. if not isinstance(value, list):
  71. value = self.decompress(value)
  72. output = []
  73. final_attrs = self.build_attrs(attrs)
  74. id_ = final_attrs.get('id', None)
  75. for i, widget in enumerate(self.widgets):
  76. try:
  77. widget_value = value[i]
  78. except IndexError:
  79. widget_value = None
  80. if id_:
  81. final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
  82. output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
  83. output.append(r'''<script type="text/javascript">
  84. (function($) {
  85. var handleSiteChange = function(site_name, selected_id) {
  86. $("#id_%(name)s_1 optgroup").remove();
  87. var myOptions = $("#id_%(name)s_2 optgroup[label=" + site_name + "]").clone();
  88. $("#id_%(name)s_1").append(myOptions);
  89. $("#id_%(name)s_1").change();
  90. };
  91. var handlePageChange = function(page_id) {
  92. if (page_id) {
  93. $("#id_%(name)s_2 option").removeAttr('selected');
  94. $("#id_%(name)s_2 option[value=" + page_id + "]").attr('selected','selected');
  95. } else {
  96. $("#id_%(name)s_2 option[value=]").attr('selected','selected');
  97. };
  98. };
  99. $("#id_%(name)s_0").change(function(){
  100. var site_label = $("#id_%(name)s_0").children(":selected").text();
  101. handleSiteChange( site_label );
  102. });
  103. $("#id_%(name)s_1").change(function(){
  104. var page_id = $(this).find('option:selected').val();
  105. handlePageChange( page_id );
  106. });
  107. $(function(){
  108. handleSiteChange( $("#id_%(name)s_0").children(":selected").text() );
  109. $("#add_id_%(name)s").hide();
  110. });
  111. })(django.jQuery);
  112. </script>''' % {'name': name})
  113. return mark_safe(self.format_output(output))
  114. def format_output(self, rendered_widgets):
  115. return u' '.join(rendered_widgets)
  116. class PluginEditor(Widget):
  117. def __init__(self, attrs=None):
  118. if attrs is not None:
  119. self.attrs = attrs.copy()
  120. else:
  121. self.attrs = {}
  122. class Media:
  123. js = [cms_static_url(path) for path in (
  124. 'js/libs/jquery.ui.core.js',
  125. 'js/libs/jquery.ui.sortable.js',
  126. 'js/plugin_editor.js',
  127. )]
  128. css = {
  129. 'all': [cms_static_url(path) for path in (
  130. 'css/plugin_editor.css',
  131. )]
  132. }
  133. def render(self, name, value, attrs=None):
  134. context = {
  135. 'plugin_list': self.attrs['list'],
  136. 'installed_plugins': self.attrs['installed'],
  137. 'copy_languages': self.attrs['copy_languages'],
  138. 'language': self.attrs['language'],
  139. 'show_copy': self.attrs['show_copy'],
  140. 'placeholder': self.attrs['placeholder'],
  141. }
  142. return mark_safe(render_to_string(
  143. 'admin/cms/page/widgets/plugin_editor.html', context))
  144. class UserSelectAdminWidget(Select):
  145. """Special widget used in page permission inlines, because we have to render
  146. an add user (plus) icon, but point it somewhere else - to special user creation
  147. view, which is accessible only if user haves "add user" permissions.
  148. Current user should be assigned to widget in form constructor as an user
  149. attribute.
  150. """
  151. def render(self, name, value, attrs=None, choices=()):
  152. output = [super(UserSelectAdminWidget, self).render(name, value, attrs, choices)]
  153. if hasattr(self, 'user') and (self.user.is_superuser or \
  154. self.user.has_perm(PageUser._meta.app_label + '.' + PageUser._meta.get_add_permission())):
  155. # append + icon
  156. add_url = '../../../cms/pageuser/add/'
  157. output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
  158. (add_url, name))
  159. output.append(u'<img src="%sicon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (CMS_ADMIN_ICON_BASE, _('Add Another')))
  160. return mark_safe(u''.join(output))
  161. class PlaceholderPluginEditorWidget(PluginEditor):
  162. attrs = {}
  163. def __init__(self, request, filter_func):
  164. self.request = request
  165. self.filter_func = filter_func
  166. def __deepcopy__(self, memo):
  167. obj = copy.copy(self)
  168. obj.request = copy.copy(self.request)
  169. obj.filter_func = self.filter_func
  170. memo[id(self)] = obj
  171. return obj
  172. def render(self, name, value, attrs=None):
  173. try:
  174. ph = Placeholder.objects.get(pk=value)
  175. except Placeholder.DoesNotExist:
  176. ph = None
  177. context = {'add':True}
  178. if ph:
  179. plugin_list = ph.cmsplugin_set.filter(parent=None).order_by('position')
  180. plugin_list = self.filter_func(self.request, plugin_list)
  181. language = get_language_from_request(self.request)
  182. copy_languages = []
  183. if ph.actions.can_copy:
  184. copy_languages = ph.actions.get_copy_languages(
  185. placeholder=ph,
  186. model=ph._get_attached_model(),
  187. fieldname=ph._get_attached_field_name()
  188. )
  189. context = {
  190. 'plugin_list': plugin_list,
  191. 'installed_plugins': plugin_pool.get_all_plugins(ph.slot, include_page_only=False),
  192. 'copy_languages': copy_languages,
  193. 'language': language,
  194. 'show_copy': bool(copy_languages) and ph.actions.can_copy,
  195. 'urloverride': True,
  196. 'placeholder': ph,
  197. }
  198. #return mark_safe(render_to_string(
  199. # 'admin/cms/page/widgets/plugin_editor.html', context))
  200. return mark_safe(render_to_string(
  201. 'admin/cms/page/widgets/placeholder_editor.html', context, RequestContext(self.request)))