Make the relations all work.

This commit is contained in:
Schuyler Erle 2011-09-01 09:19:46 +02:00
parent dcccbb3f66
commit 0a4bae4b43
4 changed files with 72 additions and 78 deletions

View File

@ -108,22 +108,24 @@ class Feature(models.Model):
def similar_features(self, max_distance=15000, scale_factor=2000, limit=20): def similar_features(self, max_distance=15000, scale_factor=2000, limit=20):
sql = """ sql = """
SELECT *, %f * similarity / (distance + 1.0) AS score FROM ( SELECT *, %s * similarity / (distance + 1.0) AS score FROM (
SELECT f.*, r.*, SELECT f.*, r.feature1_id, r.feature2_id, r.relationship_type,
similarity(preferred_name, '%s') AS similarity, similarity(preferred_name, %s) AS similarity,
st_distance_sphere(geometry, 'SRID=4326;%s') AS distance st_distance_sphere(geometry, %s) AS distance
FROM places_feature f LEFT JOIN places_relationship r FROM places_feature f LEFT JOIN places_relationship r
ON (f.id=r.feature_from OR f.id=r.feature_to) ON (f.id=r.feature1_id OR f.id=r.feature2_id)
WHERE geometry && st_buffer('%s', %f) WHERE geometry && st_buffer(%s, %s)
AND preferred_name %%%% '%s' AND preferred_name %% %s
AND f.id <> %d AND f.id <> %s
LIMIT %d AND (r.feature1_id IS NULL OR r.feature1_id = %s OR r.feature2_id = %s)
LIMIT %s
) AS whatever ) AS whatever
ORDER BY similarity / (distance + 1.0) DESC""" ORDER BY similarity / (distance + 1.0) DESC"""
qset = self.objects.raw(sql, (scale_factor, name, self.geometry.wkt, qset = type(self).objects.raw(sql, [scale_factor, self.preferred_name,
self.geometry.wkt, max_distance*0.00001, name, "SRID=4326;" + self.geometry.centroid.wkt,
self.id, limit)) self.geometry.centroid.wkt, max_distance*0.00001,
return qset self.preferred_name, self.id, self.id, self.id, limit])
return qset
LANGUAGE_CHOICES = ( LANGUAGE_CHOICES = (
('en', 'English'), ('en', 'English'),

View File

@ -2,7 +2,8 @@
from django.shortcuts import render_to_response, get_object_or_404 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 ox.django.shortcuts import render_to_json_response, get_object_or_404_json
from django.template import RequestContext 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 from django.core.paginator import Paginator, InvalidPage, EmptyPage
def search(request): def search(request):
@ -56,31 +57,6 @@ def search_json(request):
} }
return render_to_json_response(d) 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): def search_related_json(request, id):
# id = request.GET.get("id", "0") # id = request.GET.get("id", "0")
id = str(id) id = str(id)
@ -88,15 +64,15 @@ def search_related_json(request, id):
similar_features = feature.similar_features() similar_features = feature.similar_features()
d = [] d = []
for f in similar_features: for s in similar_features:
time_frame = relates_to = related_by = ""
if s.time_frame is not None: if s.time_frame is not None:
time_frame = s.time_frame.description time_frame = s.time_frame.description
else: if s.relationship_type is not None:
time_frame = '' if s.feature2_id == s.id:
if s.relationship_type is not None and s.feature_to == s.id: relates_to = s.relationship_type
relationship = s.relationship_type elif s.feature1_id == s.id:
else: related_by = s.relationship_type
relationship = ''
d.append({ d.append({
'id': s.id, 'id': s.id,
'feature_type': s.feature_type.name, #FeatureType.objects.get(pk=s.feature_type_id).name, '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, 'distance': s.distance,
'time_frame': time_frame, 'time_frame': time_frame,
'is_primary': s.is_primary, 'is_primary': s.is_primary,
'relationship': relationship 'relates_to': relates_to,
'related_by': related_by
}) })
return render_to_json_response(d) return render_to_json_response(d)
@ -139,14 +116,23 @@ def add_relation(request):
if not request.user.is_staff: if not request.user.is_staff:
return render_to_json_response({'error': 'insufficient permissions error. try logging in again? are you staff / admin?'}) 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) feature1 = get_object_or_404_json(Feature, pk=feature1)
feature2 = get_object_or_404_json(Feature, feature2) feature2 = get_object_or_404_json(Feature, pk=feature2)
Relationship(feature_from=feature1, feature_to=feature2, relationship_type=relation).save() rel_obj = None
if relation == "subsumes": try:
feature2.is_primary = False rel_obj = Relationship.objects.get(feature1=feature1, feature2=feature2, relationship_type=relation)
feature2.save() except ObjectDoesNotExist:
rel_obj = Relationship(feature1=feature1, feature2=feature2, relationship_type=relation)
return render_to_json_response({'success': 'relation made successfully.'}) 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})

