def locator(request): if request.method != "POST": raise Http404 if ( "lat" not in request.POST or "long" not in request.POST or "acc" not in request.POST or "time" not in request.POST ): raise Http404 location = Location() location.latitude = request.POST.get("lat") location.longitude = request.POST.get("long") location.accuracy = request.POST.get("acc") if "wifissid" in request.POST: location.wifissid = request.POST.get("wifissid") if "gsmtype" in request.POST: if request.POST.get("gsmtype").lower() != "unknown": location.gsmtype = request.POST.get("gsmtype") location.devicetimestamp = datetime.datetime.fromtimestamp(Decimal(request.POST.get("time")) / 1000, tz=utc) location.save() return HttpResponse("")
def where(request): start = datetime.datetime.strptime("20140101", "%Y%m%d") end = datetime.datetime.strptime("20150101", "%Y%m%d") accuracies = ( Location.objects.extra(select={"date": "extract (epoch from date_trunc('hour',devicetimestamp))"}) .filter(devicetimestamp__gte=start) .values_list("date") .annotate(avg=Avg("accuracy")) .order_by("date") ) cursor = connection.cursor() cursor.execute( "select extract (epoch from date_trunc('hour',devicetimestamp)) as date, avg(2.23693629*(distance/(timedelta/1000000::float))) from locations where devicetimestamp>'2014-01-01' group by extract (epoch from date_trunc('hour',devicetimestamp)) order by date asc" ) speedresults = cursor.fetchall() speeds = [] for result in speedresults: speeds.append([result[0], float(result[1])]) cursor = connection.cursor() cursor.execute( "select 2.23693629*avg(distance/(timedelta/1000000::float)) from locations where extract(year from devicetimestamp) = 2014;" ) avgspeedrow = cursor.fetchone()[0] cursor.execute( "select 0.000621371192*sum(distance) from locations where extract(year from devicetimestamp) = 2014;" ) totaldistancerow = cursor.fetchone()[0] # Get accuracy / hour histogram cursor.execute( "select extract (hour from devicetimestamp) as hour,avg(accuracy) from locations where extract(year from devicetimestamp) = 2014 group by extract(hour from devicetimestamp) order by hour asc;" ) accuracy_hour_results = cursor.fetchall() accuracy_hours = [] for result in accuracy_hour_results: accuracy_hours.append([result[0], float(result[1])]) # Get speed / hour histogram cursor.execute( "select extract (hour from devicetimestamp) as hour, 2.23693629*avg(distance/(timedelta/1000000::float)) from locations where extract(year from devicetimestamp) = 2014 group by extract(hour from devicetimestamp) order by hour asc;" ) speed_hour_results = cursor.fetchall() speed_hours = [] for result in speed_hour_results: speed_hours.append([result[0], float(result[1])]) return render( request, "where.html", { "accuracies": json.dumps(list(accuracies)), "avgspeed": avgspeedrow, "totaldistance": totaldistancerow, "speeds": list(speeds), "accuracy_hours": list(accuracy_hours), "speed_hours": list(speed_hours), "lastlocation": Location.get_latest(), }, )
def search(request, searchterm=None, page=1): if searchterm is None: if request.method == "GET": return redirect("/", Permanent=True) if request.method == "POST": return redirect("/search/" + request.POST.get("a", "") + "/") else: results_list = Article.objects.extra( select={"rank": "ts_rank(idxfti,plainto_tsquery('english',%s))"}, where=["idxfti @@ plainto_tsquery('english',%s)"], params=[searchterm], select_params=[searchterm, searchterm], ).order_by("-rank") paginator = Paginator(results_list, 10) try: results = paginator.page(page) except (EmptyPage, InvalidPage): results = paginator.page(paginator.num_pages) for result in results: result.snippet = smart_truncate(result.searchtext, searchterm) return render( request, "search.html", {"results": results, "searchterm": searchterm, "lastlocation": Location.get_latest()}, )
def article(request, article_shorttitle=""): if article_shorttitle == "": article = Article.objects.filter(datestamp__isnull=False, published=True).latest("datestamp") else: article = get_object_or_404(Article, shorttitle=article_shorttitle) pickled_navitems = cache.get("navitems") if pickled_navitems is None: navitems = Article.objects.filter(datestamp__isnull=False, published=True).order_by("-datestamp").all() pickled = zlib.compress(cPickle.dumps(navitems, cPickle.HIGHEST_PROTOCOL), 9) cache.set("navitems", pickled, None) else: navitems = cPickle.loads(zlib.decompress(pickled_navitems)) pickled_archives = cache.get("archives") if pickled_archives is None: archives = ( Article.objects.filter(datestamp__isnull=False, published=True) .extra(select={"month": "DATE_TRUNC('month',datestamp)"}) .values("month") .annotate(Count("title")) .order_by("-month") ) prevyear = None for archive in archives: if archive["month"].year != prevyear: archive["newyear"] = True prevyear = archive["month"].year pickled = zlib.compress(cPickle.dumps(archives, cPickle.HIGHEST_PROTOCOL), 9) cache.set("archives", pickled, None) else: archives = cPickle.loads(zlib.decompress(pickled_archives)) return render( request, "article.html", {"archives": archives, "navitems": navitems, "article": article, "lastlocation": Location.get_latest()}, )