Creates db entries for undiscovered plugins to keep track of enabled/disabled plugins
()
| 43 | register_plugins() |
| 44 | |
| 45 | def sync_plugin_db(): |
| 46 | """ |
| 47 | Creates db entries for undiscovered plugins to keep track |
| 48 | of enabled/disabled plugins |
| 49 | """ |
| 50 | if settings.MIGRATING: return |
| 51 | |
| 52 | # Erase cache |
| 53 | clear_plugins_cache() |
| 54 | |
| 55 | db_plugins = Plugin.objects.all() |
| 56 | fs_plugins = get_plugins() |
| 57 | |
| 58 | # Remove plugins that are in the database but not on the file system |
| 59 | for db_plugin in db_plugins: |
| 60 | fs_found = next((fs_plugin for fs_plugin in fs_plugins if db_plugin.name == fs_plugin.get_name()), None) |
| 61 | if not fs_found: |
| 62 | Plugin.objects.filter(name=db_plugin.name).delete() |
| 63 | logger.info("Cleaned [{}] plugin from database (not found in file system)".format(db_plugin.name)) |
| 64 | |
| 65 | # Add plugins found in the file system, but not yet in the database |
| 66 | for plugin in get_plugins(): |
| 67 | # Plugins that have a "disabled" file are disabled |
| 68 | disabled_path = plugin.get_path("disabled") |
| 69 | disabled = os.path.isfile(disabled_path) |
| 70 | |
| 71 | _, created = Plugin.objects.get_or_create( |
| 72 | name=plugin.get_name(), |
| 73 | defaults={'enabled': not disabled}, |
| 74 | ) |
| 75 | if created: |
| 76 | logger.info("Added [{}] plugin to database".format(plugin)) |
| 77 | |
| 78 | |
| 79 | def clear_plugins_cache(): |