Fixup Sanjay's todos.

This commit is contained in:
Schuyler Erle 2011-08-31 21:51:56 -07:00
parent 0d96fc6d6f
commit dcccbb3f66
2 changed files with 25 additions and 29 deletions

View File

@ -107,34 +107,23 @@ class Feature(models.Model):
time_end.short_description = "End Date" time_end.short_description = "End Date"
def similar_features(self, max_distance=15000, scale_factor=2000, limit=20): def similar_features(self, max_distance=15000, scale_factor=2000, limit=20):
cursor = connection.cursor() sql = """
name = unicode(self).replace("'", "''") # escape '
cursor.execute("""
SELECT *, %f * similarity / (distance + 1.0) AS score FROM ( SELECT *, %f * similarity / (distance + 1.0) AS score FROM (
SELECT id, url, preferred_name, feature_type_id, SELECT f.*, r.*,
admin1, admin2, is_primary,
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, 'SRID=4326;%s') AS distance
FROM places_feature 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) WHERE geometry && st_buffer('%s', %f)
AND preferred_name %%%% '%s' AND preferred_name %%%% '%s'
AND id <> %d AND f.id <> %d
LIMIT %d LIMIT %d
) AS whatever ) AS whatever
ORDER BY similarity / (distance + 1.0) DESC""" ORDER BY similarity / (distance + 1.0) DESC"""
% (scale_factor, name, self.geometry.wkt, self.geometry.wkt, qset = self.objects.raw(sql, (scale_factor, name, self.geometry.wkt,
max_distance*0.00001, name, self.id, limit) self.geometry.wkt, max_distance*0.00001, name,
) self.id, limit))
result_list = [] return qset
fields = ('id', 'url', 'preferred_name', 'feature_type_id', 'admin1', 'admin2', 'is_primary')
for row in cursor.fetchall():
vals = dict(zip(fields, row[:len(fields)]))
p = type(self)(**vals)
p.similarity = row[-3]
p.distance = row[-2]
p.score = row[-1]
result_list.append(p)
return result_list
LANGUAGE_CHOICES = ( LANGUAGE_CHOICES = (
('en', 'English'), ('en', 'English'),

View File

@ -88,23 +88,24 @@ def search_related_json(request, id):
similar_features = feature.similar_features() similar_features = feature.similar_features()
d = [] d = []
for s in similar_features: for f in similar_features:
f = Feature.objects.get(pk=s.id) # This seems inefficient - better to get something like time_frame_id in models method? if s.time_frame is not None:
if f.time_frame is not None: time_frame = s.time_frame.description
time_frame = f.time_frame.description
else: else:
time_frame = '' time_frame = ''
if s.relationship_type is not None and s.feature_to == s.id:
relationship = s.relationship_type
else:
relationship = ''
d.append({ d.append({
'id': s.id, 'id': s.id,
'feature_type': FeatureType.objects.get(pk=s.feature_type_id).name, 'feature_type': s.feature_type.name, #FeatureType.objects.get(pk=s.feature_type_id).name,
'preferred_name': s.preferred_name, 'preferred_name': s.preferred_name,
'similarity': s.similarity, 'similarity': s.similarity,
'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': '' #TODO: query db for relation between id and s.id - if exists, return relation_type as string, else return empty string. 'relationship': relationship
}) })
return render_to_json_response(d) return render_to_json_response(d)
@ -134,10 +135,16 @@ def add_relation(request):
relation = request.GET.get("relation", None) relation = request.GET.get("relation", None)
if feature1 == None or feature2 == None or relation == None: #TODO: split up errors :/ -- not imp. if feature1 == None or feature2 == None or relation == None: #TODO: split up errors :/ -- not imp.
return render_to_json_response({'error': 'bad request'}) return render_to_json_response({'error': 'bad 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?'})
#TODO: handle saving m2m between feature1 and feature2 with relation. If relation='', either remove existing relation between feature1 and feature2, or ignore if no such existing relation exists. BIG QUESTION: Here it can also deal with is_primary logic - if it sets stuff to is_primary or not, though, this needs to be conveyed to the front-end / a logic figured out to toggle the check-boxes display. 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.'}) return render_to_json_response({'success': 'relation made successfully.'})