3 from django
.db
import models
4 from django
.conf
import settings
5 from django
.contrib
.contenttypes
.models
import ContentType
6 from django
.contrib
.contenttypes
.fields
import GenericForeignKey
8 from mygpo
.users
.settings
import PUBLIC_SUB_PODCAST
9 from mygpo
.podcasts
.models
import Podcast
12 logger
= logging
.getLogger(__name__
)
15 class UserSettingsManager(models
.Manager
):
16 """ Manager for PodcastConfig objects """
18 def get_private_podcasts(self
, user
):
19 """ Returns the podcasts that the user has marked as private """
20 settings
= self
.filter(
22 content_type
=ContentType
.objects
.get_for_model(Podcast
),
26 for setting
in settings
:
27 if not setting
.get_wksetting(PUBLIC_SUB_PODCAST
):
28 private
.append(setting
.content_object
)
32 def get_for_scope(self
, user
, scope
):
33 """ Returns the settings object for the given user and scope obj
35 If scope is None, the settings for the user are returned """
40 content_type
= ContentType
.objects
.get_for_model(scope
)
44 return UserSettings
.objects
.get(
46 content_type
=content_type
,
50 except UserSettings
.DoesNotExist
:
51 # if it does not exist, return a new instance. It is up to the
52 # caller to save the object if required
55 content_type
=content_type
,
60 class UserSettings(models
.Model
):
61 """ Stores settings for a podcast, episode, user or client """
63 # the user for which the config is stored
64 user
= models
.ForeignKey(settings
.AUTH_USER_MODEL
,
65 on_delete
=models
.CASCADE
)
67 # see https://docs.djangoproject.com/en/1.6/ref/contrib/contenttypes/#generic-relations
68 content_type
= models
.ForeignKey(
72 on_delete
=models
.PROTECT
,
74 object_id
= models
.UUIDField(null
=True, blank
=True)
75 content_object
= GenericForeignKey('content_type', 'object_id')
77 settings
= models
.TextField(null
=False, default
='{}')
81 ['user', 'content_type', 'object_id'],
84 verbose_name_plural
= 'User Settings'
85 verbose_name
= 'User Settings'
87 objects
= UserSettingsManager()
89 def get_wksetting(self
, setting
):
90 """ returns the value of a well-known setting """
92 settings
= json
.loads(self
.settings
)
93 except ValueError as ex
:
94 logger
.warn('Decoding settings failed: {msg}'.format(msg
=str(ex
)))
97 return settings
.get(setting
.name
, setting
.default
)
99 def set_wksetting(self
, setting
, value
):
101 settings
= json
.loads(self
.settings
)
102 except ValueError as ex
:
103 logger
.warn('Decoding settings failed: {msg}'.format(msg
=str(ex
)))
105 settings
[setting
.name
] = value
106 self
.settings
= json
.dumps(settings
)
108 def get_setting(self
, name
, default
):
109 settings
= json
.loads(self
.settings
)
110 return settings
.get(name
, default
)
112 def set_setting(self
, name
, value
):
113 settings
= json
.loads(self
.settings
)
114 settings
[name
] = value
115 self
.settings
= json
.dumps(settings
)
117 def del_setting(self
, name
):
118 settings
= json
.loads(self
.settings
)
123 self
.settings
= json
.dumps(settings
)
126 return json
.loads(self
.settings
)