Show
Ignore:
Timestamp:
05/31/08 22:01:20 (22 months ago)
Author:
JensDiemer
Message:
  • add a few test for the new plugin models
  • unittest: test plugin would be copied to the normal plugins. So this plugin would be auto installed with all other plugins.
  • Bugfix in lucidTag.py: 1 should not be converted to True ;)
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/models/Plugin.py

    r1609 r1612  
    1919from pprint import pformat 
    2020 
     21from django.conf import settings 
     22 
    2123from django.db import models, transaction, connection 
    2224from django.core.cache import cache 
     
    3234preference_cache = {} 
    3335 
     36PLUGIN_MODEL_LABEL = "PyLucidPlugins" 
     37PLUGIN_MODEL_APP = "PyLucid.system.PyLucidPlugins" 
     38 
     39 
     40def get_plugin_models(package_name, plugin_name, debug=False): 
     41    """ 
     42    returns a list of all existing plugin models. 
     43    If no models exist, returns None! 
     44 
     45    Seperated function, because must be accessible from manager- and plugin 
     46    class, too. 
     47    """ 
     48    plugin_module = get_plugin_module(package_name, plugin_name, debug) 
     49    if not hasattr(plugin_module, "PLUGIN_MODELS"): 
     50        # Plugin has no models 
     51        return None 
     52 
     53    if not PLUGIN_MODEL_APP in settings.INSTALLED_APPS: 
     54        from django.core.exceptions import ImproperlyConfigured 
     55        raise ImproperlyConfigured( 
     56            "Error '%s' not in settings.INSTALLED_APPS!" % PLUGIN_MODEL_APP 
     57        ) 
     58 
     59    plugin_models = plugin_module.PLUGIN_MODELS 
     60 
     61    # Check app_label for every plugin model 
     62    for model in plugin_models: 
     63        app_label = model._meta.app_label 
     64        assert app_label == PLUGIN_MODEL_LABEL, ( 
     65            "Plugin models must defined in class Meta: app_label = '%s'" 
     66        ) % PLUGIN_MODEL_LABEL 
     67 
     68    return plugin_models 
     69 
     70 
     71 
     72 
     73 
    3474class PluginManager(models.Manager): 
    3575    """ 
     
    5191        plugin = self.get(plugin_name = plugin_name) 
    5292        return plugin.get_preferences() 
     93 
     94    def get_plugin_models(self, package_name, plugin_name, debug=False): 
     95        """ returns a list of all existing plugin models or None """ 
     96        return get_plugin_models(package_name, plugin_name, debug) 
     97 
    5398 
    5499 
     
    152197        return get_plugin_version(self.package_name, self.plugin_name, debug) 
    153198 
     199    def get_plugin_models(self, debug=False): 
     200        """ returns a list of all existing plugin models or None """ 
     201        return get_plugin_models(self.package_name, self.plugin_name, debug) 
     202 
    154203    #___________________________________________________________________________ 
    155204    # SAVE 
     
    173222 
    174223    def get_delete_sql(self, plugin_models): 
    175 #        from django.conf import settings 
     224        """ 
     225        Returns a list of sql statements for deleting the plugin model tabels. 
     226        For this, we used the fake django app "PyLucidPlugins" and attach 
     227        all models to this add temporarly. 
     228        """ 
     229        from django.core.management import sql 
    176230        from django.core.management.color import no_style 
    177231        style = no_style() 
    178         from django.core.management import sql 
    179232 
    180233        models.loading.register_models("PyLucidPlugins", *plugin_models) 
    181234 
    182         # get all delete statements for the given App 
    183235        app = models.get_app("PyLucidPlugins") 
     236 
    184237        statements = sql.sql_delete(app, style) 
    185238 
    186         print sql.table_list() 
    187  
    188         #cleanup 
     239        # cleanup 
    189240        app_models = models.loading.cache.app_models 
    190241        del(app_models["PyLucidPlugins"]) 
    191     #    settings.INSTALLED_APPS = old_inst_apps 
    192242 
    193243        return statements 
    194244 
    195     def get_delete_tables(self): 
    196         plugin_module = get_plugin_module( 
    197             self.package_name, 
    198             self.plugin_name, 
    199             debug=True, 
    200         ) 
    201         if not hasattr(plugin_module, "PLUGIN_MODELS"): 
     245    def _delete_tables(self): 
     246        """ 
     247        Delete all plugin model tabels. 
     248        """ 
     249        plugin_models = self.get_plugin_models() 
     250        if not plugin_models: 
    202251            # Plugin has no models 
    203252            return 
    204253 
    205         plugin_models = plugin_module.PLUGIN_MODELS 
    206  
    207254        statements = self.get_delete_sql(plugin_models) 
    208255 
    209256        cursor = connection.cursor() 
    210257        for statement in statements: 
    211             print repr(statement) 
     258            #print repr(statement) 
    212259            cursor.execute(statement) 
    213260 
     
    215262    @transaction.commit_on_success 
    216263    def delete(self): 
    217         self.get_delete_tables() 
     264        self._delete_tables() 
    218265 
    219266        super(Plugin, self).delete()