Beispiel #1
0
    def get_queryset(self):
        user = self.get_account_viewset().get_object()
        queryset = UserNotification.objects.filter(user=user)

        # Fetch all `UserNotification` instances and then,
        # overwrite their value with the data from basket.

        # Put it into a dict so we can easily check for existence.
        set_notifications = {
            user_nfn.notification.short: user_nfn
            for user_nfn in queryset if user_nfn.notification
        }
        out = []

        newsletters = None  # Lazy - fetch the first time needed.
        by_basket_id = REMOTE_NOTIFICATIONS_BY_BASKET_ID
        for basket_id, notification in by_basket_id.items():
            if notification.group == 'dev' and not user.is_developer:
                # We only return dev notifications for developers.
                continue
            if newsletters is None:
                newsletters = fetch_subscribed_newsletters(user)
            user_notification = self._get_default_object(notification)
            user_notification.enabled = basket_id in newsletters
            set_notifications[notification.short] = user_notification

        for notification in NOTIFICATIONS_COMBINED:
            if notification.group == 'dev' and not user.is_developer:
                # We only return dev notifications for developers.
                continue
            out.append(
                set_notifications.get(
                    notification.short,  # It's been set by the user.
                    self._get_default_object(notification)))  # Or, default.
        return out
Beispiel #2
0
    def get_queryset(self, dev=False):
        user = self.get_user()
        queryset = UserNotification.objects.filter(user=user)

        # Fetch all `UserNotification` instances and then,
        # overwrite their value with the data from basket.

        # Put it into a dict so we can easily check for existence.
        set_notifications = {
            user_nfn.notification.short: user_nfn for user_nfn in queryset
            if user_nfn.notification}
        out = []

        newsletters = None  # Lazy - fetch the first time needed.
        by_basket_id = REMOTE_NOTIFICATIONS_BY_BASKET_ID
        for basket_id, notification in by_basket_id.items():
            if notification.group == 'dev' and not user.is_developer:
                # We only return dev notifications for developers.
                continue
            if newsletters is None:
                newsletters = fetch_subscribed_newsletters(user)
            user_notification = self._get_default_object(notification)
            user_notification.enabled = basket_id in newsletters
            set_notifications[notification.short] = user_notification

        include_dev = dev or user.is_developer
        for notification in NOTIFICATIONS_COMBINED:
            if notification.group == 'dev' and not include_dev:
                # We only return dev notifications for developers.
                continue
            out.append(set_notifications.get(
                notification.short,  # It's been set by the user.
                self._get_default_object(notification)))  # Or, default.
        return out
Beispiel #3
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)

        instance = kwargs.get('instance')
        if instance and instance.has_anonymous_username:
            kwargs.setdefault('initial', {})
            kwargs['initial']['username'] = ''

        super(UserEditForm, self).__init__(*args, **kwargs)

        errors = {
            'invalid':
            ugettext('This URL has an invalid format. Valid URLs look like '
                     'http://example.com/my_page.')
        }
        self.fields['homepage'].error_messages = errors

        if self.instance:
            # We are fetching all `UserNotification` instances and then,
            # overwrite their value with the data from basket.
            default = {
                idx: notification.default_checked
                for idx, notification in
                notifications.NOTIFICATIONS_BY_ID.items()
            }
            user = {
                notification.notification_id: notification.enabled
                for notification in self.instance.notifications.all()
            }
            default.update(user)

            # Fetch data from basket and overwrite our local data
            # (which will then be updated on .save() accordingly)
            newsletters = fetch_subscribed_newsletters(self.instance)

            by_basket_id = notifications.REMOTE_NOTIFICATIONS_BY_BASKET_ID
            for basket_id, notification in by_basket_id.items():
                default[notification.id] = basket_id in newsletters

            # Add choices to Notification.
            if self.instance.is_developer:
                choices = [(l.id, l.label)
                           for l in notifications.NOTIFICATIONS_COMBINED]
            else:
                choices = [(l.id, l.label)
                           for l in notifications.NOTIFICATIONS_COMBINED
                           if l.group != 'dev']

            # Append a "NEW" message to new notification options.
            saved = self.instance.notifications.values_list('notification_id',
                                                            flat=True)
            self.choices_status = {}
            for idx, label in choices:
                self.choices_status[idx] = idx not in saved

            self.fields['notifications'].choices = choices
            self.fields['notifications'].initial = [
                i for i, v in default.items() if v
            ]
            self.fields['notifications'].widget.form_instance = self
Beispiel #4
0
    def get_queryset(self):
        user = self.get_account_viewset().get_object()
        queryset = UserNotification.objects.filter(user=user)

        # Fetch all `UserNotification` instances and then, if the
        # waffle-switch is active overwrite their value with the
        # data from basket. Once we switched the integration "on" on prod
        # all `UserNotification` instances that are now handled by basket
        # can be deleted.

        # Put it into a dict so we can easily check for existence.
        set_notifications = {
            user_nfn.notification.short: user_nfn for user_nfn in queryset}
        out = []
        for notification in NOTIFICATIONS_COMBINED:
            out.append(set_notifications.get(
                notification.short,  # It's been set by the user.
                self._get_default_object(notification)))  # Otherwise, default.

        if waffle.switch_is_active('activate-basket-sync'):
            newsletters = fetch_subscribed_newsletters(user)

            by_basket_id = REMOTE_NOTIFICATIONS_BY_BASKET_ID
            for basket_id, notification in by_basket_id.items():
                notification = self._get_default_object(notification)
                notification.enabled = basket_id in newsletters
                out.append(notification)

        return out
