Ejemplo n.º 1
0
def update_idiograph(request):
    response = {}
    try:
        allograph_id = int(request.POST.get('allograph', ''))
        idiograph_id = int(request.POST.get('idiograph_id', ''))
        data = json.loads(request.POST.get('data', ''))
        allograph = Allograph.objects.get(id=allograph_id)
        idiograph = Idiograph.objects.get(id=idiograph_id)
        idiograph.allograph = allograph
        idiograph.save()
        for idiograph_component in data:
            if idiograph_component['idiograph_component'] != False:
                ic = IdiographComponent.objects.get(
                    id=idiograph_component['idiograph_component'])
                ic.features.clear()
            else:
                ic = IdiographComponent()
            component = Component.objects.get(id=idiograph_component['id'])
            ic.idiograph = idiograph
            ic.component = component
            ic.save()
            for features in idiograph_component['features']:
                feature = Feature.objects.get(id=features['id'])
                ic.features.add(feature)
        response['errors'] = False
    except Exception as e:
        response['errors'] = ['Internal error: %s' % e.message]
    return HttpResponse(json.dumps(response), content_type='application/json')