<commit_before>import logging

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.template import RequestContext, loader

from busgokr.models import *
from busgokr.utils import search_bus_live, add_line_to_db, get_stations_by_line, search_stations_live, add_segment_to_db


logger = logging.getLogger(__name__)

RESULT_LIST = 'resultList'
BUS_LIST = 'busList'


def update_segments(request):
    busroutes = BusRoute.objects.all()
    for route in busroutes:
        try:
            SearchedLive.objects.get(busroute=route)
        except SearchedLive.DoesNotExist:
            segments = get_stations_by_line(route.id)
            segments = segments[RESULT_LIST]
            for segment in segments:
                add_segment_to_db(segment)
            SearchedLive(busroute=route).save()
    return redirect('/busgokr/lines/')

def update_lines(request):
    search = ''
    live_lines = search_bus_live(search)
    if RESULT_LIST in live_lines:
        live_lines = live_lines[RESULT_LIST]
        for line in live_lines:
            add_line_to_db(line)
    lines = BusRoute.objects.filter(name__contains=search).order_by('route_type', 'name')
    context = RequestContext(request, {
        'lines': lines,
        'search': search,
    })
    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))


def all_lines(request):
    if request.method == 'POST' and 'search' in request.POST:
        return HttpResponseRedirect(request.POST['search'] + '/')
    lines = BusRoute.objects.all()[:100]
    context = RequestContext(request, {
        'lines': lines,
    })

    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))


def search_lines(request, query):
    search = query
    lines = BusRoute.objects.filter(name__contains=search).order_by('route_type', 'name')
    context = RequestContext(request, {
        'lines': lines,
        'search': search,
    })
    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))


def search_stations(request):
    if request.method == 'POST' and 'search' in request.POST:
        search = request.POST['search']
        stations = BusStation.objects.filter(name__icontains=search)
        context = RequestContext(request, {
            'stations': stations,
            'search': search,
        })
    else:
        stations = BusStation.objects.all()[:100]
        context = RequestContext(request, {
            'stations': stations,
        })

    template = loader.get_template('busgokr/stations_all.html')

    return HttpResponse(template.render(context))


def line_detail(request, line_id):
    referer = '/busgokr/lines/'
    if 'HTTP_REFERER' in request.META:
        referer = request.META['HTTP_REFERER']
    busroute = BusRoute.objects.get(id=line_id)
    try:
        SearchedLive.objects.get(busroute=busroute)
    except SearchedLive.DoesNotExist:
        segments = get_stations_by_line(line_id)
        logger.error(len(segments))
        if RESULT_LIST in segments != "None":
            segments = segments[RESULT_LIST]
            for segment in segments:
                add_segment_to_db(segment)
        SearchedLive(busroute=busroute).save()

    segments = Sequence.objects.filter(route=busroute).order_by('number')

    template = loader.get_template('busgokr/line_detail.html')
    context = RequestContext(request, {
        'segments': segments,
        'referer': referer,
    })
    return HttpResponse(template.render(context))


def station_detail(request, station_id):
    referer = '/busgokr/lines/'
    if 'HTTP_REFERER' in request.META:
        referer = request.META['HTTP_REFERER']
    station = BusStation.objects.get(id=station_id)
    sequences = Sequence.objects.filter(station=station)
    template = loader.get_template('busgokr/station_detail.html')
    context = RequestContext(request, {
        'station': station,
        'sequences': sequences,
        'referer': referer,
    })
    return HttpResponse(template.render(context))

def directions(request, direction):
    search = direction
    location = Location.objects.get(name=search)
    sequences = Sequence.objects.filter(direction=location)
    lines = []
    for s in sequences:
        line = s.route
        if line not in lines:
            lines.append(s.route)
    context = RequestContext(request, {
        'lines': lines,
        'search': search,
        })
    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))<commit_msg>fix station search and add lines by corporation<commit_after>import logging

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.template import RequestContext, loader

from busgokr.models import *
from busgokr.utils import search_bus_live, add_line_to_db, get_stations_by_line, search_stations_live, add_segment_to_db


logger = logging.getLogger(__name__)

RESULT_LIST = 'resultList'
BUS_LIST = 'busList'


