Esempio n. 1
0
def get_concept_nature_set(request, obj):
    """ 
    Retrieves the list of natures for a concept. 
    It is intended for the Concept form, and uniformizes the different sources for this list of natures 
    1. Tries to retrieve it from the collecster payload in the request (populated by the ajax view, for live changes to the nature values) 
    2. Tries to retrieve it from the POST data (usefull when there are errors in the Concept form, and it is re-presented to the user) 
    3. Tries to retrieve it from the concept object (usefull when the form is used to change an existing Concept) 
    """
    ConceptNatureFormSet = modelformset_factory(ConceptNature, fields="__all__")
    concept = obj if obj else utils_payload.get_request_payload(request, "concept")

    nature_set = utils_payload.get_request_payload(request, "nature_set", [])

    if not nature_set and request.method == "POST":
        formset = ConceptNatureFormSet(request.POST, prefix="additional_nature_set")
        formset.is_valid() # populate the cleaned_data member of each form
        for form in formset:
            if form.cleaned_data.get("nature"):
                nature_set.append(form.cleaned_data.get("nature"))
        if nature_set and concept and concept.primary_nature:
            nature_set.insert(0, concept.primary_nature)

    if not nature_set and concept:
        nature_set = list(concept.all_nature_tuple)

    return nature_set
Esempio n. 2
0
def get_concept_nature_set(request, obj):
    """ 
    Retrieves the list of natures for a concept. 
    It is intended for the Concept form, and uniformizes the different sources for this list of natures 
    1. Tries to retrieve it from the collecster payload in the request (populated by the ajax view, for live changes to the nature values) 
    2. Tries to retrieve it from the POST data (usefull when there are errors in the Concept form, and it is re-presented to the user) 
    3. Tries to retrieve it from the concept object (usefull when the form is used to change an existing Concept) 
    """
    ConceptNatureFormSet = modelformset_factory(ConceptNature,
                                                fields="__all__")
    concept = obj if obj else utils_payload.get_request_payload(
        request, "concept")

    nature_set = utils_payload.get_request_payload(request, "nature_set", [])

    if not nature_set and request.method == "POST":
        formset = ConceptNatureFormSet(request.POST,
                                       prefix="additional_nature_set")
        formset.is_valid()  # populate the cleaned_data member of each form
        for form in formset:
            if form.cleaned_data.get("nature"):
                nature_set.append(form.cleaned_data.get("nature"))
        if nature_set and concept and concept.primary_nature:
            nature_set.insert(0, concept.primary_nature)

    if not nature_set and concept:
        nature_set = list(concept.all_nature_tuple)

    return nature_set
Esempio n. 3
0
    def get_inline_instances(self, request, obj=None):
        """ 
        Override allowing to dynamically generate AdminInline instances. 
        It is usefull to add formsets to a given admin form at runtime. 
        
        The method expects "collecster_dynamic_inline_classes" member to be a dictionary mapping group names to either: 
        * A callable that returns a collection of AdminInline classes (for the dynamic behaviour) 
        * A static collection of AdminInline classes (similar behaviour to the default "inlines" attribute behaviour) 
        All the AdminInlines for all groups are returned, except if the payload limits groups using its "collecster_inlines_group" entry 
        """
        AdminClass = self.__class__
        added = []
        requested_inlines = utils_payload.get_request_payload(
            request, "inlines_groups")
        if hasattr(AdminClass, "collecster_dynamic_inline_classes"):
            filter_func = (lambda x: x in requested_inlines
                           ) if requested_inlines else (lambda x: True)
            for inlines in [
                    inlines for group, inlines in
                    self.collecster_dynamic_inline_classes.items()
                    if filter_func(group)
            ]:
                for AdminInline in (inlines(request, obj)
                                    if callable(inlines) else inlines):
                    added.append(AdminInline(self.model, self.admin_site))

        if requested_inlines:
            return added
        else:
            return super(CollecsterModelAdmin, self).get_inline_instances(
                request, obj) + added
