agateblue / django-dynamic-preferences

Dynamic global and instance settings for your django project
https://django-dynamic-preferences.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
350 stars 87 forks source link

Divide preferences by section in the admin interface - Signals problem #281

Closed CapAlex94 closed 2 years ago

CapAlex94 commented 2 years ago

Hi, first of all congratulations for the excellent extension!

I'm using the global_preference for different configurations inside my app, using the admin interface to allow users to update the configurations. I'm currently using 2 different section to define configurations. I would like to divide the "GLOBAL PREFERENCE" block by section in order to make more easily to update preferences of a specific section.

What i would like to create is something like the following structure:

global preferences _ Section 1 _ Conf 1 _ Conf 2
_ Section 2
_ Conf 1
_ Conf 2

How can I create this structure inside the admin interface?

Second problem:

I'm trying to use signals to perform action when a preference is updated. At the moment, I'm following the documentation and I have create the callback function inside an utils.py file and I have registered the signal receiver inside the apps.py file inside one of the application of my Django project. I have updated one of the preference available using the admin interface but my callback function is not called. Is that normal or I am missing something?

Thanks for the support!!

CapAlex94 commented 2 years ago

Hi it's possible to have an update about this topic? In particular about the signals when a settings is updated.

thanks

agateblue commented 2 years ago

Hi,

For the first item in your issue, you'd probably have to override the django admin templates, but I cannot provide any guidance for that, unfortunately.

I'm trying to use signals to perform action when a preference is updated. At the moment, I'm following the documentation and I have create the callback function inside an utils.py file and I have registered the signal receiver inside the apps.py file inside one of the application of my Django project. I have updated one of the preference available using the admin interface but my callback function is not called. Is that normal or I am missing something?

agateblue commented 2 years ago

(Sorry, I closed by mistake)

As for your second problem, can you please share the relevant code of each file?

CapAlex94 commented 2 years ago

I used the same code provided by you on the wiki page (https://django-dynamic-preferences.readthedocs.io/en/latest/react_to_updates.html), no other custom code was created as the basic one was not working correctly.

agateblue commented 2 years ago

Is the app config in question installed in your settings.INSTALLED_APPS? Can you try putting a raise or print('hello') statement in the ready() function to ensure it is loaded properly py Django?

CapAlex94 commented 2 years ago

Yes the apps.py is loaded by django on startup:

image

agateblue commented 2 years ago

Is the registering signals line coming from your code?

It would be easier for me (or anyone else) to help if you shared the relevant bits of code (settings.INSTALLED_APPS, apps.py, utils.py) and the layout of your project.

CapAlex94 commented 2 years ago

dashboard/apps.py

from django.apps import AppConfig
from dynamic_preferences.signals import preference_updated

from dashboard.utils import notify_on_preference_update

class DashboardConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'dashboard'

    def ready(self):
        print('Registering signals')
        preference_updated.connect(notify_on_preference_update)

Settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dynamic_preferences',
    'dashboard',

dashboard/utils.py

def notify_on_preference_update(sender, section, name, old_value, new_value, **kwargs):
    print("Preference {} in section {} changed from {} to {}".format(
        name, section, old_value, new_value))
agateblue commented 2 years ago

Thank you, I have reproduced it locally and published a fix. Can you please try the develop branch on your project and let me know if it works?

CapAlex94 commented 2 years ago

Yes, now it's working correctly.

Thanks!!

agateblue commented 2 years ago

Alright, I'll publish a release on PyPi tomorrow :)

agateblue commented 2 years ago

Release 1.14.0 including this fix was just published on PyPi https://pypi.org/project/django-dynamic-preferences/1.14.0/#history

CleitonDeLima commented 2 years ago

I think that in this update the preferences per instance was not considered :thinking: