def bulk_create(self, request, **kwargs): validator = validators.EpicsBulkValidator(data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data project = Project.objects.get(id=data["project_id"]) self.check_permissions(request, "bulk_create", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) epics = services.create_epics_in_bulk( data["bulk_epics"], status_id=data.get("status_id") or project.default_epic_status_id, project=project, owner=request.user, callback=self.post_save, precall=self.pre_save) epics = self.get_queryset().filter(id__in=[i.id for i in epics]) for epic in epics: self.persist_history_snapshot(obj=epic) epics_serialized = self.get_serializer_class()(epics, many=True) return response.Ok(epics_serialized.data)
def bulk_create(self, request, **kwargs): validator = validators.CreateRelatedUserStoriesBulkValidator( data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data epic = get_object_or_404(models.Epic, id=kwargs["epic"]) project = Project.objects.get(pk=data.get('project_id')) self.check_permissions(request, 'bulk_create', project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) related_userstories = services.create_related_userstories_in_bulk( data["bulk_userstories"], epic, project=project, owner=request.user) for related_userstory in related_userstories: self.persist_history_snapshot(obj=related_userstory) self.persist_history_snapshot(obj=related_userstory.user_story) related_uss_serialized = self.get_serializer_class()( epic.relateduserstory_set.all(), many=True) return response.Ok(related_uss_serialized.data)
def bulk_create(self, request, **kwargs): validator = validators.IssuesBulkValidator(data=request.DATA) if validator.is_valid(): data = validator.data project = Project.objects.get(pk=data["project_id"]) self.check_permissions(request, 'bulk_create', project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) issues = services.create_issues_in_bulk( data["bulk_issues"], milestone_id=data["milestone_id"], project=project, owner=request.user, status=project.default_issue_status, severity=project.default_severity, priority=project.default_priority, type=project.default_issue_type, callback=self.post_save, precall=self.pre_save) issues = self.get_queryset().filter(id__in=[i.id for i in issues]) issues_serialized = self.get_serializer_class()(issues, many=True) return response.Ok(data=issues_serialized.data) return response.BadRequest(validator.errors)
def _bulk_update_order(self, order_field, request, **kwargs): validator = validators.UpdateTasksOrderBulkValidator(data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data project = get_object_or_404(Project, pk=data["project_id"]) self.check_permissions(request, "bulk_update_order", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) user_story = None user_story_id = data.get("user_story_id", None) if user_story_id is not None: user_story = get_object_or_404(UserStory, pk=user_story_id) status = None status_id = data.get("status_id", None) if status_id is not None: status = get_object_or_404(TaskStatus, pk=status_id) milestone = None milestone_id = data.get("milestone_id", None) if milestone_id is not None: milestone = get_object_or_404(Milestone, pk=milestone_id) ret = services.update_tasks_order_in_bulk(data["bulk_tasks"], order_field, project, user_story=user_story, status=status, milestone=milestone) return response.Ok(ret)
def duplicate(self, request, pk=None): project = self.get_object() self.check_permissions(request, "duplicate", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) validator = validators.DuplicateProjectValidator(data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data # Validate if the project can be imported is_private = data.get('is_private', False) total_memberships = len(data.get("users", [])) + 1 (enough_slots, error_message) = users_services.has_available_slot_for_new_project( self.request.user, is_private, total_memberships) if not enough_slots: raise exc.NotEnoughSlotsForProject(is_private, total_memberships, error_message) new_project = services.duplicate_project( project=project, owner=request.user, name=data["name"], description=data["description"], is_private=data["is_private"], users=data["users"]) new_project = get_object_or_404(self.get_queryset(), id=new_project.id) serializer = self.get_serializer(new_project) return response.Created(serializer.data)
def resend(self, request, pk=None): webhooklog = self.get_object() self.check_permissions(request, 'resend', webhooklog) webhook = webhooklog.webhook if webhook.project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) webhooklog = tasks.resend_webhook(webhook.id, webhook.url, webhook.key, webhooklog.request_data) log = serializers.WebhookLogSerializer(webhooklog) return response.Ok(log.data)
def bulk_update_order(self, request, **kwargs): bulk_data = request.DATA.get(self.bulk_update_param, None) if bulk_data is None: raise exc.BadRequest(_("'{param}' parameter is mandatory".format(param=self.bulk_update_param))) project_id = request.DATA.get('project', None) if project_id is None: raise exc.BadRequest(_("'project' parameter is mandatory")) project = get_object_or_404(Project, id=project_id) self.check_permissions(request, 'bulk_update_order', project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) self.__class__.bulk_update_order_action(project, request.user, bulk_data) return response.NoContent(data=None)
def bulk_create(self, request, **kwargs): context = {"request": request} validator = validators.MembersBulkValidator(data=request.DATA, context=context) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data project = models.Project.objects.get(id=data["project_id"]) invitation_extra_text = data.get("invitation_extra_text", None) self.check_permissions(request, 'bulk_create', project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) if "bulk_memberships" in data and isinstance(data["bulk_memberships"], list): total_new_memberships = len(data["bulk_memberships"]) self._check_if_project_can_have_more_memberships( project, total_new_memberships) try: with advisory_lock("membership-creation-{}".format(project.id)): members = services.create_members_in_bulk( data["bulk_memberships"], project=project, invitation_extra_text=invitation_extra_text, callback=self.post_save, precall=self.pre_save) signal_members_added.send(sender=self.__class__, user=self.request.user, project=project, new_members=members) except exc.ValidationError as err: return response.BadRequest(err.message_dict) members_serialized = self.admin_serializer_class(members, many=True) return response.Ok(members_serialized.data)
def create(self, request, *args, **kwargs): project = self._get_project(request) if not project: raise exc.BadRequest(_("The project doesn't exist")) if not self._validate_signature(project, request): raise exc.BadRequest(_("Bad signature")) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) event_name = self._get_event_name(request) payload = self._get_payload(request) event_hook_class = self.event_hook_classes.get(event_name, None) if event_hook_class is not None: event_hook = event_hook_class(project, payload) try: event_hook.process_event() except ActionSyntaxException as e: raise exc.BadRequest(e) return response.NoContent()
def _raise_if_blocked(self, project): if self.is_blocked(project): raise exc.Blocked(_("Blocked element"))