Пример #1
0
 def construct_query(self, msg):
     subs = construct_substitutions(msg)
     kwargs = format_args(copy.copy(self._d['filter']), subs)
     kwargs = recursive_lambda_factory(kwargs, msg, name='msg')
     kwargs['defer'] = True
     total, pages, query = datanommer.models.Message.grep(**kwargs)
     return total, pages, query
Пример #2
0
 def test_basic(self):
     msg = dict(a=dict(b=dict(c=42)))
     target = {
         "a": dict(b=dict(c=42)),
         "a.b": dict(c=42),
         "a.b.c": 42,
     }
     actual = construct_substitutions(msg)
     eq_(actual, target)
Пример #3
0
    def _format_lambda_operation(self, msg):
        """ Format the string representation of a lambda operation.

        The lambda operation can be formatted here to include strings that
        appear in the message being evaluated like
        %(msg.comment.update_submitter)s.  Placeholders like that will have
        their value substituted with whatever appears in the incoming message.
        """
        subs = construct_substitutions(msg)
        operation = format_args(copy.copy(self._d['operation']), subs)
        return operation['lambda']
Пример #4
0
    def _format_lambda_operation(self, msg):
        """ Format the string representation of a lambda operation.

        The lambda operation can be formatted here to include strings that
        appear in the message being evaluated like
        %(msg.comment.update_submitter)s.  Placeholders like that will have
        their value substituted with whatever appears in the incoming message.
        """
        subs = construct_substitutions(msg)
        operation = format_args(copy.copy(self._d['operation']), subs)
        return operation['lambda']
Пример #5
0
    def matches(self, msg):
        """ A pkgdb criteria check checks if a user owns some packages. """

        subs = construct_substitutions(msg)
        expectation = format_args(copy.copy(self._d['owns']), subs)
        expectation = recursive_lambda_factory(expectation, msg, name='msg')

        actual_packages = get_pkgdb_packages_for(
            config=fedmsg_config,
            user=expectation['user'],
        )

        return set(expectation['packages']).issubset(actual_packages)
Пример #6
0
    def matches(self, msg):
        """ A pkgdb criteria check checks if a user owns some packages. """

        subs = construct_substitutions(msg)
        expectation = format_args(copy.copy(self._d['owns']), subs)
        expectation = recursive_lambda_factory(expectation, msg, name='msg')

        actual_packages = get_pkgdb_packages_for(
            config=fedmsg_config,
            user=expectation['user'],
        )

        return set(expectation['packages']).issubset(actual_packages)
Пример #7
0
    def _construct_query(self, msg):
        """ Construct a datanommer query for this message.

        The "filter" section of this criteria object will be used.  It will
        first be formatted with any substitutions present in the incoming
        message.  This is used, for instance, to construct a query like "give
        me all the messages bearing the same topic as the message that just
        arrived".
        """
        subs = construct_substitutions(msg)
        kwargs = format_args(copy.copy(self._d['filter']), subs)
        kwargs = recursive_lambda_factory(kwargs, msg, name='msg')
        kwargs['defer'] = True
        total, pages, query = datanommer.models.Message.grep(**kwargs)
        return total, pages, query
Пример #8
0
    def _construct_query(self, msg):
        """ Construct a datanommer query for this message.

        The "filter" section of this criteria object will be used.  It will
        first be formatted with any substitutions present in the incoming
        message.  This is used, for instance, to construct a query like "give
        me all the messages bearing the same topic as the message that just
        arrived".
        """
        subs = construct_substitutions(msg)
        kwargs = format_args(copy.copy(self._d['filter']), subs)
        kwargs = recursive_lambda_factory(kwargs, msg, name='msg')
        kwargs['defer'] = True
        total, pages, query = datanommer.models.Message.grep(**kwargs)
        return total, pages, query
