diff --git a/README.rst b/README.rst index 73ab1d0..aaea8b6 100644 --- a/README.rst +++ b/README.rst @@ -4,6 +4,7 @@ Config Store - Stores configurations and are retrievable as a dictionary - Configurations are lazily loaded and are cached per request +- Configurations can have a Setup action to run one-time requests dependent on the configuration data - Configuration is defined as a django form Installation @@ -23,12 +24,19 @@ Define your configuration form somewhere:: from configstore.configs import ConfigurationInstance, register from configstore.forms import ConfigurationForm + + import logging class ExampleConfigurationForm(ConfigurationForm): amount = forms.DecimalField() message = forms.CharField() user = forms.ModelChoiceField(queryset=User.objects.all()) + @staticmethod + def config_task(configuration): + logging.info("You just ran the configuration action for %s!" % unicode(configuration.name) ) + return "Yay, you've accomplished nothing!" + Register the form:: complex_instance = ConfigurationInstance('example', 'Example Config', ExampleConfigurationForm) diff --git a/configstore/admin.py b/configstore/admin.py index 109cd2f..1b862a3 100644 --- a/configstore/admin.py +++ b/configstore/admin.py @@ -12,6 +12,14 @@ class ConfigurationAdmin(admin.ModelAdmin): list_display = ('name', 'key', 'site') + actions = ['run_setup'] + + def run_setup(self, request, queryset): + for item in queryset: + setup_task = CONFIGS.get(item.key).form.config_task + self.message_user(request, setup_task(item)) + run_setup.short_description = "Run the setup task for the configuration" + def get_fieldsets(self, request, obj=None): #consider it might be nice delegate more of this functionality to the ConfigurationInstance form_builder = self.get_form(request, obj) diff --git a/configstore/forms.py b/configstore/forms.py index e93de1c..e61061c 100644 --- a/configstore/forms.py +++ b/configstore/forms.py @@ -32,4 +32,13 @@ class Meta: model = Configuration fields = ['site'] + @staticmethod + def config_task(configuration): + """ + Params: + configuration - Instance of django-configstore.models.Configuration + Return: + Message to be echo'ed back to the user through "message_user". + """ + return "No task defined for %s." % configuration.name \ No newline at end of file