| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Preferences |
|---|
| 5 | ~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | A low level editor for the preferences. |
|---|
| 8 | Using data_eval and pprint. Supports only Constants, Dicts, Lists, Tuples. |
|---|
| 9 | |
|---|
| 10 | Last commit info: |
|---|
| 11 | ~~~~~~~~~~~~~~~~~ |
|---|
| 12 | $LastChangedDate$ |
|---|
| 13 | $Rev:1549 $ |
|---|
| 14 | $Author:JensDiemer $ |
|---|
| 15 | |
|---|
| 16 | :copyleft: 2008 by the PyLucid team, see AUTHORS for more details. |
|---|
| 17 | :license: GNU GPL v3 or above, see LICENSE for more details. |
|---|
| 18 | """ |
|---|
| 19 | |
|---|
| 20 | __version__= "$Rev: $" |
|---|
| 21 | |
|---|
| 22 | from pprint import pformat |
|---|
| 23 | |
|---|
| 24 | from django import forms |
|---|
| 25 | from django.forms import ValidationError |
|---|
| 26 | from django.utils.translation import ugettext as _ |
|---|
| 27 | |
|---|
| 28 | from PyLucid.system.BasePlugin import PyLucidBasePlugin |
|---|
| 29 | from PyLucid.models import Plugin, Preference |
|---|
| 30 | |
|---|
| 31 | class CommentForm(forms.ModelForm): |
|---|
| 32 | class Meta: |
|---|
| 33 | model = Preference |
|---|
| 34 | fields = ('comment',) |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | class preference_editor(PyLucidBasePlugin): |
|---|
| 38 | |
|---|
| 39 | def _vebose_plugin_name(self, pref): |
|---|
| 40 | return pref.plugin.plugin_name.replace("_", " ") |
|---|
| 41 | |
|---|
| 42 | def select(self): |
|---|
| 43 | """ |
|---|
| 44 | Display the sub menu |
|---|
| 45 | """ |
|---|
| 46 | plugins = Plugin.objects.exclude(default_pref__isnull=True) |
|---|
| 47 | |
|---|
| 48 | context = { |
|---|
| 49 | "plugins": plugins, |
|---|
| 50 | "edit_link": self.URLs.methodLink("edit"), |
|---|
| 51 | "add_link": self.URLs.methodLink("add"), |
|---|
| 52 | "admin_link": self.URLs.adminLink("PyLucid/preference"), |
|---|
| 53 | } |
|---|
| 54 | self._render_template("select", context)#, debug=True) |
|---|
| 55 | |
|---|
| 56 | def edit(self, url_args): |
|---|
| 57 | try: |
|---|
| 58 | url_args = url_args.strip("/") |
|---|
| 59 | pref_id = int(url_args) |
|---|
| 60 | except Exception, e: |
|---|
| 61 | self.page_msg.red("url error:", e) |
|---|
| 62 | return |
|---|
| 63 | |
|---|
| 64 | pref = Preference.objects.get(id = pref_id) |
|---|
| 65 | plugin = pref.plugin |
|---|
| 66 | unbound_form = plugin.get_pref_form(self.page_msg, self.request.debug) |
|---|
| 67 | |
|---|
| 68 | if self.request.method == 'POST': |
|---|
| 69 | form = unbound_form(self.request.POST) |
|---|
| 70 | if form.is_valid(): |
|---|
| 71 | new_data_dict = form.cleaned_data |
|---|
| 72 | |
|---|
| 73 | pref.set_data(new_data_dict, self.request.user) |
|---|
| 74 | pref.save() |
|---|
| 75 | self.page_msg("New preferences saved.") |
|---|
| 76 | return self.select() # Display the menu |
|---|
| 77 | else: |
|---|
| 78 | data_dict = pref.get_data() |
|---|
| 79 | form = unbound_form(data_dict) |
|---|
| 80 | |
|---|
| 81 | context = { |
|---|
| 82 | "plugin_name": unicode(plugin), |
|---|
| 83 | "form": form, |
|---|
| 84 | "url_abort": self.URLs.methodLink("select"), |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | # Insert DocString from preferences form, if exist |
|---|
| 88 | raw_doc = unbound_form.__doc__ |
|---|
| 89 | if raw_doc: |
|---|
| 90 | doc = raw_doc.strip().splitlines() |
|---|
| 91 | context["doc"] = "\n".join([line.strip() for line in doc if line]) |
|---|
| 92 | |
|---|
| 93 | self._render_template("edit_form", context)#, debug=True) |
|---|
| 94 | |
|---|
| 95 | def add(self, url_args): |
|---|
| 96 | try: |
|---|
| 97 | url_args = url_args.strip("/") |
|---|
| 98 | plugin_id = int(url_args) |
|---|
| 99 | except Exception, e: |
|---|
| 100 | self.page_msg.red("url error:", e) |
|---|
| 101 | return |
|---|
| 102 | |
|---|
| 103 | plugin = Plugin.objects.get(id = plugin_id) |
|---|
| 104 | |
|---|
| 105 | if self.request.method == 'POST': |
|---|
| 106 | form = CommentForm(self.request.POST) |
|---|
| 107 | if form.is_valid(): |
|---|
| 108 | default_pref_data = plugin.default_pref.get_data() |
|---|
| 109 | new_pref = plugin.add_preference( |
|---|
| 110 | comment = form.cleaned_data["comment"], |
|---|
| 111 | data = default_pref_data, |
|---|
| 112 | user = self.request.user, |
|---|
| 113 | ) |
|---|
| 114 | self.page_msg("New preferences added.") |
|---|
| 115 | return self.select() # Display the menu |
|---|
| 116 | else: |
|---|
| 117 | form = CommentForm()#instance=new_pref) |
|---|
| 118 | |
|---|
| 119 | context = { |
|---|
| 120 | "plugin_name": unicode(plugin), |
|---|
| 121 | "form": form, |
|---|
| 122 | "url_abort": self.URLs.methodLink("select"), |
|---|
| 123 | } |
|---|
| 124 | self._render_template("edit_form", context)#, debug=True) |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|