<commit_before>from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.shortcuts import render_to_response
from datetime import datetime, timedelta
from tracking.models import LoggedPoint
import requests
import json
import unicodecsv
import pytz

from tracking.models import Device


def index(request):
    return render_to_response("index.html", {
        "settings": settings,
        "user": request.user
    })


def print_map(request):
    return render_to_response("print.html", {
        "settings": settings
    })


def device(request, device_id):
    return render_to_response("device.html", {
        "device": Device.objects.get(pk=device_id)
    })


def device_csv(request):
    """Query the Device API CSV endpoint, return a file attachment.
    """
    api_url = reverse('api_dispatch_list', kwargs={'api_name': 'v1', 'resource_name': 'device'})
    params = {'limit': 10000, 'format': 'csv'}
    # Allow filtering by ``seen_age__lte=<minutes>`` query param
    if 'seen_age__lte' in request.GET:
        params['seen_age__lte'] = request.GET['seen_age__lte']
    r = requests.get(request.build_absolute_uri(api_url), params=params, cookies=request.COOKIES)

    if not r.status_code == 200:
        r.raise_for_status()

    response = HttpResponse(r.content, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=tracking_devices.csv'
    return response

def get_vehicles(request):
    if request.is_ajax():
        term = request.GET.get('term','')
        r = requests.get(settings.KMI_VEHICLE_BASE_URL + "&CQL_FILTER=rego%20ilike%20%27%25"+term+"%25%27", auth=(settings.EMAIL_USER,settings.EMAIL_PASSWORD))
        vehicle_features = json.loads(r.content).get('features')
        results = []
        for vehicle in vehicle_features:
            vehicle_json = {}
            vehicle_json['value'] = vehicle.get('properties').get('rego')
            vehicle_json['label'] = vehicle.get('properties').get('rego') + ', ' + vehicle.get('properties').get('make_desc') +', '+\
                vehicle.get('properties').get('model_desc') +', '+ vehicle.get('properties').get('category_desc')
            results.append(vehicle_json)
        data = json.dumps(results)
    else:
        data = ''
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)


def export_stop_start_points(request):

    query_list = []
    try:
        fromdate = request.GET['fromdate']
    except:
        fromdate = None
    try:
        todate = request.GET['todate']
    except:
        todate = None

    if fromdate and todate:
        try:
            fromdate = pytz.timezone("Australia/Perth").localize(datetime.strptime(fromdate, '%d-%m-%Y'))
            fromdate = fromdate.astimezone(pytz.UTC)
            todate = pytz.timezone("Australia/Perth").localize(datetime.strptime(todate, '%d-%m-%Y'))
            todate = todate.astimezone(pytz.UTC)
        except:
            pass
    elif fromdate:
        try:
            fromdate = pytz.timezone("Australia/Perth").localize(datetime.strptime(fromdate, '%d-%m-%Y'))
            fromdate = fromdate.astimezone(pytz.UTC)
            todate = fromdate + timedelta(days=30)
        except:
            pass
    elif todate:
        try:
            todate = pytz.timezone("Australia/Perth").localize(datetime.strptime(todate, '%d-%m-%Y'))
            todate = todate.astimezone(pytz.UTC)
            fromdate = todate + timedelta(days=-30)
        except:
            pass
    else:
        fromdate = pytz.utc.localize(datetime.utcnow()) + timedelta(days=-30)
        todate = pytz.utc.localize(datetime.utcnow())

    filename = 'SSS_LoggedPoint_{}-{}.csv'.format(fromdate.strftime('%Y%m%d'), todate.strftime('%Y%m%d'))
    points = LoggedPoint.objects.filter(seen__gte=fromdate,seen__lte=todate,message__in=(1,2,25,26)).order_by('seen')

    for p in points:
        seen = datetime.strftime(p.seen.astimezone(pytz.timezone("Australia/Perth")), "%d-%m-%Y %H:%M:%S")
        device_id = p.device.deviceid
        message = p.get_message_display()
        latitude = p.point.y<commit_msg>tracking: stop_start_points filename dates to AU/Perth timezone<commit_after>from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.shortcuts import render_to_response
from datetime import datetime, timedelta
from tracking.models import LoggedPoint
import requests
import json
import unicodecsv
import pytz

