Ejemplo n.º 1
0
def patch_classification_entry(request, cls_id):
    classification_entry = get_object_or_404(ClassificationEntry,
                                             id=int(cls_id))

    result = {}

    if 'parent' in request.data:
        if request.data['parent'] is None:
            classification_entry.parent = None
            classification_entry.parent_list = []

            result['parent'] = None
            result['parent_list'] = []
            result['parent_details'] = []
        else:
            pcls_id = int(request.data['parent'])

            parent = get_object_or_404(ClassificationEntry, id=pcls_id)

            classification_entry.parent = parent

            # @todo rank level
            if parent.rank_id >= classification_entry.rank_id:
                raise SuspiciousOperation(
                    _("The rank of the parent must be lowest than the classification entry itself"
                      ))

            # make parent list
            ClassificationEntryManager.update_parents(classification_entry,
                                                      parent)

            # query for parents
            parents = []
            parents_cls = ClassificationEntry.objects.filter(
                id__in=classification_entry.parent_list)

            for parent in parents_cls:
                parents.insert(
                    0, {
                        'id': parent.id,
                        'name': parent.name,
                        'rank': parent.rank_id,
                        'parent': parent.parent_id
                    })

            result['parent'] = parent.id
            result['parent_list'] = parents
            result['parent_details'] = parents

        classification_entry.update_field(['parent', 'parent_list'])

    try:
        with transaction.atomic():
            # update layout of descriptors and descriptors
            if 'layout' in request.data:
                layout_id = request.data["layout"]

                # changing of layout erase all previous descriptors values
                if layout_id is None and classification_entry.layout is not None:
                    # clean previous descriptors and owns
                    descriptors_builder = DescriptorsBuilder(
                        classification_entry)

                    descriptors_builder.clear(classification_entry.layout)

                    classification_entry.layout = None
                    classification_entry.descriptors = {}

                    descriptors_builder.update_associations()

                    result['layout'] = None
                    result['descriptors'] = {}

                elif layout_id is not None:
                    # existing descriptors and new layout is different : first clean previous descriptors
                    if (classification_entry.layout is not None
                            and classification_entry.layout.pk != layout_id):

                        # clean previous descriptors and owns
                        descriptors_builder = DescriptorsBuilder(
                            classification_entry)

                        descriptors_builder.clear(classification_entry.layout)

                        classification_entry.layout = None
                        classification_entry.descriptors = {}

                        descriptors_builder.update_associations()

                    # and set the new one
                    content_type = get_object_or_404(
                        ContentType,
                        app_label="classification",
                        model="classificationentry")
                    layout = get_object_or_404(Layout,
                                               id=layout_id,
                                               target=content_type)

                    classification_entry.layout = layout
                    classification_entry.descriptors = {}

                    result['layout'] = layout.id
                    result['descriptors'] = {}

                    classification_entry.update_field(
                        ['layout', 'descriptors'])

            # update descriptors
            if 'descriptors' in request.data:
                descriptors = request.data["descriptors"]

                descriptors_builder = DescriptorsBuilder(classification_entry)

                descriptors_builder.check_and_update(
                    classification_entry.layout, descriptors)
                classification_entry.descriptors = descriptors_builder.descriptors

                descriptors_builder.update_associations()

                result['descriptors'] = classification_entry.descriptors

                classification_entry.update_descriptors(
                    descriptors_builder.changed_descriptors())
                classification_entry.update_field('descriptors')

            classification_entry.save()

    except IntegrityError as e:
        Descriptor.integrity_except(ClassificationEntry, e)

    return HttpResponseRest(request, result)
Ejemplo n.º 2
0
def modify_panel(request, panel_id):
    acc_panel = get_object_or_404(AccessionPanel, id=int(panel_id))
    # entity_status = request.data.get("entity_status")
    descriptors = request.data.get("descriptors")

    result = {'id': acc_panel.id}

    try:
        with transaction.atomic():
            # if entity_status is not None and panel.entity_status != entity_status:
            #     panel.set_status(entity_status)
            #     result['entity_status'] = entity_status

            if 'name' in request.data:
                name = request.data['name']

                if AccessionPanel.objects.filter(name=name).exists():
                    raise SuspiciousOperation(
                        _("The name of the panel is already used"))

                acc_panel.name = name
                result['name'] = name

            if 'layout' in request.data:
                layout_id = request.data["layout"]

                # changing of layout erase all previous descriptors values
                if layout_id is None and acc_panel.layout is not None:
                    # clean previous descriptors and owns
                    descriptors_builder = DescriptorsBuilder(acc_panel)

                    descriptors_builder.clear(acc_panel.layout)

                    acc_panel.layout = None
                    acc_panel.descriptors = {}

                    descriptors_builder.update_associations()

                    result['layout'] = None
                    result['descriptors'] = {}

                elif layout_id is not None:
                    # existing descriptors and new layout is different : first clean previous descriptors
                    if acc_panel.layout is not None and acc_panel.layout.pk != layout_id:
                        # clean previous descriptors and owns
                        descriptors_builder = DescriptorsBuilder(acc_panel)

                        descriptors_builder.clear(acc_panel.layout)

                        acc_panel.layout = None
                        acc_panel.descriptors = {}

                        descriptors_builder.update_associations()

                    # and set the new one
                    content_type = get_object_or_404(ContentType,
                                                     app_label="accession",
                                                     model="accessionpanel")
                    layout = get_object_or_404(Layout,
                                               id=layout_id,
                                               target=content_type)

                    acc_panel.layout = layout
                    acc_panel.descriptors = {}

                    result['layout'] = layout.id
                    result['descriptors'] = {}

                    acc_panel.update_field(['layout', 'descriptors'])

            if descriptors is not None:
                # update descriptors
                descriptors_builder = DescriptorsBuilder(acc_panel)

                descriptors_builder.check_and_update(acc_panel.layout,
                                                     descriptors)

                acc_panel.descriptors = descriptors_builder.descriptors
                result['descriptors'] = acc_panel.descriptors

                descriptors_builder.update_associations()

                acc_panel.update_descriptors(
                    descriptors_builder.changed_descriptors())
                acc_panel.update_field('descriptors')

            acc_panel.save()
    except IntegrityError as e:
        Descriptor.integrity_except(Accession, e)

    return HttpResponseRest(request, result)