| 47 | | #______________________________________________________________________________ |
| 48 | | # Escape TextFields |
| 49 | | # http://groups.google.com/group/django-users/browse_thread/thread/d4d9a8c5e7019762 |
| 50 | | |
| 51 | | class EscapedTextarea(forms.Textarea): |
| 52 | | def render(self, name, value, attrs=None): |
| 53 | | """ |
| 54 | | -Escape/Quote the django template tags chars "{" and "}" to the |
| 55 | | HTML character entity. |
| 56 | | -Override the default textarea attributes and make it bigger |
| 57 | | Node: The cols size are setup with CSS and "width:100%;" |
| 58 | | """ |
| 59 | | attrs = {'rows': '15'} |
| 60 | | content = super(EscapedTextarea, self).render(name, value, attrs) |
| 61 | | # content = content.replace("{", "{").replace("}", "}") |
| 62 | | # content = mark_safe(content) # turn djngo auto-escaping off |
| 63 | | return content |
| 64 | | |
| 65 | | class EscapedTextField(forms.Field): |
| 66 | | "Change the textarea widget" |
| 67 | | widget = EscapedTextarea |
| 68 | | |
| 69 | | class ParentMultipleChoiceField(forms.ChoiceField): |
| 70 | | """ |
| 71 | | Change the "parent" choice field with a verbose name tree list. |
| 72 | | TODO: We must ask the database a second time :( |
| 73 | | """ |
| 74 | | def __init__(self, *args, **kwargs): |
| 75 | | super(ParentMultipleChoiceField, self).__init__(*args, **kwargs) |
| 76 | | |
| 77 | | page_list = flat_tree_list() |
| 78 | | choices = [(None, "---[root]---")] |
| 79 | | for page in page_list: |
| 80 | | choices.append((page["id"], page["level_name"])) |
| 81 | | |
| 82 | | self.choices = choices |
| 83 | | |
| 84 | | def clean(self, value): |
| 85 | | """ |
| 86 | | TODO: We should check if the parent_id is ok and not make a wrong |
| 87 | | id-parent-loop. Now, this is checkt in PyLucid.models.Page.save() |
| 88 | | """ |
| 89 | | if value == "None": |
| | 48 | |
| | 49 | class ParentChoiceField(forms.IntegerField): |
| | 50 | def clean(self, parent_id): |
| | 51 | """ |
| | 52 | returns the parent page instance. |
| | 53 | Note: |
| | 54 | In PyLucid.models.Page.save() it would be checkt if the selected |
| | 55 | parent page is logical valid. Here we check only, if the page with |
| | 56 | the given ID exists. |
| | 57 | """ |
| | 58 | # let convert the string into a integer: |
| | 59 | parent_id = super(ParentChoiceField, self).clean(parent_id) |
| | 60 | assert isinstance(parent_id, int) |
| | 61 | |
| | 62 | if parent_id == 0: |
| 105 | | if field.name == "content": |
| 106 | | # replace the content field |
| 107 | | return EscapedTextField(**kwargs) |
| 108 | | elif field.name == "parent": |
| 109 | | # replace the parent field |
| 110 | | return ParentMultipleChoiceField(**kwargs) |
| 111 | | else: |
| 112 | | # Do nothing with the other fields: |
| 113 | | return field.formfield(**kwargs) |
| | 78 | page_list = flat_tree_list() |
| | 79 | choices = [(0, "---[root]---")] |
| | 80 | for page in page_list: |
| | 81 | choices.append((page["id"], page["level_name"])) |
| | 82 | return choices |
| | 83 | |
| | 84 | class EditPageForm(forms.Form): |
| | 85 | """ |
| | 86 | Form for editing a cms page. |
| | 87 | """ |
| | 88 | edit_comment = forms.CharField( |
| | 89 | max_length=255, required=False, |
| | 90 | help_text=_("The reason for editing."), |
| | 91 | widget=forms.TextInput(attrs={'class':'bigger'}), |
| | 92 | ) |
| | 93 | |
| | 94 | content = forms.CharField( |
| | 95 | widget=forms.Textarea(attrs={'rows': '15'}), |
| | 96 | ) |
| | 97 | |
| | 98 | parent = ParentChoiceField( |
| | 99 | widget=forms.Select(choices=get_parent_choices()), |
| | 100 | # FIXME: How to set invalid_choice here? |
| | 101 | help_text="the higher-ranking father page", |
| | 102 | ) |
| | 103 | |
| | 104 | name = forms.CharField( |
| | 105 | max_length=255, help_text=_("A short page name"), |
| | 106 | ) |
| | 107 | title = forms.CharField( |
| | 108 | max_length=255, required=False, help_text=_("A long page title"), |
| | 109 | ) |
| | 110 | markup = forms.IntegerField( |
| | 111 | widget=forms.Select(choices=MARKUPS), |
| | 112 | help_text=_("the used markup language for this page"), |
| | 113 | ) |
| | 114 | |
| | 115 | keywords = forms.CharField( |
| | 116 | max_length=255, required=False, |
| | 117 | help_text=_("Keywords for the html header. (separated by commas)"), |
| | 118 | widget=forms.TextInput(attrs={'class':'bigger'}), |
| | 119 | ) |
| | 120 | description = forms.CharField( |
| | 121 | max_length=255, required=False, |
| | 122 | help_text=_("Short description of the contents. (for the html header)"), |
| | 123 | widget=forms.TextInput(attrs={'class':'bigger'}), |
| | 124 | ) |
| | 183 | def _save_edited_page(self, page_instance, html_form): |
| | 184 | """ |
| | 185 | Save a edited page into the database. |
| | 186 | |
| | 187 | if a old page was edited: |
| | 188 | -Archive the old page data |
| | 189 | if a new page was created: |
| | 190 | -return the new page content for rendering |
| | 191 | """ |
| | 192 | if page_instance.id == self.current_page.id: |
| | 193 | # A existing page was edited |
| | 194 | edit_comment = html_form.cleaned_data["edit_comment"] |
| | 195 | # achive the old page data: |
| | 196 | archive_page(self.current_page, edit_comment) |
| | 197 | self.page_msg(_("Old page data archived.")) |
| | 198 | |
| | 199 | # Transfer the form values into the page instance |
| | 200 | for key, value in html_form.cleaned_data.iteritems(): |
| | 201 | if key != "edit_comment": # The comment is only for the page archiv |
| | 202 | setattr(page_instance, key, value) |
| | 203 | |
| | 204 | try: |
| | 205 | page_instance.save() |
| | 206 | except Exception, msg: |
| | 207 | self.page_msg("Can't save the page data:", msg) |
| | 208 | return |
| | 209 | |
| | 210 | # Delete the old page data cache: |
| | 211 | self._delete_cache(page_instance) |
| | 212 | |
| | 213 | if page_instance.id == self.current_page.id: |
| | 214 | # Normal page edit |
| | 215 | self.page_msg(_("Page data updated.")) |
| | 216 | else: |
| | 217 | self.page_msg(_("The new page created.")) |
| | 218 | |
| | 219 | # refresh the current page data: |
| | 220 | self._refresh_curent_page(page_instance) |
| | 221 | |
| | 222 | # return the new page content for rendering |
| | 223 | return self.current_page.content |
| | 224 | |
| 199 | | # FIXME: Quick hack 'escape' Template String. |
| 200 | | # With the formfield_callback we switched the widget render method |
| 201 | | # and escape/quote the characters "{" and "}" so they are invisible to |
| 202 | | # the django template engine and the tag (not the result of the tag) is |
| 203 | | # editable ;) |
| 204 | | # http://www.djangoproject.com/documentation/newforms/#overriding-the-default-field-types |
| 205 | | # -With the formfield_callback we change the parent field, too. |
| 206 | | PageForm = forms.models.form_for_instance( |
| 207 | | page_instance, fields=( |
| 208 | | "content", "parent", |
| 209 | | "name", "title", |
| 210 | | "keywords", "description", "markup", |
| 211 | | ), |
| 212 | | formfield_callback=formfield_callback |
| 213 | | ) |
| 214 | | |
| 215 | | if self.request.method == 'POST': |
| 216 | | # self.page_msg(self.request.POST) |
| 217 | | html_form = PageForm(self.request.POST) |
| | 252 | # The markup is a little dynamicly. It should always be used the current |
| | 253 | # markup editor (html with or without TinyMCE) e.g. The user change the |
| | 254 | # markup and used the "preview" fuction. |
| | 255 | current_markup = page_instance.markup |
| | 256 | |
| | 257 | if self.request.method != 'POST': |
| | 258 | parent = getattr(page_instance.parent, "id", 0) # Root is None |
| | 259 | |
| | 260 | html_form = EditPageForm({ |
| | 261 | "content": page_instance.content, |
| | 262 | "parent": parent, |
| | 263 | "name": page_instance.name, |
| | 264 | "title": page_instance.title, |
| | 265 | "markup": current_markup, |
| | 266 | "keywords": page_instance.keywords, |
| | 267 | "description": page_instance.description |
| | 268 | }) |
| | 269 | else: # POST |
| | 270 | #self.page_msg(self.request.POST) |
| | 271 | html_form = EditPageForm(self.request.POST) |
| 229 | | # FIXME: How to transfer the attributes easier? |
| 230 | | old_page = PageArchiv( |
| 231 | | content = self.current_page.content, |
| 232 | | parent = self.current_page.parent, |
| 233 | | position = self.current_page.position, |
| 234 | | name = self.current_page.name, |
| 235 | | shortcut = self.current_page.shortcut, |
| 236 | | title = self.current_page.title, |
| 237 | | template = self.current_page.template, |
| 238 | | style = self.current_page.style, |
| 239 | | markup = self.current_page.markup, |
| 240 | | keywords = self.current_page.keywords, |
| 241 | | description = self.current_page.description, |
| 242 | | createtime = self.current_page.createtime, |
| 243 | | lastupdatetime = self.current_page.lastupdatetime, |
| 244 | | createby = self.current_page.createby, |
| 245 | | lastupdateby = self.current_page.lastupdateby, |
| 246 | | showlinks = self.current_page.showlinks, |
| 247 | | permitViewPublic = self.current_page.permitViewPublic, |
| 248 | | permitViewGroup = self.current_page.permitViewGroup, |
| 249 | | permitEditGroup = self.current_page.permitEditGroup, |
| 250 | | |
| 251 | | original = self.current_page |
| 252 | | ) |
| 253 | | old_page.save() |
| 254 | | self.page_msg(_("Old page data archived.")) |
| 255 | | |
| 256 | | old_content = self.current_page.content |
| 257 | | # Save the new page data into the database: |
| 258 | | try: |
| 259 | | html_form.save() |
| 260 | | except Exception, msg: |
| 261 | | self.page_msg("Can't save the page data:", msg) |
| 262 | | else: |
| 263 | | # Delete the old page data cache: |
| 264 | | self._delete_cache(page_instance) |
| 265 | | |
| 266 | | if page_instance.id == self.current_page.id: |
| 267 | | # Normal page edit |
| 268 | | self.page_msg(_("Page data updated.")) |
| 269 | | return |
| 270 | | else: |
| 271 | | self.page_msg(_("The new page created.")) |
| 272 | | |
| 273 | | # refresh the current page data: |
| 274 | | self._refresh_curent_page(page_instance) |
| 275 | | |
| 276 | | # return the new page content for rendering |
| 277 | | return self.current_page.content |
| | 287 | # Save the new page data. returns a new page instance |
| | 288 | return self._save_edited_page(page_instance, html_form) |
| 305 | | js_data = ( |
| 306 | | '<script language="javascript" type="text/javascript"' |
| 307 | | ' src="%stiny_mce/tiny_mce.js"></script>\n' |
| 308 | | '<script language="javascript" type="text/javascript">\n' |
| 309 | | ' init_tinyMCE();\n' |
| 310 | | '</script>\n' |
| 311 | | ) % media_url |
| 312 | | context["tinymce"] = mark_safe(js_data) |
| | 317 | # url to e.g. .../internal_page/page_admin/edit_page_tinymce.js |
| | 318 | use_tiny_mce_url = self.internal_page.get_url( |
| | 319 | "edit_page_tinymce", slug="js" |
| | 320 | ) |
| | 321 | # Add external media files |
| | 322 | for url in (tiny_mce_url, use_tiny_mce_url): |
| | 323 | # Add tiny_mce.js to |
| | 324 | self.context["js_data"].append({ |
| | 325 | "plugin_name": self.plugin_name, |
| | 326 | "url": url, |
| | 327 | }) |