Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 230 lines (194 sloc) 8.448 kB
e5cce60 @chrisglass Encoding changes for the rest of the CMS stuff.
chrisglass authored
1 # -*- coding: utf-8 -*-
3ebfde4 @ojii Saner defaults for plugins
ojii authored
2 import re
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
3 from cms.exceptions import SubClassNeededError, Deprecated
27b9af9 Basis of the plugin structure. Work in progress
divio authored
4 from cms.models import CMSPlugin
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
5 from django import forms
7353b46 @pcicman cms settings changed, added merging with django settings
pcicman authored
6 from django.conf import settings
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
7 from django.contrib import admin
7d8767c @ojii Removed unneeded js code from jquery.tweet.js
ojii authored
8 from django.core.exceptions import ImproperlyConfigured
27b9af9 Basis of the plugin structure. Work in progress
divio authored
9 from django.forms.models import ModelForm
48e8f37 fixed some navigation issues with nav extenders
divio authored
10 from django.utils.encoding import smart_str
b7cb4d0 @jezdez A bunch of changes I don't remember having caused during the countles…
jezdez authored
11 from django.utils.translation import ugettext_lazy as _
7b56b68 @fivethreeo added plugin media utils/templatetag
fivethreeo authored
12
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
13 class CMSPluginBaseMetaclass(forms.MediaDefiningClass):
b4b12e0 @ojii make plugins suck less and added a cms.utils.placeholder.get_placehol…
ojii authored
14 """
15 Ensure the CMSPlugin subclasses have sane values and set some defaults if
16 they're not given.
17 """
7b56b68 @fivethreeo added plugin media utils/templatetag
fivethreeo authored
18 def __new__(cls, name, bases, attrs):
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
19 super_new = super(CMSPluginBaseMetaclass, cls).__new__
20 parents = [base for base in bases if isinstance(base, CMSPluginBaseMetaclass)]
21 if not parents:
b4b12e0 @ojii make plugins suck less and added a cms.utils.placeholder.get_placehol…
ojii authored
22 # If this is CMSPluginBase itself, and not a subclass, don't do anything
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
23 return super_new(cls, name, bases, attrs)
24 new_plugin = super_new(cls, name, bases, attrs)
b4b12e0 @ojii make plugins suck less and added a cms.utils.placeholder.get_placehol…
ojii authored
25 # validate model is actually a CMSPlugin subclass.
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
26 if not issubclass(new_plugin.model, CMSPlugin):
27 raise SubClassNeededError(
28 "The 'model' attribute on CMSPluginBase subclasses must be "
5b0524e @piquadrat fix the exception text of ``SubClassNeededError`` to reflect what is …
piquadrat authored
29 "either CMSPlugin or a subclass of CMSPlugin. %r on %r is not."
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
30 % (new_plugin.model, new_plugin)
31 )
7d8767c @ojii Removed unneeded js code from jquery.tweet.js
ojii authored
32 # validate the template:
33 if not hasattr(new_plugin, 'render_template'):
34 raise ImproperlyConfigured(
35 "CMSPluginBase subclasses must have a render_template attribute"
36 )
b4b12e0 @ojii make plugins suck less and added a cms.utils.placeholder.get_placehol…
ojii authored
37 # Set the default form
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
38 if not new_plugin.form:
39 form_meta_attrs = {
40 'model': new_plugin.model,
41 'exclude': ('position', 'placeholder', 'language', 'plugin_type')
42 }
43 form_attrs = {
44 'Meta': type('Meta', (object,), form_meta_attrs)
45 }
46 new_plugin.form = type('%sForm' % name, (ModelForm,), form_attrs)
b4b12e0 @ojii make plugins suck less and added a cms.utils.placeholder.get_placehol…
ojii authored
47 # Set the default fieldsets
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
48 if not new_plugin.fieldsets:
49 basic_fields = []
50 advanced_fields = []
51 for f in new_plugin.model._meta.fields:
52 if not f.auto_created and f.editable:
53 if hasattr(f,'advanced'):
54 advanced_fields.append(f.name)
55 else: basic_fields.append(f.name)
56 if advanced_fields:
57 new_plugin.fieldsets = [
58 (
59 None,
60 {
61 'fields': basic_fields
62 }
63 ),
64 (
65 _('Advanced options'),
66 {
67 'fields' : advanced_fields,
68 'classes' : ('collapse',)
69 }
70 )
71 ]
b4b12e0 @ojii make plugins suck less and added a cms.utils.placeholder.get_placehol…
ojii authored
72 # Set default name
73 if not new_plugin.name:
3ebfde4 @ojii Saner defaults for plugins
ojii authored
74 new_plugin.name = re.sub("([a-z])([A-Z])","\g<1> \g<2>", name)
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
75 return new_plugin
7b56b68 @fivethreeo added plugin media utils/templatetag
fivethreeo authored
76
77
1f806e9 fixed that the plugin description gets updated if you save or add a p…
divio authored
78 class CMSPluginBase(admin.ModelAdmin):
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
79 __metaclass__ = CMSPluginBaseMetaclass
7b56b68 @fivethreeo added plugin media utils/templatetag
fivethreeo authored
80
27b9af9 Basis of the plugin structure. Work in progress
divio authored
81 name = ""
2db9000 Pugin inlines support for admin interface.
pcicman authored
82
7bd203f @digi604 some drafts for new docs
digi604 authored
83 form = None
2db9000 Pugin inlines support for admin interface.
pcicman authored
84 change_form_template = "admin/cms/page/plugin_change_form.html"
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
85 # Should the plugin be rendered in the admin?
3ebfde4 @ojii Saner defaults for plugins
ojii authored
86 admin_preview = False
2db9000 Pugin inlines support for admin interface.
pcicman authored
87
48e8f37 fixed some navigation issues with nav extenders
divio authored
88 render_template = None
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
89 # Should the plugin be rendered at all, or doesn't it have any output?
90 render_plugin = True
985685b some fixes
divio authored
91 model = CMSPlugin
c943283 @digi604 a lot of stuff
digi604 authored
92 text_enabled = False
ec1ee5c @fivethreeo attempt at fixing #800
fivethreeo authored
93 page_only = False
27b9af9 Basis of the plugin structure. Work in progress
divio authored
94
7bd203f @digi604 some drafts for new docs
digi604 authored
95 opts = {}
6dd94f4 Added plugin module ordering
Alberto Paro authored
96 module = None #track in which module/application belongs
7bd203f @digi604 some drafts for new docs
digi604 authored
97
ca4d90a @digi604 #32 plugin base has now the same signature as ModelAdmin
digi604 authored
98 def __init__(self, model=None, admin_site=None):
2db9000 Pugin inlines support for admin interface.
pcicman authored
99 if admin_site:
100 super(CMSPluginBase, self).__init__(self.model, admin_site)
101
102 self.object_successfully_changed = False
103
590f066 @ojii started work on placeholder branch
ojii authored
104 # variables will be overwritten in edit_view, so we got required
2db9000 Pugin inlines support for admin interface.
pcicman authored
105 self.cms_plugin_instance = None
106 self.placeholder = None
590f066 @ojii started work on placeholder branch
ojii authored
107 self.page = None
7b56b68 @fivethreeo added plugin media utils/templatetag
fivethreeo authored
108
3ebfde4 @ojii Saner defaults for plugins
ojii authored
109
d6f6ded @digi604 fixes #272 signature of render function was not correct in pluginbase
digi604 authored
110 def render(self, context, instance, placeholder):
3ebfde4 @ojii Saner defaults for plugins
ojii authored
111 context['instance'] = instance
112 context['placeholder'] = placeholder
113 return context
27b9af9 Basis of the plugin structure. Work in progress
divio authored
114
f351d52 @ojii fixed regression in placeholderadmin
ojii authored
115 @property
116 def parent(self):
117 return self.cms_plugin_instance.parent
118
2db9000 Pugin inlines support for admin interface.
pcicman authored
119 def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
35fb7bb plugins working again
patricklauber authored
120 """
121 We just need the popup interface here
2db9000 Pugin inlines support for admin interface.
pcicman authored
122 """
123 context.update({
fc4f4ae @digi604 added a missing file from before, login now possible over toolbar
digi604 authored
124 'preview': not "no_preview" in request.GET,
2db9000 Pugin inlines support for admin interface.
pcicman authored
125 'is_popup': True,
126 'plugin': self.cms_plugin_instance,
7353b46 @pcicman cms settings changed, added merging with django settings
pcicman authored
127 'CMS_MEDIA_URL': settings.CMS_MEDIA_URL,
2db9000 Pugin inlines support for admin interface.
pcicman authored
128 })
4671406 @digi604 cleanup
digi604 authored
129
2db9000 Pugin inlines support for admin interface.
pcicman authored
130 return super(CMSPluginBase, self).render_change_form(request, context, add, change, form_url, obj)
7b56b68 @fivethreeo added plugin media utils/templatetag
fivethreeo authored
131
b73f132 @pcicman - permission bug fixing;
pcicman authored
132 def has_add_permission(self, request, *args, **kwargs):
133 """Permission handling change - if user is allowed to change the page
134 he must be also allowed to add/change/delete plugins..
135
136 Not sure if there will be plugin permission requirement in future, but
137 if, then this must be changed.
138 """
65ea80e @ojii still trying to fix that regression
ojii authored
139 return self.cms_plugin_instance.has_change_permission(request)
bfd3146 @digi604 permission bug
digi604 authored
140 has_delete_permission = has_change_permission = has_add_permission
b73f132 @pcicman - permission bug fixing;
pcicman authored
141
2db9000 Pugin inlines support for admin interface.
pcicman authored
142 def save_model(self, request, obj, form, change):
35fb7bb plugins working again
patricklauber authored
143 """
144 Override original method, and add some attributes to obj
2db9000 Pugin inlines support for admin interface.
pcicman authored
145 This have to be made, because if object is newly created, he must know
ba034fb @digi604 fixes #68 orphaned text embed plugins are now deleted
digi604 authored
146 where he lives.
35fb7bb plugins working again
patricklauber authored
147 Attributes from cms_plugin_instance have to be assigned to object, if
148 is cms_plugin_instance attribute available.
b366204 jstree update
divio authored
149 """
2db9000 Pugin inlines support for admin interface.
pcicman authored
150
151 if getattr(self, "cms_plugin_instance"):
152 # assign stuff to object
153 fields = self.cms_plugin_instance._meta.fields
154 for field in fields:
155 # assign all the fields - we can do this, because object is
156 # subclassing cms_plugin_instance (one to one relation)
157 value = getattr(self.cms_plugin_instance, field.name)
158 setattr(obj, field.name, value)
159
35fb7bb plugins working again
patricklauber authored
160 # remember the saved object
161 self.saved_object = obj
162
2db9000 Pugin inlines support for admin interface.
pcicman authored
163 return super(CMSPluginBase, self).save_model(request, obj, form, change)
164
165 def response_change(self, request, obj):
35fb7bb plugins working again
patricklauber authored
166 """
167 Just set a flag, so we know something was changed, and can make
168 new version if reversion installed.
2db9000 Pugin inlines support for admin interface.
pcicman authored
169 New version will be created in admin.views.edit_plugin
170 """
171 self.object_successfully_changed = True
172 return super(CMSPluginBase, self).response_change(request, obj)
173
174 def response_add(self, request, obj):
35fb7bb plugins working again
patricklauber authored
175 """
176 Just set a flag, so we know something was changed, and can make
177 new version if reversion installed.
2db9000 Pugin inlines support for admin interface.
pcicman authored
178 New version will be created in admin.views.edit_plugin
b366204 jstree update
divio authored
179 """
2db9000 Pugin inlines support for admin interface.
pcicman authored
180 self.object_successfully_changed = True
181 return super(CMSPluginBase, self).response_add(request, obj)
a97dcf0 @fivethreeo removed logentrys for plugins
fivethreeo authored
182
183 def log_addition(self, request, object):
184 pass
185
186 def log_change(self, request, object, message):
187 pass
188
189 def log_deletion(self, request, object, object_repr):
190 pass
2db9000 Pugin inlines support for admin interface.
pcicman authored
191
c943283 @digi604 a lot of stuff
digi604 authored
192 def icon_src(self, instance):
193 """
dbcb370 fixed some popup/iframe closing bugs
divio authored
194 Overwrite this if text_enabled = True
35fb7bb plugins working again
patricklauber authored
195
c943283 @digi604 a lot of stuff
digi604 authored
196 Return the URL for an image to be used for an icon for this
197 plugin instance in a text editor.
198 """
dbcb370 fixed some popup/iframe closing bugs
divio authored
199 return ""
35fb7bb plugins working again
patricklauber authored
200
c943283 @digi604 a lot of stuff
digi604 authored
201 def icon_alt(self, instance):
202 """
dbcb370 fixed some popup/iframe closing bugs
divio authored
203 Overwrite this if necessary if text_enabled = True
c943283 @digi604 a lot of stuff
digi604 authored
204 Return the 'alt' text to be used for an icon representing
205 the plugin object in a text editor.
206 """
8ef9832 @digi604 text object insertion should be working now
digi604 authored
207 return "%s - %s" % (unicode(self.name), unicode(instance))
27b9af9 Basis of the plugin structure. Work in progress
divio authored
208
48e8f37 fixed some navigation issues with nav extenders
divio authored
209 def __repr__(self):
210 return smart_str(self.name)
211
212 def __unicode__(self):
04ea2ec @DrMeers Automatic CMSPluginBase fieldsets -- detect fields with 'advanced' at…
DrMeers authored
213 return self.name
8c61fe6 @ojii Removed the old media framework from CMSPluginBase and moved stuff fr…
ojii authored
214
215 #===========================================================================
216 # Deprecated APIs
217 #===========================================================================
218
219 @property
220 def pluginmedia(self):
221 raise Deprecated(
222 "CMSPluginBase.pluginmedia is deprecated in favor of django-sekizai"
223 )
224
225
226 def get_plugin_media(self, request, context, plugin):
227 raise Deprecated(
228 "CMSPluginBase.get_plugin_media is deprecated in favor of django-sekizai"
3ebfde4 @ojii Saner defaults for plugins
ojii authored
229 )
Something went wrong with that request. Please try again.