from tracking.models import Device


def index(request):
    return render_to_response("index.html", {
        "settings": settings,
        "user": request.user
    })


def print_map(request):
    return render_to_response("print.html", {
        "settings": settings
    })


def device(request, device_id):
    return render_to_response("device.html", {
        "device": Device.objects.get(pk=device_id)
    })


def device_csv(request):
    """Query the Device API CSV endpoint, return a file attachment.
    """
    api_url = reverse('api_dispatch_list', kwargs={'api_name': 'v1', 'resource_name': 'device'})
    params = {'limit': 10000, 'format': 'csv'}
    # Allow filtering by ``seen_age__lte=<minutes>`` query param
    if 'seen_age__lte' in request.GET:
        params['seen_age__lte'] = request.GET['seen_age__lte']
    r = requests.get(request.build_absolute_uri(api_url), params=params, cookies=request.COOKIES)

    if not r.status_code == 200:
        r.raise_for_status()

    response = HttpResponse(r.content, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=tracking_devices.csv'
    return response

def get_vehicles(request):
    if request.is_ajax():
        term = request.GET.get('term','')
        r = requests.get(settings.KMI_VEHICLE_BASE_URL + "&CQL_FILTER=rego%20ilike%20%27%25"+term+"%25%27", auth=(settings.EMAIL_USER,settings.EMAIL_PASSWORD))
        vehicle_features = json.loads(r.content).get('features')
        results = []
        for vehicle in vehicle_features:
            vehicle_json = {}
            vehicle_json['value'] = vehicle.get('properties').get('rego')
            vehicle_json['label'] = vehicle.get('properties').get('rego') + ', ' + vehicle.get('properties').get('make_desc') +', '+\
                vehicle.get('properties').get('model_desc') +', '+ vehicle.get('properties').get('category_desc')
            results.append(vehicle_json)
        data = json.dumps(results)
    else:
        data = ''
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)


def export_stop_start_points(request):

    query_list = []
    try:
        fromdate = request.GET['fromdate']
    except:
        fromdate = None
    try:
        todate = request.GET['todate']
    except:
        todate = None

    if fromdate and todate:
        try:
            fromdate = pytz.timezone("Australia/Perth").localize(datetime.strptime(fromdate, '%d-%m-%Y'))
            fromdate = fromdate.astimezone(pytz.UTC)
            todate = pytz.timezone("Australia/Perth").localize(datetime.strptime(todate, '%d-%m-%Y'))
            todate = todate.astimezone(pytz.UTC)
        except:
            pass
    elif fromdate:
        try:
            fromdate = pytz.timezone("Australia/Perth").localize(datetime.strptime(fromdate, '%d-%m-%Y'))
            fromdate = fromdate.astimezone(pytz.UTC)
            todate = fromdate + timedelta(days=30)
        except:
            pass
    elif todate:
        try:
            todate = pytz.timezone("Australia/Perth").localize(datetime.strptime(todate, '%d-%m-%Y'))
            todate = todate.astimezone(pytz.UTC)
            fromdate = todate + timedelta(days=-30)
        except:
            pass
    else:
        fromdate = pytz.utc.localize(datetime.utcnow()) + timedelta(days=-30)
        todate = pytz.utc.localize(datetime.utcnow())

    fromdatetext = fromdate.astimezone(pytz.timezone("Australia/Perth"))
    todatetext = todate.astimezone(pytz.timezone("Australia/Perth"))
    filename = 'SSS_LoggedPoint_{}-{}.csv'.format(fromdatetext.strftime('%Y%m%d'), todatetext.strftime('%Y%m%d'))
    points = LoggedPoint.objects.filter(seen__gte=fromdate,seen__lte=todate,message__in=(1,2,25,26)).order_by('seen')

    for p in points:
        seen = datetime.strftime(p.seen.astimezone(pytz.timezone("Australia/Perth")), "%d-%m-%Y %H:%M:%S")
        device_id = p.device.deviceid
        message = p.get_message_display()
        latitude = p.point.y
{'fraction_non_alphanumeric': 0.13703703703703704, 'fraction_numerical': 0.007638888888888889, 'mean_word_length': 3.2233626588465296, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7104469', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2756, 'n_words': 475}