Пример #1
0
def _clean_revision(self):
    data = self.cleaned_data["revision"]
    try:
        validate_revision(data)
    except ValueError as e:
        raise ValidationError(unicode(e))
    return re.sub("\s+", " ", data.strip(" "))
Пример #2
0
def _clean_revision(self):
    data = self.cleaned_data["revision"]
    try:
        validate_revision(data)
    except ValueError as e:
        raise ValidationError(unicode(e))
    return re.sub("\s+", " ", data.strip(" "))
Пример #3
0
    def revise(self, new_revision, group=None):
        u"""
        Makes a new revision: duplicates :attr:`object`. The duplicated
        object's revision is *new_revision*.

        Returns a controller of the new object.
        """
        self.check_readable()
        if not new_revision or new_revision == self.revision:
            raise RevisionError("Bad value for new_revision")
        try:
            validate_revision(new_revision)
        except ValueError as e:
            raise RevisionError(unicode(e))
        if self.is_cancelled or self.is_deprecated:
            raise RevisionError("Object is deprecated or cancelled.")
        if models.RevisionLink.objects.now().filter(old=self.object.pk).exists():
            raise RevisionError("A revision already exists for %s" % self.object)
        if group is None:
            group = self.object.group
        if not group.user_set.filter(id=self._user.id).exists():
            raise ValueError("Invalid group")
        data = {}
        fields = self.get_modification_fields() + self.get_creation_fields()
        for attr in fields:
            if attr not in ("reference", "type", "revision"):
                data[attr] = getattr(self.object, attr)
        data["group"] = group
        data["state"] = models.get_default_state(self.lifecycle)
        new_controller = self.create(self.reference, self.type, new_revision,
                                     self._user, data)
        details = "old : %s, new : %s" % (self.object, new_controller.object)
        self._save_histo(models.RevisionLink.ACTION_NAME, details)
        models.RevisionLink.objects.create(old=self.object, new=new_controller.object)
        return new_controller
Пример #4
0
    def revise(self, new_revision, group=None):
        u"""
        Makes a new revision: duplicates :attr:`object`. The duplicated
        object's revision is *new_revision*.

        Returns a controller of the new object.
        """
        self.check_readable()
        if not new_revision or new_revision == self.revision:
            raise RevisionError("Bad value for new_revision")
        try:
            validate_revision(new_revision)
        except ValueError as e:
            raise RevisionError(unicode(e))
        if self.is_cancelled or self.is_deprecated:
            raise RevisionError("Object is deprecated or cancelled.")
        if models.RevisionLink.objects.now().filter(
                old=self.object.pk).exists():
            raise RevisionError("A revision already exists for %s" %
                                self.object)
        if group is None:
            group = self.object.group
        if not group.user_set.filter(id=self._user.id).exists():
            raise ValueError("Invalid group")
        data = {}
        fields = self.get_modification_fields() + self.get_creation_fields()
        for attr in fields:
            if attr not in ("reference", "type", "revision"):
                data[attr] = getattr(self.object, attr)
        data["group"] = group
        data["state"] = models.get_default_state(self.lifecycle)
        new_controller = self.create(self.reference, self.type, new_revision,
                                     self._user, data)
        details = "old : %s, new : %s" % (self.object, new_controller.object)
        self._save_histo(models.RevisionLink.ACTION_NAME, details)
        models.RevisionLink.objects.create(old=self.object,
                                           new=new_controller.object)
        return new_controller
