Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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 not request.user.verified_email:
            return response.BadRequest({
                "_error_message":
                _("To add members to a project, first you have to verify your email address"
                  )
            })

        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)