def get_all_parts(request): """ Returns all the types of :class:`.Part` managed by the server. :implements: :func:`http_api.parts` """ return {"types" : sorted(models.get_all_parts().keys())}
def ajax_creation_form(request): """ Simple view which returns the html of a creation form with the data of :attr:`request.GET` as initial values. The request must contains a get parameter *type* with a valid type, otherwise, a :class:`.HttpResponseForbidden` is returned. """ tf = forms.TypeForm(request.GET) if tf.is_valid(): type_ = tf.cleaned_data["type"] request.session["type"] = type_ request.session.save() cls = models.get_all_users_and_plmobjects()[type_] view = get_creation_view(cls) if view is not None: return {"reload" : True} initial = dict(request.GET.iteritems()) if "pfiles" in request.GET: initial["pfiles"] = request.GET.getlist("pfiles") if "reference" in initial: # gets a new reference if the type switches from a part to a document # and vice versa, see ticket #99 ref = initial["reference"] if (ref.startswith("DOC_") and type_ in models.get_all_parts()) or \ (ref.startswith("PART_") and type_ in models.get_all_documents()): del initial["reference"] form = forms.get_creation_form(request.user, cls, initial=initial) return {"reload" : False, "form" : form.as_table(), "type" : type_, "form_media": form.media.render(), } else: return HttpResponseForbidden()
def get_all_parts(request): """ Returns all the types of :class:`.Part` managed by the server. :implements: :func:`http_api.parts` """ return {"types": sorted(models.get_all_parts().keys())}
def add_child(request, obj_type, obj_ref, obj_revi): """ View to add a child to a part. :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/bom-child/add/` .. include:: views_params.txt **Template:** :file:`parts/bom_add.html` **Context:** ``RequestContext`` ``add_child_form`` a form to add a child (:class:`.AddChildForm`) ``link`` :class:`.ParentChildLink` being replaced ``link_creation`` Set to True ``attach`` set to (*obj*, "add_child") """ obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi, load_all=True) if request.method == "POST" and request.POST: add_child_form = forms.AddChildForm(obj.object, request.POST) if add_child_form.is_valid(): child_obj = get_obj_from_form(add_child_form, request.user) obj.add_child(child_obj, add_child_form.cleaned_data["quantity"], add_child_form.cleaned_data["order"], add_child_form.cleaned_data["unit"], **add_child_form.extensions) return HttpResponseRedirect(obj.plmobject_url + "BOM-child/") else: if "type" in request.GET and request.GET["type"] in models.get_all_parts(): # use GET params only if they seems valid initial = request.GET else: initial = None add_child_form = forms.AddChildForm(obj.object, initial=initial) ctx['current_page'] = 'BOM-child' if ctx["results"]: obj.precompute_can_add_child2() orders = list(obj.parentchildlink_parent.values_list('order', flat=True)) initial_order = max(orders) + 10 if orders else 10 ctx['order'] = initial_order ctx.update({'link_creation': True, 'add_child_form': add_child_form, 'attach' : (obj, "add_child")}) return r2r('parts/bom_add.html', ctx, request)
def can_add_type(parent_type, child_type): """ Used in Bom View. :param parent_type: Type of the parent, the current part :param child_type: type of the object that may be added as child :return: True if child_type is a type of object that can be added to parent_type """ part_types = models.get_all_parts() return parent_type in part_types and child_type in part_types
def can_link(current_type, suggested_type): """ Used in Doc-Parts views. :param current_type: type of the current object (part or document) :param suggested_type: type of the object that may be attached to the current one :return: True if current_type is a type of object that can be attached to current_type object """ doc_types = models.get_all_documents() part_types = models.get_all_parts() return ((current_type in doc_types and suggested_type in part_types) or (current_type in part_types and suggested_type in doc_types))
def main_type(obj): if isinstance(obj, (User, UserController)): return "user" if isinstance(obj, (models.Part, PartController)) or getattr(obj, "is_part", False): return "part" if (isinstance(obj, (models.Document, DocumentController)) or getattr(obj, "is_document", False)): return "document" if isinstance(obj, (Group, GroupController)): return "group" if hasattr(obj, "has_key") and "type" in obj: if obj["type"] in models.get_all_parts(): return "part" if obj["type"] in models.get_all_documents(): return "document" return obj["type"] return getattr(obj, "type", "").lower()
def ajax_part_creation_form(request, prefix): """ It updates the form of an assembly determined by **prefix** without recharging the whole page and respecting the information introduced up to the moment The attributes can change depending on the type of part selected """ tf = forms.Doc_Part_type_Form(request.GET, prefix=prefix) if tf.is_valid(): cls = pmodels.get_all_parts()[tf.cleaned_data["type_part"]] cf = pforms.get_creation_form(request.user, cls, prefix=prefix+"-part", data=dict(request.GET.iteritems())) return r2r("extra_attributes.html", {"creation_form" : cf}, request) return HttpResponseForbidden()
def main_type(obj): if isinstance(obj, (User, UserController)): return "user" if isinstance(obj, (models.Part, PartController)) or getattr( obj, "is_part", False): return "part" if (isinstance(obj, (models.Document, DocumentController)) or getattr(obj, "is_document", False)): return "document" if isinstance(obj, (Group, GroupController)): return "group" if hasattr(obj, "has_key") and "type" in obj: if obj["type"] in models.get_all_parts(): return "part" if obj["type"] in models.get_all_documents(): return "document" return obj["type"] return getattr(obj, "type", "").lower()
def get_attached_parts(self): types = models.get_all_parts().keys() return self.plmobject_group.exclude_cancelled().filter(type__in=types)
def add_child(request, obj_type, obj_ref, obj_revi): """ View to add a child to a part. :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/bom-child/add/` .. include:: views_params.txt **Template:** :file:`parts/bom_add.html` **Context:** ``RequestContext`` ``add_child_form`` a form to add a child (:class:`.AddChildForm`) ``link`` :class:`.ParentChildLink` being replaced ``link_creation`` Set to True ``attach`` set to (*obj*, "add_child") """ obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi, load_all=True) if request.method == "POST" and request.POST: add_child_form = forms.AddChildForm(obj.object, request.POST) if add_child_form.is_valid(): child_obj = get_obj_from_form(add_child_form, request.user) obj.add_child(child_obj, add_child_form.cleaned_data["quantity"], add_child_form.cleaned_data["order"], add_child_form.cleaned_data["unit"], **add_child_form.extensions) return HttpResponseRedirect(obj.plmobject_url + "BOM-child/") else: if "type" in request.GET and request.GET[ "type"] in models.get_all_parts(): # use GET params only if they seems valid initial = request.GET else: initial = None add_child_form = forms.AddChildForm(obj.object, initial=initial) ctx['current_page'] = 'BOM-child' if ctx["results"]: obj.precompute_can_add_child2() orders = list( obj.parentchildlink_parent.values_list('order', flat=True)) initial_order = max(orders) + 10 if orders else 10 ctx['order'] = initial_order ctx.update({ 'link_creation': True, 'add_child_form': add_child_form, 'attach': (obj, "add_child") }) return r2r('parts/bom_add.html', ctx, request)
def get_attached_parts(self, time=None): types = models.get_all_parts().keys() return self.plmobjects.filter(plmobject__type__in=types).at(time)
def is_part(plmobject): return plmobject["type"] in models.get_all_parts()