Пример #5
0
    def create(cls,
               reference,
               type,
               revision,
               user,
               data={},
               block_mails=False,
               no_index=False):
        u"""
        This method builds a new :class:`.PLMObject` of
        type *class_* and return a :class:`PLMObjectController` associated to
        the created object.

        Raises :exc:`ValueError` if *reference*, *type* or *revision* are
        empty. Raises :exc:`ValueError` if *type* is not valid.

        :param reference: reference of the objet
        :param type: type of the object
        :param revision: revision of the object
        :param user: user who creates/owns the object
        :param data: a dict<key, value> with informations to add to the plmobject
        :rtype: :class:`PLMObjectController`
        """

        profile = user.profile
        if not (profile.is_contributor or profile.is_administrator):
            raise PermissionError("%s is not a contributor" % user)
        if not user.is_active:
            raise PermissionError(u"%s's account is inactive" % user)
        if profile.restricted:
            raise PermissionError(
                "Restricted account can not create a part or document.")
        if not reference or not type or not revision:
            raise ValueError(
                "Empty value not permitted for reference/type/revision")
        validate_reference(reference)
        validate_revision(revision)
        try:
            class_ = models.get_all_plmobjects()[type]
        except KeyError:
            raise ValueError("Incorrect type")
        # create an object
        reference_number = parse_reference_number(reference, class_)
        obj = class_(reference=reference,
                     type=type,
                     revision=revision,
                     owner=user,
                     creator=user,
                     reference_number=reference_number)
        if no_index:
            obj.no_index = True
        if data:
            for key, value in data.iteritems():
                if key not in [
                        "reference", "type", "revision", "auto", "pfiles"
                ]:
                    setattr(obj, key, value)
        obj.state = models.get_default_state(obj.lifecycle)
        obj.save()
        res = cls(obj, user)
        if block_mails:
            res.block_mails()
        # record creation in history
        infos = {"type": type, "reference": reference, "revision": revision}
        infos.update(data)
        details = u" / ".join(u"%s : %s" % (k, v) for k, v in infos.items()
                              if k not in ("auto", "pfiles", "type",
                                           "reference", "revision", "name"))
        res._save_histo("created", details)

        # add links (bulk create)
        ctime = obj.ctime
        links = [
            models.PLMObjectUserLink(plmobject=obj,
                                     user=user,
                                     role="owner",
                                     ctime=ctime)
        ]
        try:
            l = models.DelegationLink.current_objects.select_related(
                "delegator").get(delegatee=user, role=models.ROLE_SPONSOR)
            sponsor = l.delegator
            if sponsor.username == settings.COMPANY:
                sponsor = user
            elif not res.check_in_group(sponsor, False):
                sponsor = user
        except models.DelegationLink.DoesNotExist:
            sponsor = user
        # the user can promote to the next state
        links.append(
            models.PLMObjectUserLink(plmobject=obj,
                                     user=user,
                                     role=level_to_sign_str(0),
                                     ctime=ctime))
        # from the next state, only the sponsor can promote this object
        for i in range(1, obj.lifecycle.nb_states - 1):
            links.append(
                models.PLMObjectUserLink(plmobject=obj,
                                         user=sponsor,
                                         role=level_to_sign_str(i),
                                         ctime=ctime))
        models.PLMObjectUserLink.objects.bulk_create(links)

        res._update_state_history()
        return res
Пример #6
0
    def create(cls, reference, type, revision, user, data={}, block_mails=False,
            no_index=False):
        u"""
        This method builds a new :class:`.PLMObject` of
        type *class_* and return a :class:`PLMObjectController` associated to
        the created object.

        Raises :exc:`ValueError` if *reference*, *type* or *revision* are
        empty. Raises :exc:`ValueError` if *type* is not valid.

        :param reference: reference of the objet
        :param type: type of the object
        :param revision: revision of the object
        :param user: user who creates/owns the object
        :param data: a dict<key, value> with informations to add to the plmobject
        :rtype: :class:`PLMObjectController`
        """

        profile = user.profile
        if not (profile.is_contributor or profile.is_administrator):
            raise PermissionError("%s is not a contributor" % user)
        if not user.is_active:
            raise PermissionError(u"%s's account is inactive" % user)
        if profile.restricted:
            raise PermissionError("Restricted account can not create a part or document.")
        if not reference or not type or not revision:
            raise ValueError("Empty value not permitted for reference/type/revision")
        validate_reference(reference)
        validate_revision(revision)
        try:
            class_ = models.get_all_plmobjects()[type]
        except KeyError:
            raise ValueError("Incorrect type")
        # create an object
        reference_number = parse_reference_number(reference, class_)
        obj = class_(reference=reference, type=type, revision=revision,
                     owner=user, creator=user, reference_number=reference_number)
        if no_index:
            obj.no_index = True
        if data:
            for key, value in data.iteritems():
                if key not in ["reference", "type", "revision", "auto", "pfiles"]:
                    setattr(obj, key, value)
        obj.state = models.get_default_state(obj.lifecycle)
        obj.save()
        res = cls(obj, user)
        if block_mails:
            res.block_mails()
        # record creation in history
        infos = {"type" : type, "reference" : reference, "revision" : revision}
        infos.update(data)
        details = u" / ".join(u"%s : %s" % (k, v) for k, v in infos.items()
                if k not in ("auto", "pfiles", "type", "reference", "revision", "name"))
        res._save_histo("created", details)

        # add links (bulk create)
        ctime = obj.ctime
        links = [models.PLMObjectUserLink(plmobject=obj, user=user, role="owner", ctime=ctime)]
        try:
            l = models.DelegationLink.current_objects.select_related("delegator").get(delegatee=user,
                    role=models.ROLE_SPONSOR)
            sponsor = l.delegator
            if sponsor.username == settings.COMPANY:
                sponsor = user
            elif not res.check_in_group(sponsor, False):
                sponsor = user
        except models.DelegationLink.DoesNotExist:
            sponsor = user
        # the user can promote to the next state
        links.append(models.PLMObjectUserLink(plmobject=obj, user=user,
            role=level_to_sign_str(0), ctime=ctime))
        # from the next state, only the sponsor can promote this object
        for i in range(1, obj.lifecycle.nb_states - 1):
            links.append(models.PLMObjectUserLink(plmobject=obj, user=sponsor,
                role=level_to_sign_str(i), ctime=ctime))
        models.PLMObjectUserLink.objects.bulk_create(links)

        res._update_state_history()
        return res