Exemplo n.º 1
0
    def execute_platform_action(self, action, delete_policykit_post=True):
        from policyengine.views import clean_up_proposals

        obj = action

        if not obj.community_origin or (obj.community_origin
                                        and obj.community_revert):
            call = obj.ACTION

            data = {}
            admin_user_token = SlackUtils.get_admin_user_token(community=self)

            if hasattr(action, "EXECUTE_PARAMETERS"):
                for fieldname in action.EXECUTE_PARAMETERS:
                    data[fieldname] = getattr(action, fieldname)

            logger.debug(
                f"Preparing to make request {call} which requires token type {obj.AUTH}..."
            )
            if obj.AUTH == "user":
                data["token"] = action.initiator.access_token
                if not data["token"]:
                    # we don't have the token for the user who proposed the action, so use an admin user token instead
                    data["token"] = admin_user_token
            elif obj.AUTH == "admin_bot":
                if action.initiator.is_community_admin:
                    data["token"] = action.initiator.access_token
            elif obj.AUTH == "admin_user":
                data["token"] = admin_user_token

            logger.debug(
                f"Overriding token? {True if data.get('token') else False}")

            try:
                self.__make_generic_api_call(call, data)
            except Exception as e:
                logger.error(
                    f"Error making API call in execute_platform_action: {e}")
                clean_up_proposals(action, False)
                raise

            # delete PolicyKit Post
            if delete_policykit_post:
                posted_action = None
                if action.is_bundled:
                    bundle = action.platformactionbundle_set.all()
                    if bundle.exists():
                        posted_action = bundle[0]
                else:
                    posted_action = action

                if posted_action.community_post:
                    values = {
                        "token": admin_user_token,
                        "ts": posted_action.community_post,
                        "channel": obj.channel,
                    }
                    self.__make_generic_api_call("chat.delete", values)

        clean_up_proposals(action, True)
Exemplo n.º 2
0
    def execute_platform_action(self, action, delete_policykit_post=True):
        from policyengine.models import LogAPICall, CommunityUser
        from policyengine.views import clean_up_proposals

        logger.info('here')
        obj = action

        if not obj.community_origin or (obj.community_origin and obj.community_revert):
            logger.info('EXECUTING ACTION BELOW:')
            call = self.API + obj.ACTION
            logger.info(call)

            obj_fields = []
            for f in obj._meta.get_fields():
                if f.name not in ['polymorphic_ctype',
                                  'community',
                                  'initiator',
                                  'communityapi_ptr',
                                  'platformaction',
                                  'platformactionbundle',
                                  'community_revert',
                                  'community_origin',
                                  'is_bundled'
                                  ]:
                    obj_fields.append(f.name)

            data = {}

            for item in obj_fields:
                try:
                    if item != 'id':
                        value = getattr(obj, item)
                        data[item] = value
                except obj.DoesNotExist:
                    continue

            res = LogAPICall.make_api_call(self, data, call)

            if delete_policykit_post:
                posted_action = None
                if action.is_bundled:
                    bundle = action.platformactionbundle_set.all()
                    if bundle.exists():
                        posted_action = bundle[0]
                else:
                    posted_action = action

                if posted_action.community_post:
                    data = {}
                    call = 'channels/{0}/messages/{1}'.format(obj.channel, posted_action.community_post)
                    _ = LogAPICall.make_api_call(self, data, call)

            if res['ok']:
                clean_up_proposals(action, True)
            else:
                error_message = res['error']
                logger.info(error_message)
                clean_up_proposals(action, False)
        else:
            clean_up_proposals(action, True)