def update_segments(request):
    busroutes = BusRoute.objects.all()
    for route in busroutes:
        try:
            SearchedLive.objects.get(busroute=route)
        except SearchedLive.DoesNotExist:
            segments = get_stations_by_line(route.id)
            segments = segments[RESULT_LIST]
            for segment in segments:
                add_segment_to_db(segment)
            SearchedLive(busroute=route).save()
    return redirect('/busgokr/lines/')

def update_lines(request):
    search = ''
    live_lines = search_bus_live(search)
    if RESULT_LIST in live_lines:
        live_lines = live_lines[RESULT_LIST]
        for line in live_lines:
            add_line_to_db(line)
    lines = BusRoute.objects.filter(name__contains=search).order_by('route_type', 'name')
    context = RequestContext(request, {
        'lines': lines,
        'search': search,
    })
    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))


def all_lines(request):
    if request.method == 'POST' and 'search' in request.POST:
        return HttpResponseRedirect(request.POST['search'] + '/')
    lines = BusRoute.objects.all()[:100]
    context = RequestContext(request, {
        'lines': lines,
    })

    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))


def search_lines(request, query):
    search = query
    lines = BusRoute.objects.filter(name__contains=search).order_by('route_type', 'name')
    context = RequestContext(request, {
        'lines': lines,
        'search': search,
    })
    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))


def all_stations(request):
    if request.method == 'POST' and 'search' in request.POST:
        return HttpResponseRedirect(request.POST['search'] + '/')
    stations = BusStation.objects.all()[:100]
    context = RequestContext(request, {
        'stations': stations,
    })

    template = loader.get_template('busgokr/stations_all.html')

    return HttpResponse(template.render(context))

def search_stations(request, query):
    search = query
    locations = Location.objects.filter(name__icontains=search)
    stations = []
    for l in locations:
        stations += BusStation.objects.filter(name=l)
    logger.error(stations)
    context = RequestContext(request, {
        'stations': stations,
        'search': search,
    })
    template = loader.get_template('busgokr/stations_all.html')

    return HttpResponse(template.render(context))


def line_detail(request, line_id):
    referer = '/busgokr/lines/'
    if 'HTTP_REFERER' in request.META:
        referer = request.META['HTTP_REFERER']
    busroute = BusRoute.objects.get(id=line_id)
    try:
        SearchedLive.objects.get(busroute=busroute)
    except SearchedLive.DoesNotExist:
        segments = get_stations_by_line(line_id)
        logger.error(len(segments))
        if RESULT_LIST in segments != "None":
            segments = segments[RESULT_LIST]
            for segment in segments:
                add_segment_to_db(segment)
        SearchedLive(busroute=busroute).save()

    segments = Sequence.objects.filter(route=busroute).order_by('number')

    template = loader.get_template('busgokr/line_detail.html')
    context = RequestContext(request, {
        'segments': segments,
        'referer': referer,
    })
    return HttpResponse(template.render(context))


def station_detail(request, station_id):
    referer = '/busgokr/lines/'
    if 'HTTP_REFERER' in request.META:
        referer = request.META['HTTP_REFERER']
    station = BusStation.objects.get(id=station_id)
    sequences = Sequence.objects.filter(station=station)
    template = loader.get_template('busgokr/station_detail.html')
    context = RequestContext(request, {
        'station': station,
        'sequences': sequences,
        'referer': referer,
    })
    return HttpResponse(template.render(context))

def directions(request, direction):
    search = direction
    location = Location.objects.get(name=search)
    sequences = Sequence.objects.filter(direction=location)
    lines = []
    for s in sequences:
        line = s.route
        if line not in lines:
            lines.append(s.route)
    context = RequestContext(request, {
        'lines': lines,
        'search': search,
        })
    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))

def corporations(request, id):
    corporation = Corporation.objects.get(id=id)
    lines = BusRoute.objects.filter(corporation=corporation)
    context = RequestContext(request, {
        'lines': lines,
    })

    template = loader.get_template('busgokr/lines_all.html')

    return HttpResponse(template.render(context))
{'fraction_non_alphanumeric': 0.10904175428056473, 'fraction_numerical': 0.0012015620306398318, 'mean_word_length': 3.312607944732297, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '7343214', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2988, 'n_words': 555}