def save(self, *args, **kwargs): if self.shouldCreate(): #runs only if they have propose permission if self.initiator.has_perm(self.app_name + '.add_' + self.action_codename): if hasattr(self, 'proposal'): self.proposal.status = Proposal.PROPOSED else: self.proposal = Proposal.objects.create(status=Proposal.PROPOSED, author=self.initiator) super(ConstitutionAction, self).save(*args, **kwargs) if not self.is_bundled: action = self #if they have execute permission, skip all policies if action.initiator.has_perm(action.app_name + '.can_execute_' + action.action_codename): action.execute() else: for policy in ConstitutionPolicy.objects.filter(community=self.community): if filter_policy(policy, action): initialize_policy(policy, action) check_result = check_policy(policy, action) if check_result == Proposal.PASSED: pass_policy(policy, action) elif check_result == Proposal.FAILED: fail_policy(policy, action) else: notify_policy(policy, action) else: self.proposal = Proposal.objects.create(status=Proposal.FAILED, author=self.initiator) else: if not self.pk: # Runs only when object is new self.proposal = Proposal.objects.create(status=Proposal.FAILED, author=self.initiator) super(ConstitutionAction, self).save(*args, **kwargs)
def save(self, *args, **kwargs): logger.info('entered save') if not self.pk: logger.info('is pk') #runs only if they have propose permission if self.initiator.has_perm(self.app_name + '.add_' + self.action_codename): logger.info('has propose permission') p = Proposal.objects.create(status=Proposal.PROPOSED, author=self.initiator) self.proposal = p super(PlatformAction, self).save(*args, **kwargs) if not self.is_bundled: action = self #if they have execute permission, skip all policies if action.initiator.has_perm(action.app_name + '.can_execute_' + action.action_codename): logger.info('has execute permission') action.execute() else: for policy in PlatformPolicy.objects.filter( community=self.community): logger.info('save: policy checking') if filter_policy(policy, action): initialize_policy(policy, action) check_result = check_policy(policy, action) if check_result == Proposal.PASSED: logger.info('passed (save)') pass_policy(policy, action) elif check_result == Proposal.FAILED: logger.info('failed (save)') fail_policy(policy, action) else: logger.info('notify (save)') notify_policy(policy, action) else: logger.info('does not have propose permission') p = Proposal.objects.create(status=Proposal.FAILED, author=self.initiator) self.proposal = p else: super(PlatformAction, self).save(*args, **kwargs)
def after_constitutionaction_bundle_save(sender, instance, **kwargs): action = instance if action.initiator.has_perm(action.app_name + '.add_' + action.action_codename): #if they have execute permission, skip all policies if action.initiator.has_perm(action.app_name + '.can_execute_' + action.action_codename): action.execute() else: for policy in ConstitutionPolicy.objects.filter(community=action.community): if filter_policy(policy, action): initialize_policy(policy, action) check_result = check_policy(policy, action) if check_result == Proposal.PASSED: pass_policy(policy, action) elif check_result == Proposal.FAILED: fail_policy(policy, action) else: notify_policy(policy, action)
def action(request): json_data = json.loads(request.body) logger.info('RECEIVED ACTION') logger.info(json_data) action_type = json_data.get('type') if action_type == "url_verification": challenge = json_data.get('challenge') return HttpResponse(challenge) elif action_type == "event_callback": event = json_data.get('event') team_id = json_data.get('team_id') community = SlackCommunity.objects.get(team_id=team_id) admin_user = SlackUser.objects.filter(is_community_admin=True)[0] new_api_action = None policy_kit_action = False if event.get('type') == "channel_rename": if not is_policykit_action(community, event['channel']['name'], 'name', SlackRenameConversation.ACTION): new_api_action = SlackRenameConversation() new_api_action.community = community new_api_action.name = event['channel']['name'] new_api_action.channel = event['channel']['id'] u,_ = SlackUser.objects.get_or_create(username=event['user'], community=community) new_api_action.initiator = u prev_names = new_api_action.get_channel_info() new_api_action.prev_name = prev_names[0] elif event.get('type') == 'message' and event.get('subtype') == None: if not is_policykit_action(community, event['text'], 'text', SlackPostMessage.ACTION): new_api_action = SlackPostMessage() new_api_action.community = community new_api_action.text = event['text'] new_api_action.channel = event['channel'] new_api_action.time_stamp = event['ts'] u,_ = SlackUser.objects.get_or_create(username=event['user'], community=community) new_api_action.initiator = u elif event.get('type') == "member_joined_channel": if not is_policykit_action(community, event['channel'], 'channel', SlackJoinConversation.ACTION): new_api_action = SlackJoinConversation() new_api_action.community = community if event.get('inviter'): u,_ = SlackUser.objects.get_or_create(username=event['inviter'], community=community) new_api_action.initiator = u else: u,_ = SlackUser.objects.get_or_create(username=event['user'], community=community) new_api_action.initiator = u new_api_action.users = event.get('user') new_api_action.channel = event['channel'] elif event.get('type') == 'pin_added': if not is_policykit_action(community, event['channel_id'], 'channel', SlackPinMessage.ACTION): new_api_action = SlackPinMessage() new_api_action.community = community u,_ = SlackUser.objects.get_or_create(username=event['user'], community=community) new_api_action.initiator = u new_api_action.channel = event['channel_id'] new_api_action.timestamp = event['item']['message']['ts'] if new_api_action.initiator.has_perm('slackintegration.add_' + new_api_action.action_codename): if new_api_action and not policy_kit_action: #if they have execute permission, skip all policies if new_api_action.initiator.has_perm('slackintegration.can_execute_' + new_api_action.action_codename): new_api_action.execute() else: for policy in PlatformPolicy.objects.filter(community=new_api_action.community): if filter_policy(policy, new_api_action): if not new_api_action.pk: new_api_action.community_origin = True new_api_action.is_bundled = False new_api_action.save() initialize_policy(policy, new_api_action) cond_result = check_policy(policy, new_api_action) if cond_result == Proposal.PROPOSED or cond_result == Proposal.FAILED: new_api_action.revert() else: p = Proposal.objects.create(status=Proposal.FAILED, author=new_api_action.initiator) new_api_action.proposal = p if event.get('type') == 'reaction_added': ts = event['item']['ts'] action = None action_res = PlatformAction.objects.filter(community_post=ts) if action_res.exists(): action = action_res[0] if event['reaction'] == '+1' or event['reaction'] == '-1': if event['reaction'] == '+1': value = True elif event['reaction'] == '-1': value = False user,_ = SlackUser.objects.get_or_create(username=event['user'], community=action.community) uv = BooleanVote.objects.filter(proposal=action.proposal, user=user) if uv.exists(): uv = uv[0] uv.boolean_value = value uv.save() else: uv = BooleanVote.objects.create(proposal=action.proposal, user=user, boolean_value=value) if action == None: action_res = PlatformActionBundle.objects.filter(community_post=ts) if action_res.exists(): action = action_res[0] bundled_actions = list(action.bundled_actions.all()) if event['reaction'] in NUMBERS_TEXT.keys(): num = NUMBERS_TEXT[event['reaction']] voted_action = bundled_actions[num] user,_ = SlackUser.objects.get_or_create(username=event['user'], community=voted_action.community) uv = NumberVote.objects.filter(proposal=voted_action.proposal, user=user) if uv.exists(): uv = uv[0] uv.number_value = 1 uv.save() else: uv = NumberVote.objects.create(proposal=voted_action.proposal, user=user, number_value=1) return HttpResponse("")