From 0a4bae4b43366dbcf1fdd03a6c56f0614a949eb8 Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Thu, 1 Sep 2011 09:19:46 +0200 Subject: [PATCH] Make the relations all work. --- gazetteer/places/models.py | 28 +++---- gazetteer/places/views.py | 76 ++++++++----------- gazetteer/static/js/feature_admin.js | 37 +++++---- .../admin/places/feature/change_form.html | 9 +-- 4 files changed, 72 insertions(+), 78 deletions(-) diff --git a/gazetteer/places/models.py b/gazetteer/places/models.py index 71da611..192bc69 100644 --- a/gazetteer/places/models.py +++ b/gazetteer/places/models.py @@ -108,22 +108,24 @@ class Feature(models.Model): def similar_features(self, max_distance=15000, scale_factor=2000, limit=20): sql = """ - SELECT *, %f * similarity / (distance + 1.0) AS score FROM ( - SELECT f.*, r.*, - similarity(preferred_name, '%s') AS similarity, - st_distance_sphere(geometry, 'SRID=4326;%s') AS distance + SELECT *, %s * similarity / (distance + 1.0) AS score FROM ( + SELECT f.*, r.feature1_id, r.feature2_id, r.relationship_type, + similarity(preferred_name, %s) AS similarity, + st_distance_sphere(geometry, %s) AS distance FROM places_feature f LEFT JOIN places_relationship r - ON (f.id=r.feature_from OR f.id=r.feature_to) - WHERE geometry && st_buffer('%s', %f) - AND preferred_name %%%% '%s' - AND f.id <> %d - LIMIT %d + ON (f.id=r.feature1_id OR f.id=r.feature2_id) + WHERE geometry && st_buffer(%s, %s) + AND preferred_name %% %s + AND f.id <> %s + AND (r.feature1_id IS NULL OR r.feature1_id = %s OR r.feature2_id = %s) + LIMIT %s ) AS whatever ORDER BY similarity / (distance + 1.0) DESC""" - qset = self.objects.raw(sql, (scale_factor, name, self.geometry.wkt, - self.geometry.wkt, max_distance*0.00001, name, - self.id, limit)) - return qset + qset = type(self).objects.raw(sql, [scale_factor, self.preferred_name, + "SRID=4326;" + self.geometry.centroid.wkt, + self.geometry.centroid.wkt, max_distance*0.00001, + self.preferred_name, self.id, self.id, self.id, limit]) + return qset LANGUAGE_CHOICES = ( ('en', 'English'), diff --git a/gazetteer/places/views.py b/gazetteer/places/views.py index 0329441..84932f9 100644 --- a/gazetteer/places/views.py +++ b/gazetteer/places/views.py @@ -2,7 +2,8 @@ from django.shortcuts import render_to_response, get_object_or_404 from ox.django.shortcuts import render_to_json_response, get_object_or_404_json from django.template import RequestContext -from models import Feature, FeatureType, AuthorityRecord +from models import Feature, FeatureType, AuthorityRecord, Relationship +from django.core.exceptions import ObjectDoesNotExist from django.core.paginator import Paginator, InvalidPage, EmptyPage def search(request): @@ -56,31 +57,6 @@ def search_json(request): } return render_to_json_response(d) -''' -This function will be removed. No need for this page. -''' -''' -def search_related(request): - id = request.GET.get("id", "0") - feature = get_object_or_404(Feature, pk=id) - similar_features = feature.similar_features() - d = [] - for s in similar_features: - d.append({ - 'id': s.id, - 'feature_type': FeatureType.objects.get(pk=s.feature_type_id), - 'preferred_name': s.preferred_name, - 'similarity': s.similarity, - 'distance': s.distance, - 'relationship': '' - }) - - return render_to_response("search_related.html", { - 'feature': feature, - 'similar_features': d - }) -''' - def search_related_json(request, id): # id = request.GET.get("id", "0") id = str(id) @@ -88,15 +64,15 @@ def search_related_json(request, id): similar_features = feature.similar_features() d = [] - for f in similar_features: + for s in similar_features: + time_frame = relates_to = related_by = "" if s.time_frame is not None: time_frame = s.time_frame.description - else: - time_frame = '' - if s.relationship_type is not None and s.feature_to == s.id: - relationship = s.relationship_type - else: - relationship = '' + if s.relationship_type is not None: + if s.feature2_id == s.id: + relates_to = s.relationship_type + elif s.feature1_id == s.id: + related_by = s.relationship_type d.append({ 'id': s.id, 'feature_type': s.feature_type.name, #FeatureType.objects.get(pk=s.feature_type_id).name, @@ -105,7 +81,8 @@ def search_related_json(request, id): 'distance': s.distance, 'time_frame': time_frame, 'is_primary': s.is_primary, - 'relationship': relationship + 'relates_to': relates_to, + 'related_by': related_by }) return render_to_json_response(d) @@ -139,14 +116,23 @@ def add_relation(request): if not request.user.is_staff: return render_to_json_response({'error': 'insufficient permissions error. try logging in again? are you staff / admin?'}) - feature1 = get_object_or_404_json(Feature, feature1) - feature2 = get_object_or_404_json(Feature, feature2) - Relationship(feature_from=feature1, feature_to=feature2, relationship_type=relation).save() - if relation == "subsumes": - feature2.is_primary = False - feature2.save() - - return render_to_json_response({'success': 'relation made successfully.'}) - - - + feature1 = get_object_or_404_json(Feature, pk=feature1) + feature2 = get_object_or_404_json(Feature, pk=feature2) + rel_obj = None + try: + rel_obj = Relationship.objects.get(feature1=feature1, feature2=feature2, relationship_type=relation) + except ObjectDoesNotExist: + rel_obj = Relationship(feature1=feature1, feature2=feature2, relationship_type=relation) + verb = "created" + if relation == "": + if rel_obj.id: + verb = "deleted" + rel_obj.delete() + else: + return render_to_json_response({'error': 'relation is already deleted'}) + else: + rel_obj.save() + if relation == "subsumes": + feature2.is_primary = False + feature2.save() + return render_to_json_response({'success': 'relation %s successfully.' % verb}) diff --git a/gazetteer/static/js/feature_admin.js b/gazetteer/static/js/feature_admin.js index a741bdf..e57bb40 100644 --- a/gazetteer/static/js/feature_admin.js +++ b/gazetteer/static/js/feature_admin.js @@ -24,7 +24,7 @@ function getRow(d) { var distance = Math.round(parseFloat(d.distance)); $('').text(d.feature_type).appendTo($tr); $('').text(similarity).appendTo($tr); - $('').text(d.distance).appendTo($tr); + $('').text(distance).appendTo($tr); $('').text(d.time_frame).appendTo($tr); var $primary_td = $('').appendTo($tr); var $checkbox = $('').attr("type", "checkbox").attr("disabled", "disabled").appendTo($primary_td); @@ -34,23 +34,30 @@ function getRow(d) { $.noop(); } - var $relations_td = $('').appendTo($tr); - var $relationselect = $('').appendTo($relations_td); + var $opt1 = $('