| | 24 | from PyLucid.models import Plugin |
| | 25 | from PyLucid.system.plugin_manager import get_plugin_list, install_plugin |
| | 26 | from PyLucid.system.plugin_import import get_plugin_config, get_plugin_version |
| | 27 | |
| | 28 | |
| | 29 | class MenuSection(object): |
| | 30 | """ |
| | 31 | Container for all entries in a admin sub menu section |
| | 32 | """ |
| | 33 | def __init__(self, page_msg, section_name, section_weight): |
| | 34 | self.page_msg = page_msg |
| | 35 | self.name = section_name |
| | 36 | self.weight = section_weight |
| | 37 | self.data = [] |
| | 38 | |
| | 39 | def add_method(self, menu_data): |
| | 40 | self.data.append(menu_data) |
| | 41 | |
| | 42 | def __iter__(self): |
| | 43 | """ |
| | 44 | for django template engine |
| | 45 | """ |
| | 46 | data = sorted(self.data, key=lambda x: x.get('weight',0)) |
| | 47 | for entry in data: |
| | 48 | yield entry |
| | 49 | |
| | 50 | def __repr__(self): |
| | 51 | return repr(self.data) |
| | 52 | |
| | 53 | |
| | 54 | class Menu(object): |
| | 55 | """ |
| | 56 | Container for the complete admin sub menu data |
| | 57 | """ |
| | 58 | def __init__(self, page_msg, section_weights): |
| | 59 | self.page_msg = page_msg |
| | 60 | self.section_weights = section_weights |
| | 61 | self.data = {} |
| | 62 | |
| | 63 | def add_entry(self, section_name, menu_data): |
| | 64 | if section_name not in self.data: |
| | 65 | # Create new section |
| | 66 | weights = self.section_weights |
| | 67 | section_weight = weights.get(section_name, 0) |
| | 68 | self.data[section_name] = MenuSection( |
| | 69 | self.page_msg, section_name, section_weight |
| | 70 | ) |
| | 71 | |
| | 72 | section = self.data[section_name] |
| | 73 | section.add_method(menu_data) |
| | 74 | |
| | 75 | def __iter__(self): |
| | 76 | """ |
| | 77 | for django template engine |
| | 78 | """ |
| | 79 | sections = self.data.keys() |
| | 80 | weights = self.section_weights |
| | 81 | |
| | 82 | # sort in two steps: |
| | 83 | sections = [(weights.get(s, 0), s) for s in sections] |
| | 84 | sections = [x[1] for x in sorted(sections)] |
| | 85 | |
| | 86 | # FIXME: sorting in one step, but it doesn't work, why? |
| | 87 | # sections = sorted(sections, key=lambda x: weights.get(x, 0)) |
| | 88 | |
| | 89 | for section in sections: |
| | 90 | yield self.data[section] |
| | 91 | |
| | 92 | def debug(self): |
| | 93 | """ |
| | 94 | Display all data |
| | 95 | """ |
| | 96 | self.page_msg("Menu debug:") |
| | 97 | self.page_msg(self.section_weights) |
| | 98 | self.page_msg(self.data) |
| | 99 | |
| 59 | | |
| 60 | | |
| | 142 | def _add_static_entries(self, menu): |
| | 143 | """ |
| | 144 | Adds some static sub menu entries to the django admin panel |
| | 145 | TODO: Make this dynamic changeable |
| | 146 | """ |
| | 147 | if not self.request.user.is_staff: |
| | 148 | # All entries here are in the django admin panel |
| | 149 | # Skip all links, if the current user can't use it |
| | 150 | return |
| | 151 | |
| | 152 | page_obj = self.context["PAGE"] |
| | 153 | |
| | 154 | def add_entry(section, link, title, help_text="", weight=0): |
| | 155 | menu.add_entry( |
| | 156 | section, |
| | 157 | menu_data = { |
| | 158 | #"section" : section, |
| | 159 | "link" : link, |
| | 160 | "title" : title, |
| | 161 | "help_text" : help_text, |
| | 162 | "open_in_window": True, |
| | 163 | "weight" : weight, |
| | 164 | }, |
| | 165 | ) |
| | 166 | |
| | 167 | section = _("edit look") |
| | 168 | |
| | 169 | # STYLE |
| | 170 | add_entry(section, |
| | 171 | self.URLs.adminLink("PyLucid/style/%s" % page_obj.style.id), |
| | 172 | _("edit '%s' stylesheet") % page_obj.style, |
| | 173 | _("The current used stylesheet."), |
| | 174 | -5, |
| | 175 | ) |
| | 176 | add_entry(section, |
| | 177 | self.URLs.adminLink("PyLucid/style"), |
| | 178 | _("edit all stylesheets"), |
| | 179 | "You get a list of all existing stylesheets.", |
| | 180 | -4, |
| | 181 | ) |
| | 182 | |
| | 183 | # TEMPLATE |
| | 184 | add_entry(section, |
| | 185 | self.URLs.adminLink("PyLucid/template"), |
| | 186 | _("edit all templates"), |
| | 187 | "You get a list of all existing templates.", |
| | 188 | 8, |
| | 189 | ) |
| | 190 | add_entry(section, |
| | 191 | self.URLs.adminLink("PyLucid/template/%s" % page_obj.template.id), |
| | 192 | _("edit '%s' template") % page_obj.template, |
| | 193 | _("The current used template."), |
| | 194 | 5, |
| | 195 | ) |
| | 196 | |
| | 197 | section = _("user management") |
| | 198 | |
| | 199 | add_entry(section, |
| | 200 | self.URLs.adminLink("auth/user"), |
| | 201 | _("edit users"), |
| | 202 | "Edit all existing users.", |
| | 203 | 5, |
| | 204 | ) |
| | 205 | add_entry(section, |
| | 206 | self.URLs.adminLink("auth/group"), |
| | 207 | _("edit user groups"), |
| | 208 | "Edit all existing users.", |
| | 209 | 5, |
| | 210 | ) |
| | 211 | |
| | 212 | |
| | 213 | def _generate_menu(self): |
| | 214 | """ |
| | 215 | Generate the dynamic admin sub menu |
| | 216 | """ |
| | 217 | # Get the preferences from the database: |
| | 218 | preferences = self.get_preferences() |
| | 219 | if preferences == None: |
| | 220 | # preferences not in database -> reinit required |
| | 221 | if self.request.debug == True: |
| | 222 | msg = ( |
| | 223 | '<a href="http://www.pylucid.org/_goto/121/changes/">' |
| | 224 | 'reinit "admin_menu" plugin required!</a>' |
| | 225 | ) |
| | 226 | self.page_msg.red(mark_safe(msg)) |
| | 227 | section_weights = [] |
| | 228 | else: |
| | 229 | # Sort the sections with the weight information from the preferences |
| | 230 | section_weights = preferences["section_weights"] |
| | 231 | |
| | 232 | # All installed + active plugins |
| | 233 | plugins = Plugin.objects.all().filter(active = True).order_by( |
| | 234 | 'package_name', 'plugin_name' |
| | 235 | ) |
| | 236 | |
| | 237 | menu = Menu(self.page_msg, section_weights) |
| | 238 | |
| | 239 | # Get the plugin config and build the menu data |
| | 240 | for plugin in plugins: |
| | 241 | config = get_plugin_config( |
| | 242 | package_name = plugin.package_name, |
| | 243 | plugin_name = plugin.plugin_name, |
| | 244 | debug = False, |
| | 245 | ) |
| | 246 | |
| | 247 | for method, data in config.plugin_manager_data.iteritems(): |
| | 248 | if "admin_sub_menu" not in data: |
| | 249 | # This method should not listed into the admin sub menu |
| | 250 | continue |
| | 251 | |
| | 252 | menu_data = data["admin_sub_menu"] |
| | 253 | section = unicode(menu_data["section"]) # translate gettext_lazy |
| | 254 | |
| | 255 | # Add the _command link to the menu data |
| | 256 | link = self.URLs.commandLink( |
| | 257 | plugin_name = plugin.plugin_name, |
| | 258 | method_name = method, |
| | 259 | ) |
| | 260 | menu_data["link"] = link |
| | 261 | |
| | 262 | menu.add_entry(section, menu_data) |
| | 263 | |
| | 264 | return menu |