Пример #9
0
    def matches(self, msg):

        # First, do a lightweight check to see if the msg matches a pattern.
        if not self.trigger.matches(msg):
            return set()

        # Before proceeding further, let's see who would get this badge if
        # our more heavyweight checks matched up.  If the user specifies a
        # recipient_key, we can use that to extract the potential awardee.  If
        # that is not specified, we just use `msg2usernames`.
        if self.recipient_key:
            subs = construct_substitutions(msg)
            obj = format_args(self.recipient_key, subs)

            if isinstance(obj, (basestring, int, float)):
                obj = [obj]

            # On the way, it is possible for the fedmsg message to contain None
            # for "agent".  A problem here though is that None is not iterable,
            # so let's replace it with an equivalently empty iterable so code
            # further down doesn't freak out.  An instance of this is when a
            # user without a fas account comments on a bodhi update.
            if obj is None:
                obj = []

            awardees = frozenset(obj)

            if self.recipient_nick2fas:
                awardees = frozenset([
                    nick2fas(nick, **fedmsg_config) for nick in awardees
                ])
        else:
            usernames = fedmsg.meta.msg2usernames(msg)
            awardees = usernames.difference(self.banned_usernames)

        # Strip anyone who is an IP address
        awardees = frozenset([
            user for user in awardees if not (
                user.startswith('192.168.') or
                user.startswith('10.')
            )
        ])

        # If no-one would get the badge by default, then no reason to waste
        # time doing any further checks.  No need to query the Tahrir DB.
        if not awardees:
            return awardees

        # Limit awardees to only those who do not already have this badge.
        # Do this only if we have an active connection to the Tahrir DB.
        if self.tahrir:
            awardees = frozenset([
                user for user in awardees
                if not self.tahrir.assertion_exists(
                    self.badge_id, "*****@*****.**" % user
                )])

            # Also, exclude any potential awardees who have opted out.
            awardees = frozenset([
                user for user in awardees
                if not self.tahrir.person_opted_out(
                    "*****@*****.**" % user
                )])

        # If no-one would get the badge at this point, then no reason to waste
        # time doing any further checks.  No need to query datanommer.
        if not awardees:
            return awardees

        # Check our backend criteria -- likely, perform datanommer queries.
        if not self.criteria.matches(msg):
            return set()

        return awardees
Пример #10
0
    def matches(self, msg):

        # First, do a lightweight check to see if the msg matches a pattern.
        if not self.trigger.matches(msg):
            return set()

        # Before proceeding further, let's see who would get this badge if
        # our more heavyweight checks matched up.  If the user specifies a
        # recipient_key, we can use that to extract the potential awardee.  If
        # that is not specified, we just use `msg2usernames`.
        if self.recipient_key:
            subs = construct_substitutions(msg)
            obj = format_args(self.recipient_key, subs)

            if isinstance(obj, (basestring, int, float)):
                obj = [obj]

            # On the way, it is possible for the fedmsg message to contain None
            # for "agent".  A problem here though is that None is not iterable,
            # so let's replace it with an equivalently empty iterable so code
            # further down doesn't freak out.  An instance of this is when a
            # user without a fas account comments on a bodhi update.
            if obj is None:
                obj = []

            awardees = frozenset(obj)

            if self.recipient_nick2fas:
                awardees = frozenset(
                    [nick2fas(nick, **fedmsg_config) for nick in awardees])

            if self.recipient_email2fas:
                awardees = frozenset(
                    [email2fas(email, **fedmsg_config) for email in awardees])
        else:
            usernames = fedmsg.meta.msg2usernames(msg)
            awardees = usernames.difference(self.banned_usernames)

        # Strip anyone who is an IP address
        awardees = frozenset([
            user for user in awardees
            if not (user.startswith('192.168.') or user.startswith('10.'))
        ])

        # If no-one would get the badge by default, then no reason to waste
        # time doing any further checks.  No need to query the Tahrir DB.
        if not awardees:
            return awardees

        # Limit awardees to only those who do not already have this badge.
        # Do this only if we have an active connection to the Tahrir DB.
        if self.tahrir:
            awardees = frozenset([
                user for user in awardees if not self.tahrir.assertion_exists(
                    self.badge_id, "*****@*****.**" % user)
            ])

            # Also, exclude any potential awardees who have opted out.
            awardees = frozenset([
                user for user in awardees
                if not self.tahrir.person_opted_out("*****@*****.**" %
                                                    user)
            ])

        # If no-one would get the badge at this point, then no reason to waste
        # time doing any further checks.  No need to query datanommer.
        if not awardees:
            return awardees

        # Check our backend criteria -- likely, perform datanommer queries.
        try:
            if not self.criteria.matches(msg):
                return set()
        except IOError as e:
            log.exception(e)
            return set()

        # Lastly, and this is probably most expensive.  Make sure the person
        # actually has a FAS account before we award anything.
        # https://github.com/fedora-infra/tahrir/issues/225
        awardees = set(
            [u for u in awardees if user_exists_in_fas(fedmsg_config, u)])

        return awardees
