<commit_before>from modularodm import fields

from framework.mongo import StoredObject, ObjectId

from website.project.model import Node
from website.notifications.constants import NOTIFICATION_TYPES


class NotificationSubscription(StoredObject):
    _id = fields.StringField(primary=True)  # pxyz_wiki_updated, uabc_comment_replies

    event_name = fields.StringField()      # wiki_updated, comment_replies
    owner = fields.AbstractForeignField()

    # Notification types
    none = fields.ForeignField('user', list=True, backref='none')
    email_digest = fields.ForeignField('user', list=True, backref='email_digest')
    email_transactional = fields.ForeignField('user', list=True, backref='email_transactional')

    def remove_user_from_subscription(self, user, save=True):
        for notification_type in NOTIFICATION_TYPES:
            try:
                getattr(self, notification_type, []).remove(user)
            except ValueError:
                pass

        if isinstance(self.owner, Node):
            parent = self.owner.parent or self.owner

            try:
                parent.child_node_subscriptions[user._id].remove(self.owner._id)
                parent.save()
            except ValueError:
                pass

        if save:
            self.save()<commit_msg>Add add user to subscription<commit_after>from modularodm import fields

from framework.mongo import StoredObject, ObjectId

from website.project.model import Node
from website.notifications.constants import NOTIFICATION_TYPES


class NotificationSubscription(StoredObject):
    _id = fields.StringField(primary=True)  # pxyz_wiki_updated, uabc_comment_replies

    event_name = fields.StringField()      # wiki_updated, comment_replies
    owner = fields.AbstractForeignField()

    # Notification types
    none = fields.ForeignField('user', list=True, backref='none')
    email_digest = fields.ForeignField('user', list=True, backref='email_digest')
    email_transactional = fields.ForeignField('user', list=True, backref='email_transactional')

    def add_user_to_subscription(self, user, notification_type, save=True):
        for nt in NOTIFICATION_TYPES:
            if user in getattr(self, nt):
                if nt != notification_type:
                    getattr(self, nt).remove(user)
            else:
                if nt == notification_type:
                    getattr(self, nt).append(user)

        if notification_type != 'none' and isinstance(self.owner, Node) and self.owner.parent_node:
            user_subs = self.owner.parent_node.child_node_subscriptions
            if self.owner._id not in user_subs.setdefault(user._id, []):
                user_subs[user._id].append(self.owner._id)
                self.owner.parent_node.save()

        if save:
            self.save()

    def remove_user_from_subscription(self, user, save=True):
        for notification_type in NOTIFICATION_TYPES:
            try:
                getattr(self, notification_type, []).remove(user)
            except ValueError:
                pass

        if isinstance(self.owner, Node) and self.owner.parent_node:
            try:
                self.owner.parent_node.child_node_subscriptions.get(user._id, []).remove(self.owner._id)
                self.owner.parent_node.save()
            except ValueError:
                pass

        if save:
            self.save()
{'fraction_non_alphanumeric': 0.0887905604719764, 'fraction_numerical': 0.0, 'mean_word_length': 2.689880304678999, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1054533', 'n_tokens_mistral': 953, 'n_tokens_neox': 920, 'n_words': 195}