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)
def revert(self): admin_user_token = SlackUtils.get_admin_user_token(self.community) values = { "method_name": "chat.delete", "token": admin_user_token, "ts": self.timestamp, "channel": self.channel, } super().revert(values=values, call=SLACK_METHOD_ACTION)