From a61d8dd6febadc4ca31d0f4e6ff307331890a0ed Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 1 Sep 2011 08:28:02 +0530 Subject: [PATCH] add JS to edit relations; add TODOs in views.py --- gazetteer/places/views.py | 13 ++++++++++--- gazetteer/static/js/feature_admin.js | 15 ++++++++++++++- gazetteer/urls.py | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/gazetteer/places/views.py b/gazetteer/places/views.py index 6422e89..dca9f83 100644 --- a/gazetteer/places/views.py +++ b/gazetteer/places/views.py @@ -89,7 +89,7 @@ def search_related_json(request, id): d = [] for s in similar_features: - f = Feature.objects.get(pk=s.id) # This seems inefficient - better to get something like time_frame_id in models method? TODO + f = Feature.objects.get(pk=s.id) # This seems inefficient - better to get something like time_frame_id in models method? if f.time_frame is not None: time_frame = f.time_frame.description else: @@ -104,7 +104,7 @@ def search_related_json(request, id): 'distance': s.distance, 'time_frame': time_frame, 'is_primary': s.is_primary, - 'relationship': '' + 'relationship': '' #TODO: query db for relation between id and s.id - if exists, return relation_type as string, else return empty string. }) return render_to_json_response(d) @@ -128,7 +128,14 @@ def time_frame_json(request): } return render_to_json_response(d) - +def add_relation(request): + feature1 = request.GET.get("feature1", None) + feature2 = request.GET.get("feature2", None) + relation = request.GET.get("relation", "") + if feature1 == None or feature2 == None or not request.user.is_staff(): #TODO: split up errors :/ -- not imp. + return render_to_json_response({'error': 'bad request'}) + #TODO: handle saving m2m between feature1 and feature2 with relation + return render_to_json_response({'success': 'relation made successfully.'}) diff --git a/gazetteer/static/js/feature_admin.js b/gazetteer/static/js/feature_admin.js index 7000816..2e1aceb 100644 --- a/gazetteer/static/js/feature_admin.js +++ b/gazetteer/static/js/feature_admin.js @@ -17,7 +17,7 @@ $(function() { var RELATIONSHIP_OPTIONS = ['conflates', 'contains', 'consumes', 'supersedes'] function getRow(d) { - var $tr = $(''); + var $tr = $('').data("id", d.id); var $one = $('').appendTo($tr); var $a = $('').attr("href", "/admin/places/feature/" + d.id).text(d.preferred_name).appendTo($one); var similarity = Math.round(parseFloat(d.similarity) * 1000) / 10; @@ -51,6 +51,19 @@ function getRow(d) { }); } + $relationselect.change(function() { + var feature1 = FEATURE_ID; + var feature2 = $(this).parents("tr").data("id"); + var relation = $(this).val(); + $.getJSON("/add_relation", { + 'feature1': feature1, + 'feature2': feature2, + 'relation': relation + }, function(response) { + + }); + + }); return $tr; } diff --git a/gazetteer/urls.py b/gazetteer/urls.py index 631e731..4ad1fe3 100644 --- a/gazetteer/urls.py +++ b/gazetteer/urls.py @@ -16,6 +16,7 @@ urlpatterns = patterns('', ('^feature/(?P\d+)/similar.json$', 'places.views.search_related_json'), ('^auth_record.json$', 'places.views.auth_record_json'), ('^time_frame.json$', 'places.views.time_frame_json'), + ('^add_relation$', 'places.views.add_relation'), # Uncomment the admin/doc line below to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^ajax_select/', include('ajax_select.urls')),