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 (GenericRelation
,
9 from mygpo
.users
.settings
import PUBLIC_SUB_PODCAST
10 from mygpo
.podcasts
.models
import Podcast
13 logger
= logging
.getLogger(__name__
)
16 class UserSettingsManager(models
.Manager
):
17 """ Manager for PodcastConfig objects """
19 def get_private_podcasts(self
, user
):
20 """ Returns the podcasts that the user has marked as private """
21 settings
= self
.filter(
23 content_type
=ContentType
.objects
.get_for_model(Podcast
),
27 for setting
in settings
:
28 if not setting
.get_wksetting(PUBLIC_SUB_PODCAST
):
29 private
.append(setting
.content_object
)
33 def get_for_scope(self
, user
, scope
):
34 """ Returns the settings object for the given user and scope obj
36 If scope is None, the settings for the user are returned """
41 content_type
= ContentType
.objects
.get_for_model(scope
)
45 return UserSettings
.objects
.get(
47 content_type
=content_type
,
51 except UserSettings
.DoesNotExist
:
52 # if it does not exist, return a new instance. It is up to the
53 # caller to save the object if required
56 content_type
=content_type
,
61 class UserSettings(models
.Model
):
62 """ Stores settings for a podcast, episode, user or client """
64 # the user for which the config is stored
65 user
= models
.ForeignKey(settings
.AUTH_USER_MODEL
,
66 on_delete
=models
.CASCADE
)
68 # see https://docs.djangoproject.com/en/1.6/ref/contrib/contenttypes/#generic-relations
69 content_type
= models
.ForeignKey(ContentType
, null
=True, blank
=True)
70 object_id
= models
.UUIDField(null
=True, blank
=True)
71 content_object
= GenericForeignKey('content_type', 'object_id')
73 settings
= models
.TextField(null
=False, default
='{}')
77 ['user', 'content_type', 'object_id'],
80 verbose_name_plural
= 'User Settings'
81 verbose_name
= 'User Settings'
83 objects
= UserSettingsManager()
85 def get_wksetting(self
, setting
):
86 """ returns the value of a well-known setting """
88 settings
= json
.loads(self
.settings
)
89 except ValueError as ex
:
90 logger
.warn('Decoding settings failed: {msg}'.format(msg
=str(ex
)))
93 return settings
.get(setting
.name
, setting
.default
)
95 def set_wksetting(self
, setting
, value
):
97 settings
= json
.loads(self
.settings
)
98 except ValueError as ex
:
99 logger
.warn('Decoding settings failed: {msg}'.format(msg
=str(ex
)))
101 settings
[setting
.name
] = value
102 self
.settings
= json
.dumps(settings
)
104 def get_setting(self
, name
, default
):
105 settings
= json
.loads(self
.settings
)
106 return settings
.get(name
, default
)
108 def set_setting(self
, name
, value
):
109 settings
= json
.loads(self
.settings
)
110 settings
[name
] = value
111 self
.settings
= json
.dumps(settings
)
113 def del_setting(self
, name
):
114 settings
= json
.loads(self
.settings
)
119 self
.settings
= json
.dumps(settings
)
122 return json
.loads(self
.settings
)