From 4cd6154fb87df77908b56dbc9b0d2a8eeb238763 Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 9 Aug 2012 01:36:44 +0530 Subject: [PATCH] fuzzystops edit page --- chaloBEST/mumbai/models.py | 7 ++ chaloBEST/mumbai/views.py | 102 ++++++++++++++++++++------- chaloBEST/templates/fuzzystops.html | 104 ++++++++++++++++++++++++++++ chaloBEST/urls.py | 1 + 4 files changed, 189 insertions(+), 25 deletions(-) diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index 13d1e75..3fba7dd 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -315,6 +315,13 @@ class UniqueRoute(models.Model): def get_stop_choices(self): return Stop.objects.filter(routedetail__route=self.route).order_by('routedetail') + +class FuzzyStopMatch(models.Model): + unr = models.OneToOneField(UniqueRoute) + checked = models.BooleanField(default=True) + + + class RouteSchedule(models.Model): unique_route = models.ForeignKey(UniqueRoute) schedule_type = models.CharField(max_length=16) diff --git a/chaloBEST/mumbai/views.py b/chaloBEST/mumbai/views.py index 9475c73..fa71cfe 100644 --- a/chaloBEST/mumbai/views.py +++ b/chaloBEST/mumbai/views.py @@ -3,7 +3,9 @@ from models import * from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from django.contrib.auth.decorators import login_required +from django.views.decorators.csrf import csrf_exempt from fuzzywuzzy import process as fuzzprocess +from django.http import HttpResponse def index(request): return render_to_response("index.html", {}) @@ -105,33 +107,83 @@ def stats(request): return render_to_response("stats.html", context) - @login_required def fuzzystops(request): -# import pdb - froms_arr = [] - tos_arr = [] + unrs = [] for unr in UniqueRoute.objects.all(): - s1 = unr.from_stop.name.lower() - s2 = unr.from_stop_txt.lower() - from_ratio = fuzzprocess.ratio(s1,s2) - if from_ratio < 70: - froms_arr.append( - (unr, from_ratio,) - ) - s3 = unr.to_stop.name.lower() - s4 = unr.to_stop_txt.lower() - to_ratio = fuzzprocess.ratio(s3,s4) - if to_ratio < 70: - tos_arr.append( - (unr, to_ratio,) - ) - - froms_arr.sort(key=lambda item: item[1]) - tos_arr.sort(key=lambda item: item[1]) - context = RequestContext(request, { - 'fuzzy_froms': [item[0] for item in froms_arr], - 'fuzzy_tos': [item[0] for item in tos_arr] - }) + if FuzzyStopMatch.objects.filter(unr=unr).filter(checked=True).count() > 0: + continue + unrs.append(unr) +# import pdb # pdb.set_trace() + context = RequestContext(request, { + 'unrs': unrs[0:100] + }) return render_to_response("fuzzystops.html", context) + + +@login_required +@csrf_exempt +def fuzzystops_edit(request): + unr_id = request.POST.get("id", 0) + unr = UniqueRoute.objects.get(pk=unr_id) + from_stop_id = int(request.POST.get("from_stop")) + to_stop_id = int(request.POST.get("to_stop")) + unr.from_stop_id = from_stop_id + unr.to_stop_id = to_stop_id + unr.save() + change_all = request.POST.get("change_all", False) + if change_all: + from_stop_txt = unr.from_stop_txt + to_stop_txt = unr.to_stop_txt + for u in UniqueRoute.objects.filter(from_stop_txt=from_stop_txt): + u.from_stop = from_stop_id + u.save() + for u in UniqueRoute.objects.filter(to_stop_txt=from_stop_txt): + u.to_stop = from_stop_id + u.save() + for u in UniqueRoute.objects.filter(to_stop_txt=to_stop_txt): + u.to_stop = to_stop_id + u.save() + for u in UniqueRoute.objects.filter(from_stop_txt=to_stop_txt): + u.from_stop = to_stop_id + u.save() + mark_checked = request.POST.get("mark_checked", False) + if mark_checked: + fsm, created = FuzzyStopMatch.objects.get_or_create(unr=unr) + fsm.checked = True + fsm.save() + return HttpResponse("ok") + + + + +#@login_required +#def fuzzystops(request): +## import pdb +# froms_arr = [] +# tos_arr = [] +# for unr in UniqueRoute.objects.all(): +# s1 = unr.from_stop.name.lower() +# s2 = unr.from_stop_txt.lower() +# from_ratio = fuzzprocess.ratio(s1,s2) +# if from_ratio < 70: +# froms_arr.append( +# (unr, from_ratio,) +# ) +# s3 = unr.to_stop.name.lower() +# s4 = unr.to_stop_txt.lower() +# to_ratio = fuzzprocess.ratio(s3,s4) +# if to_ratio < 70: +# tos_arr.append( +# (unr, to_ratio,) +# ) +# +# froms_arr.sort(key=lambda item: item[1]) +# tos_arr.sort(key=lambda item: item[1]) +# context = RequestContext(request, { +# 'fuzzy_froms': [item[0] for item in froms_arr], +# 'fuzzy_tos': [item[0] for item in tos_arr] +# }) +## pdb.set_trace() +# return render_to_response("fuzzystops.html", context) diff --git a/chaloBEST/templates/fuzzystops.html b/chaloBEST/templates/fuzzystops.html index 8b4171f..cd9d3de 100644 --- a/chaloBEST/templates/fuzzystops.html +++ b/chaloBEST/templates/fuzzystops.html @@ -1,3 +1,4 @@ +{% comment %}

Likely Mismatched From stops:

{% for f in fuzzy_froms %} @@ -12,3 +13,106 @@ {{ t.route.alias}} - {{ t.to_stop.name }} - {{ t.to_stop_txt }}
{% endfor %} + +{% endcomment %} + + + + + + + + + + + + + + + + + + + + + + + + {% for u in unrs %} + + + + + + + + + {% endfor %} + +
Bus NoFrom TxtFrom StopTo TxtTo Stop
{{ u.route.alias }}{{ u.from_stop_txt }} + + {{ u.to_stop_txt }} + + + + +   +   + +
+ + + + diff --git a/chaloBEST/urls.py b/chaloBEST/urls.py index c846d0c..49c7eec 100644 --- a/chaloBEST/urls.py +++ b/chaloBEST/urls.py @@ -34,6 +34,7 @@ urlpatterns = patterns('', # Uncomment the next line to enable the admin: (r'^admin/', include(admin.site.urls)), (r'^fuzzystops/$', 'mumbai.views.fuzzystops'), + (r'^fuzzystops_edit/$', 'mumbai.views.fuzzystops_edit'), )