<s> """<STR_LIT>""" <EOL> from django . db import models <EOL> from django . db . models . query import QuerySet <EOL> from django . conf import settings <EOL> from django . utils . functional import cached_property <EOL> from dynamic_preferences . registries import preference_models , user_preferences_registry , global_preferences_registry <EOL> from . utils import update <EOL> class BasePreferenceModel ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> section = models . CharField ( <EOL> max_length = <NUM_LIT> , db_index = True , blank = True , null = True , default = None ) <EOL> name = models . CharField ( max_length = <NUM_LIT> , db_index = True ) <EOL> raw_value = models . TextField ( null = True , blank = True ) <EOL> class Meta : <EOL> abstract = True <EOL> app_label = '<STR_LIT>' <EOL> @ cached_property <EOL> def preference ( self ) : <EOL> return self . registry . get ( section = self . section , name = self . name ) <EOL> @ property <EOL> def verbose_name ( self ) : <EOL> return self . preference . get ( '<STR_LIT>' , self . preference . identifier ) <EOL> @ property <EOL> def help_text ( self ) : <EOL> return self . preference . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def set_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . raw_value = self . preference . serializer . serialize ( value ) <EOL> def get_value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . preference . serializer . deserialize ( self . raw_value ) <EOL> value = property ( get_value , set_value ) <EOL> def save ( self , ** kwargs ) : <EOL> if self . pk is None and not self . raw_value : <EOL> self . value = self . preference . default <EOL> super ( BasePreferenceModel , self ) . save ( ** kwargs ) <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , self . section , self . name ) <EOL> class GlobalPreferenceModel ( BasePreferenceModel ) : <EOL> registry = global_preferences_registry <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = "<STR_LIT>" <EOL> verbose_name_plural = "<STR_LIT>" <EOL> class PerInstancePreferenceModel ( BasePreferenceModel ) : <EOL> """<STR_LIT>""" <EOL> instance = None <EOL> class Meta ( BasePreferenceModel . Meta ) : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> abstract = True <EOL> @ classmethod <EOL> def get_instance_model ( cls ) : <EOL> return cls . _meta . get_field ( '<STR_LIT>' ) . rel . to <EOL> @ property <EOL> def registry ( self ) : <EOL> return preference_models . get_by_instance ( self . instance ) <EOL> class UserPreferenceModel ( PerInstancePreferenceModel ) : <EOL> instance = models . ForeignKey ( settings . AUTH_USER_MODEL ) <EOL> class Meta ( PerInstancePreferenceModel . Meta ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = "<STR_LIT>" <EOL> verbose_name_plural = "<STR_LIT>" <EOL> global_preferences_registry . preference_model = GlobalPreferenceModel <EOL> from django . db . models . signals import post_save <EOL> def create_default_per_instance_preferences ( sender , created , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if created : <EOL> try : <EOL> registry = preference_models . get_by_instance ( instance ) <EOL> registry . create_default_preferences ( instance ) <EOL> except AttributeError : <EOL> pass <EOL> def invalidate_cache ( sender , created , instance , ** kwargs ) : <EOL> if not isinstance ( instance , BasePreferenceModel ) : <EOL> return <EOL> registry = preference_models . get_by_preference ( instance ) <EOL> linked_instance = getattr ( instance , '<STR_LIT>' , None ) <EOL> kwargs = { } <EOL> if linked_instance : <EOL> kwargs [ '<STR_LIT>' ] = linked_instance <EOL> manager = registry . manager ( ** kwargs ) <EOL> manager . to_cache ( instance ) <EOL> post_save . connect ( create_default_per_instance_preferences ) <EOL> post_save . connect ( invalidate_cache ) </s>