Beispiel #5
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)

        instance = kwargs.get('instance')
        if instance and instance.has_anonymous_username:
            kwargs.setdefault('initial', {})
            kwargs['initial']['username'] = ''

        super(UserEditForm, self).__init__(*args, **kwargs)

        errors = {
            'invalid':
            ugettext('This URL has an invalid format. Valid URLs look like '
                     'http://example.com/my_page.')
        }
        self.fields['homepage'].error_messages = errors

        if self.instance:
            # We are fetching all `UserNotification` instances and then,
            # if the waffle-switch is active overwrite their value with the
            # data from basket. This simplifies the process of implementing
            # the waffle-switch. Once we switched the integration "on" on prod
            # all `UserNotification` instances that are now handled by basket
            # can be deleted.
            default = {
                idx: notification.default_checked
                for idx, notification in
                notifications.NOTIFICATIONS_BY_ID.items()
            }
            user = {
                notification.notification_id: notification.enabled
                for notification in self.instance.notifications.all()
            }
            default.update(user)

            if waffle.switch_is_active('activate-basket-sync'):
                newsletters = fetch_subscribed_newsletters(self.instance)

                by_basket_id = notifications.REMOTE_NOTIFICATIONS_BY_BASKET_ID
                for basket_id, notification in by_basket_id.items():
                    default[notification.id] = basket_id in newsletters

            # Add choices to Notification.
            choices = notifications.NOTIFICATIONS_CHOICES
            if not self.instance.is_developer:
                choices = notifications.NOTIFICATIONS_CHOICES_NOT_DEV

            # Append a "NEW" message to new notification options.
            saved = self.instance.notifications.values_list('notification_id',
                                                            flat=True)
            self.choices_status = {}
            for idx, label in choices:
                self.choices_status[idx] = idx not in saved

            self.fields['notifications'].choices = choices
            self.fields['notifications'].initial = [
                i for i, v in default.items() if v
            ]
            self.fields['notifications'].widget.form_instance = self
Beispiel #6
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)

        instance = kwargs.get('instance')
        if instance and instance.has_anonymous_username:
            kwargs.setdefault('initial', {})
            kwargs['initial']['username'] = ''

        super(UserEditForm, self).__init__(*args, **kwargs)

        errors = {
            'invalid': ugettext(
                'This URL has an invalid format. Valid URLs look like '
                'http://example.com/my_page.')}
        self.fields['homepage'].error_messages = errors

        if self.instance:
            # We are fetching all `UserNotification` instances and then,
            # if the waffle-switch is active overwrite their value with the
            # data from basket. This simplifies the process of implementing
            # the waffle-switch. Once we switched the integration "on" on prod
            # all `UserNotification` instances that are now handled by basket
            # can be deleted.
            default = {
                idx: notification.default_checked
                for idx, notification
                in notifications.NOTIFICATIONS_BY_ID.items()}
            user = {
                notification.notification_id: notification.enabled
                for notification in self.instance.notifications.all()}
            default.update(user)

            if waffle.switch_is_active('activate-basket-sync'):
                newsletters = fetch_subscribed_newsletters(self.instance)

                by_basket_id = notifications.REMOTE_NOTIFICATIONS_BY_BASKET_ID
                for basket_id, notification in by_basket_id.items():
                    default[notification.id] = basket_id in newsletters

            # Add choices to Notification.
            choices = notifications.NOTIFICATIONS_CHOICES
            if not self.instance.is_developer:
                choices = notifications.NOTIFICATIONS_CHOICES_NOT_DEV

            # Append a "NEW" message to new notification options.
            saved = self.instance.notifications.values_list('notification_id',
                                                            flat=True)
            self.choices_status = {}
            for idx, label in choices:
                self.choices_status[idx] = idx not in saved

            self.fields['notifications'].choices = choices
            self.fields['notifications'].initial = [i for i, v
                                                    in default.items() if v]
            self.fields['notifications'].widget.form_instance = self
Beispiel #7
0
    def get_queryset(self):
        user = self.get_account_viewset().get_object()
        queryset = UserNotification.objects.filter(user=user)

        # Fetch all `UserNotification` instances and then, if the
        # waffle-switch is active overwrite their value with the
        # data from basket. Once we switched the integration "on" on prod
        # all `UserNotification` instances that are now handled by basket
        # can be deleted.

        # Put it into a dict so we can easily check for existence.
        set_notifications = {
            user_nfn.notification.short: user_nfn for user_nfn in queryset
            if user_nfn.notification}
        out = []

        if waffle.switch_is_active('activate-basket-sync'):
            newsletters = None  # Lazy - fetch the first time needed.
            by_basket_id = REMOTE_NOTIFICATIONS_BY_BASKET_ID
            for basket_id, notification in by_basket_id.items():
                if notification.group == 'dev' and not user.is_developer:
                    # We only return dev notifications for developers.
                    continue
                if newsletters is None:
                    newsletters = fetch_subscribed_newsletters(user)
                user_notification = self._get_default_object(notification)
                user_notification.enabled = basket_id in newsletters
                set_notifications[notification.short] = user_notification

        for notification in NOTIFICATIONS_COMBINED:
            if notification.group == 'dev' and not user.is_developer:
                # We only return dev notifications for developers.
                continue
            out.append(set_notifications.get(
                notification.short,  # It's been set by the user.
                self._get_default_object(notification)))  # Or, default.
        return out