コード例 #1
0
def patch_accession(request, acc_id):
    accession = get_object_or_404(Accession, id=int(acc_id))

    entity_status = request.data.get("entity_status")
    descriptors = request.data.get("descriptors")

    result = {'id': accession.id}

    try:
        with transaction.atomic():
            if 'primary_classification_entry' in request.data:
                primary_classification_entry_id = int(
                    request.data['primary_classification_entry'])
                primary_classification_entry = get_object_or_404(
                    ClassificationEntry, id=primary_classification_entry_id)

                # update FK
                accession.primary_classification_entry = primary_classification_entry
                result[
                    'primary_classification_entry'] = primary_classification_entry.id

                # and replace from classification entry M2M previous primary
                accession_classification_entry = get_object_or_404(
                    AccessionClassificationEntry,
                    accession=accession,
                    primary=True)

                accession_classification_entry.classification_entry = primary_classification_entry
                accession_classification_entry.save()

                accession.update_field('primary_classification_entry')

            if entity_status is not None and accession.entity_status != entity_status:
                accession.set_status(entity_status)
                result['entity_status'] = entity_status
                accession.update_field('entity_status')

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

                descriptors_builder.check_and_update(accession.layout,
                                                     descriptors)

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

                descriptors_builder.update_associations()

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

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

    return HttpResponseRest(request, result)
コード例 #2
0
def patch_organisation(request, org_id):
    organisation = get_object_or_404(Organisation, id=int(org_id))

    organisation_name = request.data.get("name")
    organisation_type = request.data.get("type")
    entity_status = request.data.get("entity_status")
    descriptors = request.data.get("descriptors")

    result = {'id': organisation.id}

    try:
        with transaction.atomic():
            if organisation_name is not None:
                organisation.name = organisation_name
                result['name'] = organisation_name

                organisation.update_field('name')

            if organisation_type is not None:
                organisation.type = organisation_type
                result['type'] = organisation_type

                organisation.update_field('type')

            if entity_status is not None and organisation.entity_status != entity_status:
                organisation.set_status(entity_status)
                result['entity_status'] = entity_status

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

                descriptors_builder.check_and_update(organisation.layout,
                                                     descriptors)

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

                descriptors_builder.update_associations()

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

            organisation.save()
    except IntegrityError as e:
        Descriptor.integrity_except(Organisation, e)

    return HttpResponseRest(request, result)
コード例 #3
0
def patch_batch(request, bat_id):
    batch = get_object_or_404(Batch, id=int(bat_id))

    # check permission on accession object
    perms = get_permissions_for(request.user,
                                batch.accession.content_type.app_label,
                                batch.accession.content_type.model,
                                batch.accession.pk)
    if 'accession.change_accession' not in perms:
        raise PermissionDenied(
            _('Invalid permission to modify this accession'))

    entity_status = request.data.get("entity_status")
    descriptors = request.data.get("descriptors")

    result = {'id': batch.id}

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

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

                descriptors_builder.check_and_update(batch.layout, descriptors)

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

                descriptors_builder.update_associations()

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

                batch.save()
    except IntegrityError as e:
        logger.error(repr(e))
        raise SuspiciousOperation(_("Unable to update the batch"))

    return HttpResponseRest(request, result)
コード例 #4
0
ファイル: establishment.py プロジェクト: Medhi83/collgate
def patch_establishment(request, est_id):
    establishment = get_object_or_404(Establishment, id=int(est_id))

    organisation_name = request.data.get("name")
    entity_status = request.data.get("entity_status")
    descriptors = request.data.get("descriptors")

    result = {
        'id': establishment.id
    }

    try:
        with transaction.atomic():
            if organisation_name is not None:
                establishment.name = organisation_name
                result['name'] = organisation_name

                establishment.update_field('name')

            if entity_status is not None and establishment.entity_status != entity_status:
                establishment.set_status(entity_status)
                result['entity_status'] = entity_status

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

                descriptors_builder.check_and_update(establishment.layout, descriptors)

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

                descriptors_builder.update_associations()

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

            establishment.save()
    except IntegrityError as e:
        Descriptor.integrity_except(Organisation, e)

    return HttpResponseRest(request, result)
コード例 #5
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)
コード例 #6
0
ファイル: accessionpanel.py プロジェクト: Medhi83/collgate
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)