def get_state_class(plmobject): """ Returns the state class ("cancelled", "draft", "proposed", "official", or "deprecated") of *plmobject*. """ state, lifecycle = (plmobject.state_id, plmobject.lifecycle_id) if lifecycle == models.get_cancelled_lifecycle().name: return "cancelled" lc = models.Lifecycle.objects.get(name=lifecycle) if state == lc.official_state_id: return "official" if state == lc.first_state.name: return "draft" if state == lc.last_state.name: return "deprecated" return "proposed"
def setUp(self): super(PartDocControllerTestCase, self).setUp() self.other_owner = self.get_contributor("Otherowner") self.other_owner.groups.add(self.group) self.other_owner.save() lcl = LifecycleList("dpop", "official", "draft", "proposed", "official", "deprecated") lc = models.Lifecycle.from_lifecyclelist(lcl) self.states = dict((s, models.State.objects.get(name=s)) for s in lcl) self.states["cancelled"] = models.get_cancelled_state() self.lifecycles = dict.fromkeys(lcl, lc) self.lifecycles["cancelled"] = models.get_cancelled_lifecycle() data = self.DATA.copy() data["lifecycle"] = lc self.part = PartController.create("p1", "Part", "a", self.user, data, True, True) self.doc = DocumentController.create("d1", "Document", "a", self.user, data, True, True)
def cancel(self): """ Cancels the object: * Its lifecycle becomes "cancelled". * Its owner becomes the company. * It removes all signer. """ company = models.User.objects.get(username=settings.COMPANY) self.lifecycle = models.get_cancelled_lifecycle() self.state = models.get_cancelled_state() self.set_owner(company, True) self.users.filter(role__startswith=models.ROLE_SIGN).end() self.plmobjects.end() self._clear_approvals() self.save(with_history=False) self._save_histo("Cancel", "Object cancelled")
def cancel(self): """ Cancels the object: * Its lifecycle becomes "cancelled". * Its owner becomes the company. * It removes all signer. """ company = models.User.objects.get(username=settings.COMPANY) self.lifecycle = models.get_cancelled_lifecycle() self.state = models.get_cancelled_state() self.set_owner(company, True) self.users.filter(role__startswith=models.ROLE_SIGN).end() self._clear_approvals() self.save(with_history=False) self._save_histo("cancelled", "%s (%s//%s//%s) cancelled"%(self.object.name, self.object.type, self.object.reference, self.object.revision)) self._update_state_history()
def get_creation_form(user, data=None, start=0, inbulk_cache=None, **kwargs): u""" Returns a creation form suitable to create an ECR. The returned form can be used, if it is valid, with the function :meth:`~plmapp.controllers.ECRController.create_from_form` to create a :class:`~plmapp.models.ECR` and its associated :class:`~plmapp.controllers.ECRController`. If *data* is provided, it will be used to fill the form. *start* is used if *data* is ``None``, it's usefull if you need to show several initial creation forms at once and you want different references. *inbulk_cache* may be a dictionary to cache lifecycles, groups and other values. It is useful if a page renders several creation forms bound to the same user """ if data is None: initial = get_initial_creation_data(start, inbulk_cache) initial.update(kwargs.pop("initial", {})) form = ECRForm(initial=initial, **kwargs) else: form = ECRForm(data=data, **kwargs) # do not accept the cancelled lifecycle field = form.fields["lifecycle"] field.cache_choices = inbulk_cache is not None form.inbulk_cache = inbulk_cache if inbulk_cache is None or "lifecycles" not in inbulk_cache: lifecycles = m.Lifecycle.objects.filter(type=m.Lifecycle.ECR).\ exclude(pk=m.get_cancelled_lifecycle().pk) form.fields["lifecycle"].queryset = lifecycles if inbulk_cache is not None: inbulk_cache["lifecycles"] = lifecycles list(field.choices) inbulk_cache["lc_cache"] = field.choice_cache else: lifecycles = inbulk_cache["lifecycles"] form.fields["lifecycle"].queryset = lifecycles field.choice_cache = inbulk_cache["lc_cache"] return form
def cancel(self): """ Cancels the object: * Its lifecycle becomes "cancelled". * Its owner becomes the company. * It removes all signer. """ company = models.User.objects.get(username=settings.COMPANY) self.lifecycle = models.get_cancelled_lifecycle() self.state = models.get_cancelled_state() self.set_owner(company, True) self.users.filter(role__startswith=models.ROLE_SIGN).end() self._clear_approvals() self.save(with_history=False) self._save_histo( "cancelled", "%s (%s//%s//%s) cancelled" % (self.object.name, self.object.type, self.object.reference, self.object.revision)) self._update_state_history()
def get_creation_form(user, data=None, start=0, inbulk_cache=None, **kwargs): u""" Returns a creation form suitable to create an ECR. The returned form can be used, if it is valid, with the function :meth:`~plmapp.controllers.ECRController.create_from_form` to create a :class:`~plmapp.models.ECR` and its associated :class:`~plmapp.controllers.ECRController`. If *data* is provided, it will be used to fill the form. *start* is used if *data* is ``None``, it's usefull if you need to show several initial creation forms at once and you want different references. *inbulk_cache* may be a dictionary to cache lifecycles, groups and other values. It is useful if a page renders several creation forms bound to the same user """ if data is None: initial = get_initial_creation_data(start, inbulk_cache) initial.update(kwargs.pop("initial", {})) form = ECRForm(initial=initial, **kwargs) else: form = ECRForm(data=data, **kwargs) # do not accept the cancelled lifecycle field = form.fields["lifecycle"] field.cache_choices = inbulk_cache is not None form.inbulk_cache = inbulk_cache if inbulk_cache is None or "lifecycles" not in inbulk_cache: lifecycles = m.Lifecycle.objects.filter(type=m.Lifecycle.ECR).exclude(pk=m.get_cancelled_lifecycle().pk) form.fields["lifecycle"].queryset = lifecycles if inbulk_cache is not None: inbulk_cache["lifecycles"] = lifecycles list(field.choices) inbulk_cache["lc_cache"] = field.choice_cache else: lifecycles = inbulk_cache["lifecycles"] form.fields["lifecycle"].queryset = lifecycles field.choice_cache = inbulk_cache["lc_cache"] return form
def browse_ecr(request): user = request.user if not user.profile.restricted: # only authenticated users can see all groups and users obj, ctx = bv.get_generic_data(request, search=False) object_list = ECR.objects.all() # this is only relevant for authenticated users ctx["state"] = state = request.GET.get("state", "all") if state == "official": object_list = object_list.\ exclude(lifecycle=models.get_cancelled_lifecycle()).\ filter(state=F("lifecycle__official_state")) ctx["plmobjects"] = False ctime_filter = SimpleDateFilter("ctime", request, ECR, "ctime") object_list = ctime_filter.queryset(request, object_list) mtime_filter = SimpleDateFilter("mtime", request, ECR, "mtime") object_list = mtime_filter.queryset(request, object_list) ctx["time_filters"] = [ctime_filter, mtime_filter] else: ctx = bv.init_ctx("-", "-", "-") ctx.update({ 'is_readable': True, 'restricted': True, 'object_menu': [], 'is_contributor': False, 'navigation_history': [], }) readable = user.ecrs.now().filter(role=models.ROLE_READER) readable |= user.ecrs.now().filter(role=models.ROLE_OWNER) object_list = ECR.objects.filter(id__in=readable) ctx.update(get_pagination(request, object_list, "ECR")) extra_types = [c.__name__ for c in models.IObject.__subclasses__()] ctx.update({ "object_type": _("Browse"), "type": "ECR", "extra_types": extra_types, }) return r2r("browse_ecr.html", ctx, request)
def is_cancelled(self): """ True if the object is cancelled. """ return self.lifecycle == pmodels.get_cancelled_lifecycle()
def __init__(self, user, start, inbulk_cache, *args, **kwargs): data = kwargs.get("data") if data is None: initial = get_initial_creation_data(user, self.Meta.model, start, inbulk_cache) initial.update(kwargs.pop("initial", {})) kwargs["initial"] = initial super(PLMObjectCreationForm, self).__init__(user, start, inbulk_cache, *args, **kwargs) # lifecycles and groups are cached if inbulk_cache is a dictionary # this is an optimization if several creation forms are displayed # in one request # for example, the decomposition of a STEP file can display # a lot of creation forms # display only valid groups field = self.fields["group"] field.cache_choices = inbulk_cache is not None if inbulk_cache is None or "group" not in inbulk_cache: groups = user.groups.all().values_list("id", flat=True) field.queryset = m.GroupInfo.objects.filter(id__in=groups).order_by("name") if inbulk_cache is not None: # a bit ugly but ModelChoiceField reevalute the # queryset if cache_choices is False and choice_cache # is not populated inbulk_cache["group"] = field.queryset list(field.choices) # populates choice_cache inbulk_cache["gr_cache"] = field.choice_cache else: field.queryset = inbulk_cache["group"] field.choice_cache = inbulk_cache["gr_cache"] field.error_messages["invalid_choice"] = INVALID_GROUP if data is None and "group" not in initial: # set initial value of group to the last selected group try: field.initial = inbulk_cache["gr_initial"] except (KeyError, TypeError): try: last_created_object = m.PLMObject.objects.filter(creator=user).order_by("-ctime")[0] last_group = last_created_object.group except IndexError: last_group = field.queryset[0] if field.queryset else None if last_group in field.queryset: field.initial = last_group if inbulk_cache: inbulk_cache["gr_initial"] = field.initial # do not accept the cancelled lifecycle field = self.fields["lifecycle"] field.cache_choices = inbulk_cache is not None if inbulk_cache is None or "lifecycles" not in inbulk_cache: lifecycles = m.Lifecycle.objects.filter(type__in=(m.Lifecycle.STANDARD, m.Lifecycle.TEMPLATE)).\ exclude(pk=m.get_cancelled_lifecycle().pk).order_by("name") self.fields["lifecycle"].queryset = lifecycles if inbulk_cache is not None: inbulk_cache["lifecycles"] = lifecycles list(field.choices) inbulk_cache["lc_cache"] = field.choice_cache else: lifecycles = inbulk_cache["lifecycles"] self.fields["lifecycle"].queryset = lifecycles field.choice_cache = inbulk_cache["lc_cache"]
def __init__(self, user, start, inbulk_cache, *args, **kwargs): data = kwargs.get("data") if data is None: initial = get_initial_creation_data(user, self.Meta.model, start, inbulk_cache) initial.update(kwargs.pop("initial", {})) kwargs["initial"] = initial self.start = start self.inbulk_cache = inbulk_cache self.user = user super(PLMObjectCreationForm, self).__init__(*args, **kwargs) # lifecycles and groups are cached if inbulk_cache is a dictionary # this is an optimization if several creation forms are displayed # in one request # for example, the decomposition of a STEP file can display # a lot of creation forms # display only valid groups field = self.fields["group"] field.cache_choices = inbulk_cache is not None if inbulk_cache is None or "group" not in inbulk_cache: groups = user.groups.all().values_list("id", flat=True) field.queryset = m.GroupInfo.objects.filter( id__in=groups).order_by("name") if inbulk_cache is not None: # a bit ugly but ModelChoiceField reevalute the # queryset if cache_choices is False and choice_cache # is not populated inbulk_cache["group"] = field.queryset list(field.choices) # populates choice_cache inbulk_cache["gr_cache"] = field.choice_cache else: field.queryset = inbulk_cache["group"] field.choice_cache = inbulk_cache["gr_cache"] field.error_messages["invalid_choice"] = INVALID_GROUP if data is None and "group" not in initial: # set initial value of group to the last selected group try: field.initial = inbulk_cache["gr_initial"] except (KeyError, TypeError): try: last_created_object = m.PLMObject.objects.filter( creator=user).order_by("-ctime")[0] last_group = last_created_object.group except IndexError: last_group = field.queryset[0] if field.queryset else None if last_group in field.queryset: field.initial = last_group if inbulk_cache: inbulk_cache["gr_initial"] = field.initial # do not accept the cancelled lifecycle field = self.fields["lifecycle"] field.cache_choices = inbulk_cache is not None if inbulk_cache is None or "lifecycles" not in inbulk_cache: lifecycles = m.Lifecycle.objects.filter(type=m.Lifecycle.STANDARD).\ exclude(pk=m.get_cancelled_lifecycle().pk).order_by("name") self.fields["lifecycle"].queryset = lifecycles if inbulk_cache is not None: inbulk_cache["lifecycles"] = lifecycles list(field.choices) inbulk_cache["lc_cache"] = field.choice_cache else: lifecycles = inbulk_cache["lifecycles"] self.fields["lifecycle"].queryset = lifecycles field.choice_cache = inbulk_cache["lc_cache"]