View File

@ -24,7 +24,7 @@ function getRow(d) {
var distance = Math.round(parseFloat(d.distance)); var distance = Math.round(parseFloat(d.distance));
$('<td />').text(d.feature_type).appendTo($tr); $('<td />').text(d.feature_type).appendTo($tr);
$('<td />').text(similarity).appendTo($tr); $('<td />').text(similarity).appendTo($tr);
$('<td />').text(d.distance).appendTo($tr); $('<td />').text(distance).appendTo($tr);
$('<td />').text(d.time_frame).appendTo($tr); $('<td />').text(d.time_frame).appendTo($tr);
var $primary_td = $('<td />').appendTo($tr); var $primary_td = $('<td />').appendTo($tr);
var $checkbox = $('<input />').attr("type", "checkbox").attr("disabled", "disabled").appendTo($primary_td); var $checkbox = $('<input />').attr("type", "checkbox").attr("disabled", "disabled").appendTo($primary_td);
@ -34,21 +34,28 @@ function getRow(d) {
$.noop(); $.noop();
} }
var $relations_td = $('<td />').appendTo($tr); var $relations_td = $('<td />').prependTo($tr);
var $relationselect = $('<select />').appendTo($relations_td); if (d.related_by != '') {
var $opt1 = $('<option />').val('').text('---').appendTo($relationselect); var verb = d.related_by.replace(/e?s$/i, "");
var related_by = "is " + verb + "ed by";
var $relationselect = $('<a />').attr("href", "/admin/places/feature/" + d.id)
.text(related_by).appendTo($relations_td);
} else {
var $relationselect = $('<select />').appendTo($relations_td);
var $opt1 = $('<option />').val('').text('---').appendTo($relationselect);
for (var i=0; i<RELATIONSHIP_OPTIONS.length; i++) { for (var i=0; i<RELATIONSHIP_OPTIONS.length; i++) {
var r = RELATIONSHIP_OPTIONS[i]; var r = RELATIONSHIP_OPTIONS[i];
$('<option />').val(r).text(r).appendTo($relationselect); $('<option />').val(r).text(r).appendTo($relationselect);
} }
if (d.relationship != '') { if (d.relates_to != '') {
$relationselect.children().each(function() { $relationselect.children().each(function() {
if ($(this).val() == d.relationship) { if ($(this).val() == d.relates_to) {
$(this).attr("selected", "selected"); $(this).attr("selected", "selected");
} }
}); });
}
} }
$relationselect.change(function() { $relationselect.change(function() {

View File

@ -20,6 +20,9 @@
<table id="similarTable" class="customTable"> <table id="similarTable" class="customTable">
<thead> <thead>
<tr> <tr>
<th>
Relation
</th>
<th> <th>
Name Name
</th> </th>
@ -30,7 +33,7 @@
Similarity Similarity
</th> </th>
<th> <th>
Distance Distance (m)
</th> </th>
<th> <th>
Time Frame Time Frame
@ -39,10 +42,6 @@
Is Primary? Is Primary?
</th> </th>
<th>
Relation
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>