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) 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 _bulk_update_order(self, order_field, request, **kwargs): validator = validators.UpdateUserStoriesOrderBulkValidator( 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"]) status = None status_id = data.get("status_id", None) if status_id is not None: status = get_object_or_404(UserStoryStatus, 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) self.check_permissions(request, "bulk_update_order", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) ret = services.update_userstories_order_in_bulk(data["bulk_stories"], order_field, project, status=status, milestone=milestone) return response.Ok(ret)
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"], 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_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 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 bulk_create(self, request, **kwargs): validator = validators.UserStoriesBulkValidator(data=request.DATA) if validator.is_valid(): 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")) user_stories = services.create_userstories_in_bulk( data["bulk_stories"], project=project, owner=request.user, status_id=data.get("status_id") or project.default_us_status_id, callback=self.post_save, precall=self.pre_save) user_stories = self.get_queryset().filter( id__in=[i.id for i in user_stories]) for user_story in user_stories: self.persist_history_snapshot(obj=user_story) user_stories_serialized = self.get_serializer_class()(user_stories, many=True) return response.Ok(user_stories_serialized.data) return response.BadRequest(validator.errors)
def bulk_create(self, request, **kwargs): serializer = serializers.MembersBulkSerializer(data=request.DATA) if not serializer.is_valid(): return response.BadRequest(serializer.errors) data = serializer.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")) # TODO: this should be moved to main exception handler instead # of handling explicit exception catchin here. if "bulk_memberships" in data and isinstance(data["bulk_memberships"], list): (enough_slots, not_enough_slots_error) = users_service.has_available_slot_for_project( request.user, project=project, members=len(data["bulk_memberships"]) ) if not enough_slots: raise exc.BadRequest(not_enough_slots_error) try: 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) except 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 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.TasksBulkValidator(data=request.DATA) if validator.is_valid(): 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")) tasks = services.create_tasks_in_bulk( data["bulk_tasks"], milestone_id=data["sprint_id"], user_story_id=data["us_id"], status_id=data.get("status_id") or project.default_task_status_id, project=project, owner=request.user, callback=self.post_save, precall=self.pre_save) tasks = self.get_queryset().filter(id__in=[i.id for i in tasks]) tasks_serialized = self.get_serializer_class()(tasks, many=True) return response.Ok(tasks_serialized.data) return response.BadRequest(validator.errors)
def bulk_update_order(self, request, **kwargs): contenttype = self.get_content_type() # Validate data data = request.DATA.copy() data["content_type_id"] = contenttype.id validator = validators.UpdateAttachmentsOrderBulkValidator(data=data) if not validator.is_valid(): return response.BadRequest(validator.errors) # Get and validate permissions item = contenttype.get_object_for_this_type(pk=data["object_id"]) self.check_permissions(request, "bulk_update_order", item.project) if item.project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) # Get after_attachment after_attachment = None after_attachment_id = data.get("after_attachment_id", None) if after_attachment_id is not None: after_attachment = get_object_or_error(item.attachments, request.user, pk=after_attachment_id) ret = services.update_order_in_bulk(item=item, after_attachment=after_attachment, bulk_attachments=data["bulk_attachments"]) return response.Ok(ret)
def bulk_update_kanban_order(self, request, **kwargs): # Validate data validator = validators.UpdateUserStoriesKanbanOrderBulkValidator( data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data # Get and validate project permissions project = get_object_or_error(Project, request.user, pk=data["project_id"]) self.check_permissions(request, "bulk_update_order", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) # Get status status = get_object_or_error(UserStoryStatus, request.user, pk=data["status_id"], project=project) # Get swimlane swimlane = None swimlane_id = data.get("swimlane_id", None) if swimlane_id is not None: swimlane = get_object_or_error(Swimlane, request.user, pk=swimlane_id, project=project) # Get after_userstory after_userstory = None after_userstory_id = data.get("after_userstory_id", None) if after_userstory_id is not None: after_userstory = get_object_or_error(models.UserStory, request.user, pk=after_userstory_id, project=project) # Get before_userstory before_userstory = None before_userstory_id = data.get("before_userstory_id", None) if before_userstory_id is not None: before_userstory = get_object_or_error(models.UserStory, request.user, pk=before_userstory_id, project=project) ret = services.update_userstories_kanban_order_in_bulk( user=request.user, project=project, status=status, swimlane=swimlane, after_userstory=after_userstory, before_userstory=before_userstory, bulk_userstories=data["bulk_userstories"]) return response.Ok(ret)
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_backlog_order(self, request, **kwargs): # Validate data validator = validators.UpdateUserStoriesBacklogOrderBulkValidator( data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data # Get and validate project permissions project = get_object_or_error(Project, request.user, pk=data["project_id"]) self.check_permissions(request, "bulk_update_order", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) # Get milestone milestone = None milestone_id = data.get("milestone_id", None) if milestone_id is not None: milestone = get_object_or_error(Milestone, request.user, pk=milestone_id, project=project) # Get after_userstory after_userstory = None after_userstory_id = data.get("after_userstory_id", None) if after_userstory_id is not None: after_userstory = get_object_or_error(models.UserStory, request.user, pk=after_userstory_id, project=project) # Get before_userstory before_userstory = None before_userstory_id = data.get("before_userstory_id", None) if before_userstory_id is not None: before_userstory = get_object_or_error(models.UserStory, request.user, pk=before_userstory_id, project=project) ret = services.update_userstories_backlog_or_sprint_order_in_bulk( user=request.user, project=project, milestone=milestone, after_userstory=after_userstory, before_userstory=before_userstory, bulk_userstories=data["bulk_userstories"]) return response.Ok(ret)
def _bulk_update_order(self, order_field, request, **kwargs): serializer = serializers.UpdateUserStoriesOrderBulkSerializer(data=request.DATA) if not serializer.is_valid(): return response.BadRequest(serializer.errors) data = serializer.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")) services.update_userstories_order_in_bulk(data["bulk_stories"], project=project, field=order_field) services.snapshot_userstories_in_bulk(data["bulk_stories"], request.user) return response.NoContent()
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"]) self.check_permissions(request, 'bulk_create', project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) if not request.user.verified_email: return response.BadRequest({ "_error_message": _("To add members to a project, first you have to verify your email address" ) }) bulk_memberships = data["bulk_memberships"] invitation_extra_text = data.get("invitation_extra_text", None) try: with advisory_lock("membership-creation-{}".format(project.id)): members = services.get_members_from_bulk( bulk_memberships, project=project, invited_by=request.user, invitation_extra_text=invitation_extra_text) self._check_if_new_members_can_be_created(project, members) services.create_members_in_bulk(members, callback=self.post_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 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 new_name = data.get("name", "") new_description = data.get("description", "") new_owner = self.request.user new_is_private = data.get('is_private', False) new_members = data.get("users", []) # Validate if the project can be imported (enough_slots, error_message, total_members) = services.check_if_project_can_be_duplicate( project=project, new_owner=new_owner, new_is_private=new_is_private, new_user_id_members=[m["id"] for m in new_members]) if not enough_slots: raise exc.NotEnoughSlotsForProject(new_is_private, total_members, error_message) new_project = services.duplicate_project(project=project, name=new_name, description=new_description, owner=new_owner, is_private=new_is_private, users=new_members) new_project = get_object_or_error(self.get_queryset(), request.user, id=new_project.id) serializer = self.get_serializer(new_project) return response.Created(serializer.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"))