Exemplo n.º 3
0
    def execute_platform_action(self, action, delete_policykit_post=True):
        from policyengine.models import LogAPICall, CommunityUser
        from policyengine.views import clean_up_proposals

        logger.info('here')

        logger.info(action)

        obj = action

        if not obj.community_origin or (obj.community_origin and obj.community_revert):
            logger.info('EXECUTING ACTION BELOW:')
            call = obj.ACTION
            logger.info(call)

            obj_fields = []
            for f in obj._meta.get_fields():
                if f.name not in ['polymorphic_ctype',
                                  'community',
                                  'initiator',
                                  'communityaction_ptr',
                                  'platformaction',
                                  'platformactionbundle',
                                  'community_revert',
                                  'community_origin',
                                  'is_bundled',
                                  'proposal',
                                  'data',
                                  'community_post',
                                  'name'
                                  ]:
                    obj_fields.append(f.name)

            data = {}

            for item in obj_fields:
                try :
                    if item != 'id':
                        value = getattr(obj, item)
                        data[item] = value
                except obj.DoesNotExist:
                    continue


            data['sr'] = action.community.community_name
            data['api_type'] = 'json'

            res = LogAPICall.make_api_call(self, data, call, action=action)

            logger.info(res)

            # delete PolicyKit Post
            logger.info('delete policykit post')
            if delete_policykit_post:
                posted_action = None
                if action.is_bundled:
                    bundle = action.platformactionbundle_set.all()
                    if bundle.exists():
                        posted_action = bundle[0]
                else:
                    posted_action = action

                if posted_action.community_post:
                    values = {'id': posted_action.community_post
                            }
                    call = 'api/remove'
                    _ = LogAPICall.make_api_call(self, values, call)

            # approve post
            logger.info('approve executed post')
            action.community.make_call('api/approve', {'id': res['json']['data']['name']})

        clean_up_proposals(action, True)
Exemplo n.º 4
0
    def execute_platform_action(self, action, delete_policykit_post=True):

        from policyengine.models import LogAPICall, CommunityUser
        from policyengine.views import clean_up_proposals

        logger.info('here')
        obj = action

        if not obj.community_origin or (obj.community_origin
                                        and obj.community_revert):
            logger.info('EXECUTING ACTION BELOW:')
            call = obj.ACTION
            logger.info(call)

            obj_fields = []
            for f in obj._meta.get_fields():
                if f.name not in [
                        'polymorphic_ctype', 'community', 'initiator',
                        'communityapi_ptr', 'platformaction',
                        'platformactionbundle', 'community_revert',
                        'community_origin', 'is_bundled', 'proposal',
                        'platformaction_ptr', 'data', 'community_post'
                ]:
                    obj_fields.append(f.name)

            data = {}

            if obj.AUTH == "user":
                data['token'] = action.proposal.author.access_token
                if not data['token']:
                    admin_user = CommunityUser.objects.filter(
                        is_community_admin=True)[0]
                    data['token'] = admin_user.access_token
            elif obj.AUTH == "admin_bot":
                if action.proposal.author.is_community_admin:
                    data['token'] = action.proposal.author.access_token
                else:
                    data['token'] = self.access_token
            elif obj.AUTH == "admin_user":
                admin_user = CommunityUser.objects.filter(
                    is_community_admin=True)[0]
                data['token'] = admin_user.access_token
            else:
                data['token'] = self.access_token

            for item in obj_fields:
                try:
                    if item != 'id':
                        value = getattr(obj, item)
                        data[item] = value
                except obj.DoesNotExist:
                    continue

            res = LogAPICall.make_api_call(self, data, call)

            # delete PolicyKit Post
            if delete_policykit_post:
                posted_action = None
                if action.is_bundled:
                    bundle = action.platformactionbundle_set.all()
                    if bundle.exists():
                        posted_action = bundle[0]
                else:
                    posted_action = action

                if posted_action.community_post:
                    admin_user = CommunityUser.objects.filter(
                        is_community_admin=True)[0]
                    values = {
                        'token': admin_user.access_token,
                        'ts': posted_action.community_post,
                        'channel': obj.channel
                    }
                    call = 'chat.delete'
                    _ = LogAPICall.make_api_call(self, values, call)

            if res['ok']:
                clean_up_proposals(action, True)
            else:
                error_message = res['error']
                logger.info(error_message)
                clean_up_proposals(action, False)

        else:
            clean_up_proposals(action, True)