def get_recipients(self):
        """
        Determine recipients depending on selected filtering which can be either:
            * group based
            * layer based
            * user based

        Choosing "group" and "layer" filtering together has the effect of sending the message
        only to users for which the following conditions are both true:
            * have a node assigned to one of the selected layers
            * are part of any of the specified groups (eg: registered, community, trusted)

        The user based filtering has instead the effect of translating in an **OR** query. Here's a practical example:
        if selecting "group" and "user" filtering the message will be sent to all the users for which ANY of the following conditions is true:
            * are part of any of the specified groups (eg: registered, community, trusted)
            * selected users
        """
        # user model
        User = get_user_model()

        # prepare email list
        emails = []

        # the following code is a bit ugly. Considering the titanic amount of work required to build all
        # the cools functionalities that I have in my mind, I can't be bothered to waste time on making it nicer right now.
        # if you have ideas on how to improve it to make it cleaner and less cluttered, please join in
        # this method has unit tests written for it, therefore if you try to change it be sure to check unit tests do not fail after your changes
        # python manage.py test mailing

        # send to all case
        if not self.is_filtered:
            # retrieve only email DB column of all active users
            users = User.objects.filter(is_active=True).only('email')
            # loop over users list
            for user in users:
                # add email to the recipient list if not already there
                if user.email not in emails:
                    emails += [user.email]
        else:
            # selected users
            if FILTERS.get('users') in self.filters:
                # retrieve selected users
                users = self.users.all().only('email')
                # loop over selected users
                for user in users:
                    # add email to the recipient list if not already there
                    if user.email not in emails:
                        emails += [user.email]

            # Q is a django object for "complex" filtering queries (not that complex in this case)
            # init empty Q object that will be needed in case of group filtering
            q = Q()
            q2 = Q()

            # if group filtering is checked
            if FILTERS.get('groups') in self.filters:
                # loop over each group
                for group in self.groups:
                    # if not superusers
                    if group != '0':
                        # add the group to the Q object
                        # this means that the query will look for users of that specific group
                        q = q | Q(groups=int(group))
                        q2 = q2 | Q(user__groups=int(group))
                    else:
                        # this must be done manually because superusers is not a group but an attribute of the User model
                        q = q | Q(is_superuser=True)
                        q2 = q2 | Q(user__is_superuser=True)

            # plus users must be active
            q = q & Q(is_active=True)

            # if layer filtering is checked
            if FILTERS.get('layers') in self.filters:
                # retrieve non-external layers
                layers = self.layers.all().only('id')
                # init empty q3
                q3 = Q()
                # loop over layers to form q3 object
                for layer in layers:
                    q3 = q3 | Q(layer=layer)
                # q2: user group if present
                # q3: layers
                # retrieve nodes
                nodes = Node.objects.filter(q2 & q3)
                # loop over nodes of a layer and get their email
                for node in nodes:
                    # add email to the recipient list if not already there
                    if node.user.email not in emails:
                        emails += [node.user.email]
            # else if group filterins is checked but not layers
            elif FILTERS.get('groups') in self.filters and not FILTERS.get('layers') in self.filters:
                # retrieve only email DB column of all active users
                users = User.objects.filter(q).only('email')
                # loop over users list
                for user in users:
                    # add email to the recipient list if not already there
                    if user.email not in emails:
                        emails += [user.email]
        return emails
def get_recipients ( self ) : User = get_user_model ( ) emails = [ ] if not self . is_filtered : users = User . objects . filter ( is_active = True ) . only ( 'email' ) for user in users : if user . email not in emails : emails += [ user . email ] else : if FILTERS . get ( 'users' ) in self . filters : users = self . users . all ( ) . only ( 'email' ) for user in users : if user . email not in emails : emails += [ user . email ] q = Q ( ) q2 = Q ( ) if FILTERS . get ( 'groups' ) in self . filters : for group in self . groups : if group != '0' : q = q | Q ( groups = int ( group ) ) q2 = q2 | Q ( user__groups = int ( group ) ) else : q = q | Q ( is_superuser = True ) q2 = q2 | Q ( user__is_superuser = True ) q = q & Q ( is_active = True ) if FILTERS . get ( 'layers' ) in self . filters : layers = self . layers . all ( ) . only ( 'id' ) q3 = Q ( ) for layer in layers : q3 = q3 | Q ( layer = layer ) nodes = Node . objects . filter ( q2 & q3 ) for node in nodes : if node . user . email not in emails : emails += [ node . user . email ] elif FILTERS . get ( 'groups' ) in self . filters and not FILTERS . get ( 'layers' ) in self . filters : users = User . objects . filter ( q ) . only ( 'email' ) for user in users : if user . email not in emails : emails += [ user . email ] return emails
Determine recipients depending on selected filtering which can be either:
            * group based
            * layer based
            * user based

        Choosing "group" and "layer" filtering together has the effect of sending the message
        only to users for which the following conditions are both true:
            * have a node assigned to one of the selected layers
            * are part of any of the specified groups (eg: registered, community, trusted)

        The user based filtering has instead the effect of translating in an **OR** query. Here's a practical example:
        if selecting "group" and "user" filtering the message will be sent to all the users for which ANY of the following conditions is true:
            * are part of any of the specified groups (eg: registered, community, trusted)
            * selected users