root/trunk/pylucid_project/doc/plugin_api.py

Revision 1767, 6.4 KB (checked in by JensDiemer, 18 months ago)

not self._error() it's self.error() ;)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author
Line 
1# -*- coding: utf-8 -*-
2
3"""
4    PyLucid Plugin API example
5    ~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7    Pseudocode!
8
9    Last commit info:
10    ~~~~~~~~~~~~~~~~~
11    $LastChangedDate$
12    $Rev$
13    $Author$
14
15    :copyleft: 2008 by the PyLucid team, see AUTHORS for more details.
16    :license: GNU GPL v3 or above, see LICENSE for more details.
17"""
18
19from PyLucid.system.BasePlugin import PyLucidBasePlugin
20
21class ExamplePlugin(PyLucidBasePlugin):
22    """
23    We inherit from the base plugin.
24    """
25    def global_objects(self):
26        """
27        A list of all global plugin objects how inherit from the base plugin.
28        """
29        # The plugin name as a String:
30        self.plugin_name
31
32        # Interface to the internal pages (only for special needs)
33        # PyLucid.system.internal_page.InternalPage()
34        self.internal_page
35
36        # The global context
37        self.context
38
39        # A response object (only for special needs)
40        self.response
41
42        # The django request object
43        # see http://www.djangoproject.com/documentation/request_response/
44        self.request
45
46        # Interface to the PyLucid page message system
47        self.page_msg
48
49        # object to build URLs:
50        # comes from PyLucid.system.URLs
51        self.URLs
52
53        # database instance of the cms page object:
54        self.current_page
55
56    #__________________________________________________________________________
57    # URLs
58    #
59    # How to build urls
60    #
61    def building_urls(self):
62        """
63        You should never building urls "by hand". You should use URLs!
64        In the most case you need only methodLink() and commandLink().
65
66        In special cases you can some other methods. Please look into the
67        source code PyLucid.system.URLs
68        """
69        # mostly you will create a link to a other method of the current plugin
70        # Use methodLink() for this:
71        link = self.URLs.methodLink("method_name")
72
73        # In special cases you would like to insert a link to a other plugin.
74        # Use commandLink() for this:
75        link = self.URLs.commandLink("page_admin", "edit_page")
76
77        # The URLs object is a dictionary like object. You can display the
78        # content of it into the page_msg with:
79        self.URLs.debug()
80
81
82    #__________________________________________________________________________
83    # OUTPUT
84    #
85    # How a Plugin can create a response
86    #
87    def render_a_internal_page(self):
88        """
89        Normal way to create output into the content area with a existing
90        internal page.
91
92        See also: http://pylucid.org/_goto/142/internal-pages/
93        """
94        # Example:
95        context = {
96            # Building a template context for the internal page
97            "key": value,
98            "Foo": Bar,
99        }
100        # Render the template with the context and write the result into the
101        # response object:
102        self._render_template("InternalPageName", context)
103
104    def page_msg(self):
105        """
106        With the global self.page_msg object a plugin can easy send short
107        messages to the user.
108        You can call page_msg several times and everywhere.
109        All messages would be inserted in the generated page.
110
111        Note:
112            The page message object is good for debugging. List and Dictioaries
113            would be formatted with pprint.
114
115        More info in PyLucid.system.page_msg
116        """
117        # Examples:
118
119        # Normal ouput in blue color
120        self.page_msg("The shorted way...")
121
122        # A error in red color
123        self.page_msg.red("Error!")
124
125        # A black colored messages:
126        self.page_msg.black("I am black")
127
128        # Successfull messages should be geen:
129        self.page_msg.green("Update XY successfull")
130
131    def return_a_response(self):
132        """
133        A Plugin method can return a django HttpResponse object directy back.
134        e.g. send files to the client or a Redirect
135        """
136        # send the XML file to the client
137        from django.http import HttpResponse
138        response = HttpResponse()
139        response['Content-Type'] = 'application/xml; charset=utf-8'
140        response.write(FooBar)
141        return response
142
143    def change_page_title(self):
144        """
145        If a plugin creates his own response page, it should be change the
146        current displayed page title, so it makes more sense for the user.
147        """
148        self.context["PAGE"].title = "The Plugin Title"
149
150    #__________________________________________________________________________
151    # PREFERENCES
152    #
153    def get_my_preferences(self):
154        """
155        more info about plugin preferences:
156            http://www.pylucid.org/_goto/153/preferences/
157        """
158        # Get the preferences from the database:
159        preferences = self.get_preferences()
160
161        # Get a entry (It's a dict api)
162        my_entry_value = preferences["entry_key"]
163
164    def change_preference_entry(self):
165        """
166        A plugin can change a preference entry with self.set_preferences()
167        """
168        new_preference_dict = self.set_preferences("key", "new_value")
169
170        self.page_msg("Changed value:", new_preference_dict["key"])
171
172    #__________________________________________________________________________
173    # ENVIRONMENT
174    #
175    # Some needfull information
176    #
177    def on_request_object(self):
178        """
179        Some needfull objects from the request object.
180        """
181        # Is debug on? If you will display information only if debug is on, you
182        # should not use settings.DEBUG directly!
183        # Use request.debug, because it is true if settings.DEBUG if true *or*
184        # if the client IP is in settings.INTERNAL_IPS
185        # This is append to the request object in
186        # PyLucid.system.utils.setup_debug
187        if self.request.debug:
188            self.page_msg("Debug is on.")
189
190    #__________________________________________________________________________
191    # UTILS
192    #
193    # Some needfull methods
194    #
195    def simple_error_feedback(self):
196        """
197        with self.error() you can easy create a "abort" error messages.
198        The goal is: The method gets two messages types:
199            public_msg, debug_msg
200        The public messages would be always displayed. The debug messages would
201        be append if self.request.debug is on.
202        So you can easy gives feedback for anonymous users and developers, too.
203        """
204        try:
205            print "...do something..."
206        except Exception, err:
207            return self.error(_("Wrong URL."), err)
Note: See TracBrowser for help on using the browser.