def delete_element(request): el_type, el_id = request.POST['obj'].split('-') element = find_location(el_type, el_id) try: element.delete() except ProtectedError: # Return 409 Conflict logger.warn('Error deleting location: %s', element) return HttpResponse("Unable to delete.", status=409) return HttpResponse('success')
def find_children(request, type=None, id=None): if type: object = find_location(type, id) children = object.children.annotate(Count('museumobject')) else: children = GlobalRegion.objects.annotate(Count('museumobject')) nodes = serialize_locs_jstree(children) return HttpResponse( json.dumps(nodes, sort_keys=True, indent=4), content_type='application/json')
def create_element(request): name = request.POST['name'] model_type = request.POST['type'] p_type, p_id = request.POST['parent'].split('-') parent = find_location(p_type, p_id) location_model = ContentType.objects.get(app_label='location', model=model_type) new_location = location_model.model_class()() new_location.name = name new_location.parent = parent try: new_location.save() except IntegrityError: # Return 409 Conflict logger.warn('Error creating location: %s', new_location) return HttpResponse('A location by that name already exists', status=409) return HttpResponse(json.dumps( {'id': new_location.id}), mimetype="application/json")
def rename_element(request): el_type, el_id = request.POST['obj'].split('-') new_name = request.POST['new-name'] element = find_location(el_type, el_id) element.name = new_name linked_count = element.museumobject_set.count() try: element.save() except IntegrityError: # Would like to return 409 Conflict # but browser has issue parsing json, so using a 200 # with an error flag instead logger.warn('Error renaming location: %s', element) return HttpResponse(json.dumps({ 'message': 'A location by that name already exists', 'count': linked_count, 'error': True}), mimetype="application/json") return HttpResponse(json.dumps( {'new_name': element.name, 'count': linked_count, 'error': False}), mimetype="application/json")
def process_element_move(type, id, np_type, np_id): new_parent = find_location(np_type, np_id) element = find_location(type, id) element.moveto_parent(new_parent)