agateblue / agateblue/django-dynamic-preferences

Add settings option to not save default preferences on preference form initialization

Đang mở
#155 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
359
Fork
86
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I am using v1.6 of django-dynamic-preferences, and am using it mainly for user preferences in forms for other models. I noticed whenever a new UserPreferenceForm is initialized for each user, the default is saved in the database. That's fine for a few preferences and a few users, but if there's several preferences and several users and most users always use the default value, then the database entries for the user preferences becomes mostly defaults. This eats up database space, and can make row retrieval slow because the database has to search over many entries.

Could a setting be added to specify whether to always save a preference instance to the database on form initialization or just produce an instance that isn't saved to the database on form initialization? The reason why is because in order to do this currently, some of the PreferencesManager class' functions have to be overwritten. However, there is only one manager to one registry, So I cant tie in another PreferenceManager to the user registry, and I'd rather not re-write everything to point to another manager and registry when I really just want to use the user registry.

The first class and functions that would need to be updated would be the PreferencesManager class, and the functions would be get_db_pref and create_db_pref:

def get_db_pref(self, section, name):
try:
pref = self.queryset.get(section=section, name=name)

except self.model.DoesNotExist:
pref_obj = self.pref_obj(section=section, name=name)
pref = self.create_db_pref(
section=section, name=name, value=pref_obj.get('default'), settings.SAVE_DEFAULTS)

return pref

def create_db_pref(self, section, name, value, save_on_create=True):
kwargs = {
'section': section,
'name': name,
}
if self.instance:
kwargs['instance'] = self.instance

# this is a just a shortcut to get the raw, serialized value
# so we can pass it to get_or_create
m = self.model(**kwargs)
m.value = value
raw_value = m.raw_value

if save_on_create:
db_pref, created = self.model.objects.get_or_create(**kwargs)
if created and db_pref.raw_value != raw_value:
db_pref.raw_value = raw_value
db_pref.save()
else:
m.raw_value = raw_value
db_pref = m

return db_pref

The other class and function would be the PreferenceForm and the update_preferences function to check if an instances value is the same as the default or if the SAVE_DEFAULTS flag is on. If the instance value isn't the same as the preference default, then don't save it.If the SAVE_DEFAULTS flag is true, then always save the instance no matter what:

def update_preferences(self, **kwargs):
for instance in self.instances:
instance.value = self.cleaned_data[
instance.preference.identifier()]
if ( settings.SAVE_DEFAULTS or
instance.value != instance.preference.get('default')
):
instance.save()
elif ( not settings.SAVE_DEFAULTS and
instance.value == instance.preferences.get('default') and
instance.pk
):
instance.delete()

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.