Skip to content

Commit

Permalink
Fake UserSettings objects when usersetting for the current site `Do…
Browse files Browse the repository at this point in the history
…esNotExist`
  • Loading branch information
Mishbah committed May 26, 2015
1 parent 0bdf3c7 commit 9ee8b88
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions usersettings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ def clear_cache(self):
USERSETTINGS_CACHE = {}


@python_2_unicode_compatible
class EmptyUserSetting(object):
"""
This fake `UserSettings` objects will be returned by
the `get_current_usersettings()` if a `usersettings`
object `DoesNotExist` for the current site.
"""

id = None
pk = None

def __init__(self):
pass

def __str__(self):
return 'User Setting not defined for current site'

def save(self, force_insert=False, force_update=False):
raise NotImplementedError('EmptyUserSettings cannot be saved.')

def delete(self):
raise NotImplementedError('EmptyUserSettings cannot be deleted.')


class UserSettings(models.Model):
site = models.OneToOneField(
Site, editable=False, null=True, related_name='usersettings')
Expand All @@ -65,6 +89,12 @@ class Meta:
def __str__(self):
return force_text(self.site)

def get_default(self):
"""
Returns the default object for usersettings.
"""
return EmptyUserSetting()


def clear_usersettings_cache(sender, **kwargs):
"""
Expand Down
2 changes: 1 addition & 1 deletion usersettings/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def get_current_usersettings():
try:
current_usersettings = USERSETTINGS_MODEL.objects.get_current()
except USERSETTINGS_MODEL.DoesNotExist:
current_usersettings = None
current_usersettings = USERSETTINGS_MODEL.get_default()
return current_usersettings

0 comments on commit 9ee8b88

Please sign in to comment.