Exemple #1
0
def set_user_preferences_from_request(request):
    """Sets user subscription preferences using data from a request.

    Expects data sent by form built with `sitemessage_prefs_table` template tag.

    :param request:
    :rtype: bool
    :return: Flag, whether prefs were found in the request.
    """
    prefs = []

    for pref in request.POST.getlist(_PREF_POST_KEY):
        message_alias, messenger_alias = pref.split(_ALIAS_SEP)

        try:
            get_registered_message_type(message_alias)
            get_registered_messenger_object(messenger_alias)

        except (UnknownMessengerError, UnknownMessageTypeError):
            pass

        else:
            prefs.append((message_alias, messenger_alias))

    Subscription.replace_for_user(request.user, prefs)

    return bool(prefs)
Exemple #2
0
    def test_replace_for_user(self, user):
        new_prefs = [('message3', 'messenger3')]

        assert Subscription.replace_for_user(user, new_prefs)

        Subscription.create(user, 'message', 'messenger')
        Subscription.create(user, 'message2', 'messenger2')

        assert Subscription.get_for_user(user).count() == 3

        Subscription.replace_for_user(user, new_prefs)

        s = Subscription.get_for_user(user)
        assert s.count() == 1
        s = s[0]
        assert s.message_cls == 'message3'
        assert s.messenger_cls == 'messenger3'
    def test_replace_for_user(self, user):
        new_prefs = [('message3', 'messenger3')]

        assert Subscription.replace_for_user(user, new_prefs)

        Subscription.create(user, 'message', 'messenger')
        Subscription.create(user, 'message2', 'messenger2')

        assert Subscription.get_for_user(user).count() == 3

        Subscription.replace_for_user(user, new_prefs)

        s = Subscription.get_for_user(user)
        assert s.count() == 1
        s = s[0]
        assert s.message_cls == 'message3'
        assert s.messenger_cls == 'messenger3'