Пример #11
0
    def test_real(self):
        msg = {
            "i": 2,
            "msg": {
                "thread": {
                    "tagnames": [
                        "town"
                    ],
                    "pk": 2,
                    "title": "alskdjflaksjdf lakjsf a"
                },
                "created": False,
                "timestamp": 1359947640.0,
                "topmost_post_id": 2,
                "agent": "ralph",
                "newly_mentioned_users": [],
                "diff": "<p>alskdfj... the diff is actually here",
                "post": {
                    "vote_up_count": 0,
                    "text": "alskdfjalskdjf alkjasdalskdjf ...",
                    "summary": "alskdfjalskdjf alkjasdalskdjf ...",
                    "comment_count": 0,
                    "vote_down_count": 0,
                    "pk": 2,
                    "post_type": "question"
                }
            },
            "topic": "org.fedoraproject.dev.askbot.post.edit",
            "username": "******",
            "timestamp": 1359947640.986208
        }
        target = {
            'username': '******',
            'msg.post.text': 'alskdfjalskdjf alkjasdalskdjf ...',
            'msg.thread.title': 'alskdjflaksjdf lakjsf a',
            'msg.post.vote_down_count': 0,
            'msg.post.post_type': 'question',
            'msg.thread.pk': 2,
            'msg.newly_mentioned_users': [],
            'msg.diff': '<p>alskdfj... the diff is actually here',
            'topic': 'org.fedoraproject.dev.askbot.post.edit',
            'msg.agent': 'ralph',
            'msg.post.comment_count': 0,
            'msg.post': {
                'vote_up_count': 0,

                'text': 'alskdfjalskdjf alkjasdalskdjf ...',
                'summary': 'alskdfjalskdjf alkjasdalskdjf ...',
                'comment_count': 0,
                'vote_down_count': 0,
                'pk': 2,
                'post_type': 'question'},
            'msg.timestamp': 1359947640.0,
            'timestamp': 1359947640.986208,
            'msg.topmost_post_id': 2,
            'i': 2,
            'msg.post.pk': 2,
            'msg.post.vote_up_count': 0,
            'msg.post.summary': 'alskdfjalskdjf alkjasdalskdjf ...',
            'msg.thread.tagnames': ['town'],
            'msg.thread': {'tagnames': ['town'],
                           'pk': 2,
                           'title': 'alskdjflaksjdf lakjsf a'},
            'msg': {'newly_mentioned_users': [],
                    'thread': {'tagnames': ['town'],
                               'pk': 2,
                               'title': 'alskdjflaksjdf lakjsf a'},
                    'created': False,
                    'topmost_post_id': 2,
                    'timestamp': 1359947640.0,
                    'post': {'vote_up_count': 0,
                             'text': 'alskdfjalskdjf alkjasdalskdjf ...',
                             'summary': 'alskdfjalskdjf alkjasdalskdjf ...',
                             'comment_count': 0,
                             'vote_down_count': 0,
                             'pk': 2,
                             'post_type': 'question'},
                    'diff': '<p>alskdfj... the diff is actually here',
                    'agent': 'ralph'},
            'msg.created': False,
        }
        actual = construct_substitutions(msg)
        eq_(actual, target)
Пример #12
0
    def matches(self, msg):

        # First, do a lightweight check to see if the msg matches a pattern.
        if not self.trigger.matches(msg):
            return set()

        # Before proceeding further, let's see who would get this badge if
        # our more heavyweight checks matched up.  If the user specifies a
        # recipient_key, we can use that to extract the potential awardee.  If
        # that is not specified, we just use `msg2usernames`.
        if self.recipient_key:
            subs = construct_substitutions(msg)
            obj = format_args(self.recipient_key, subs)

            if isinstance(obj, (basestring, int, float)):
                obj = [obj]

            awardees = set(obj)

            if self.recipient_nick2fas:
                awardees = set([
                    nick2fas(nick, **fedmsg_config) for nick in awardees
                ])
        else:
            usernames = fedmsg.meta.msg2usernames(msg)
            awardees = usernames.difference(self.banned_usernames)

        # Strip anyone who is an IP address
        awardees = set([
            user for user in awardees if not (
                user.startswith('192.168.') or
                user.startswith('10.')
            )
        ])

        # If no-one would get the badge by default, then no reason to waste
        # time doing any further checks.  No need to query the Tahrir DB.
        if not awardees:
            return awardees

        # Limit awardees to only those who do not already have this badge.
        # Do this only if we have an active connection to the Tahrir DB.
        if self.tahrir:
            awardees = set([user for user in awardees
                            if not self.tahrir.assertion_exists(
                                self.badge_id, "*****@*****.**" % user
                            )])

            # Also, exclude any potential awardees who have opted out.
            awardees = set([user for user in awardees
                            if not self.tahrir.person_opted_out(
                                "*****@*****.**" % user
                            )])

        # If no-one would get the badge at this point, then no reason to waste
        # time doing any further checks.  No need to query datanommer.
        if not awardees:
            return awardees

        # Check our backend criteria -- likely, perform datanommer queries.
        if not self.criteria.matches(msg):
            return set()

        return awardees