Esempio n. 4
0
def get_concept_id(request, release=None, occurrence=None):
    # Even if the forwarded object is not None, it could not have its related field not populated:
    # Eg. On creation of the empty form for a Release, a default constructed Release instance is forwarded to formsets
    # see: https://github.com/django/django/blob/1.8.3/django/contrib/admin/options.py#L1480

    # See forms_admins.py _collecster_fixup_request()
    release = release if release else utils_payload.get_request_payload(request, "release")
    occurrence = occurrence if occurrence else utils_payload.get_request_payload(request, "occurrence")

    if release and hasattr(release, "concept"):
        return release.concept.pk
    elif occurrence and hasattr(occurrence, "release"):
        return occurrence.release.concept.pk
    else:
        concept_id = utils_payload.get_request_payload(request, "concept_id", 0)
        ## This is not possible anymore if we want to make this file not include the models
        # if not concept_id:
        #    release_id = utils_payload.get_request_payload(request, "release_id", 0)
        #    if release_id:
        #        concept_id = Release.objects.get(pk=release_id).concept.pk
        return concept_id
Esempio n. 5
0
def get_concept_id(request, release=None, occurrence=None):
    # Even if the forwarded object is not None, it could not have its related field not populated:
    # Eg. On creation of the empty form for a Release, a default constructed Release instance is forwarded to formsets
    # see: https://github.com/django/django/blob/1.8.3/django/contrib/admin/options.py#L1480

    # See forms_admins.py _collecster_fixup_request()
    release = release if release else utils_payload.get_request_payload(request, "release")
    occurrence = occurrence if occurrence else utils_payload.get_request_payload(request, "occurrence")

    if release and hasattr(release, "concept"):
        return release.concept.pk
    elif occurrence and hasattr(occurrence, "release"):
        return occurrence.release.concept.pk
    else:
        concept_id = utils_payload.get_request_payload(request, "concept_id", 0)
        ## This is not possible anymore if we want to make this file not include the models
        #if not concept_id:
        #    release_id = utils_payload.get_request_payload(request, "release_id", 0)
        #    if release_id:
        #        concept_id = Release.objects.get(pk=release_id).concept.pk
        return concept_id
Esempio n. 6
0
    def get_inline_instances(self, request, obj=None):
        """ 
        Override allowing to dynamically generate AdminInline instances. 
        It is usefull to add formsets to a given admin form at runtime. 
        
        The method expects "collecster_dynamic_inline_classes" member to be a dictionary mapping group names to either: 
        * A callable that returns a collection of AdminInline classes (for the dynamic behaviour) 
        * A static collection of AdminInline classes (similar behaviour to the default "inlines" attribute behaviour) 
        All the AdminInlines for all groups are returned, except if the payload limits groups using its "collecster_inlines_group" entry 
        """
        AdminClass = self.__class__
        added = []
        requested_inlines = utils_payload.get_request_payload(request, "inlines_groups")
        if hasattr(AdminClass, "collecster_dynamic_inline_classes"):
            filter_func = (lambda x: x in requested_inlines) if requested_inlines else (lambda x: True)
            for inlines in [inlines for group, inlines in self.collecster_dynamic_inline_classes.items() if filter_func(group)]:
                for AdminInline in (inlines(request, obj) if callable(inlines) else inlines):
                    added.append(AdminInline(self.model, self.admin_site))

        if requested_inlines:
            return added
        else:
            return super(CollecsterModelAdmin, self).get_inline_instances(request, obj) + added
Esempio n. 7
0
def get_release_id(request, occurrence=None):
    # Even if the forwarded object is not None, it could not have its related field not populated
    if occurrence is not None and hasattr(occurrence, "release"):
        return occurrence.release.pk
    else:
        return utils_payload.get_request_payload(request, "release_id", 0)
Esempio n. 8
0
def get_release_id(request, occurrence=None):
    # Even if the forwarded object is not None, it could not have its related field not populated
    if occurrence is not None and hasattr(occurrence, "release"):
        return occurrence.release.pk
    else:
        return utils_payload.get_request_payload(request, "release_id", 0)