<commit_before># coding: utf-8

# maposmatic, the web front-end of the MapOSMatic city map generation system
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Views for MapOSMatic

import datetime

from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _

from ocitysmap2 import OCitySMap, coords, renderers
from www.maposmatic import helpers, forms, nominatim, models
import www.settings

try:
    from json import dumps as json_encode
except ImportError:
    try:
        from cjson import encode as json_encode
    except ImportError:
        from json import write as json_encode

def index(request):
    """The main page."""
    form = forms.MapSearchForm(request.GET)
    return render_to_response('maposmatic/index.html',
                              { 'form': form },
                              context_instance=RequestContext(request))

def about(request):
    """The about page."""
    return render_to_response('maposmatic/about.html',
                              context_instance=RequestContext(request))

def new(request):
    """The map creation page and form."""

    if request.method == 'POST':
        form = forms.MapRenderingJobForm(request.POST)
        if form.is_valid():
            job = form.save(commit=False)
            job.administrative_osmid = form.cleaned_data.get('administrative_osmid')
            job.stylesheet = form.cleaned_data.get('stylesheet')
            job.layout = form.cleaned_data.get('layout')
            job.paper_width_mm = form.cleaned_data.get('paper_width_mm')
            job.paper_height_mm = form.cleaned_data.get('paper_height_mm')
            job.status = 0 # Submitted
            job.submitterip = request.META['REMOTE_ADDR']
            job.map_language = form.cleaned_data.get('map_language')
            job.index_queue_at_submission = (models.MapRenderingJob.objects
                                             .queue_size())
            job.nonce = helpers.generate_nonce(models.MapRenderingJob.NONCE_SIZE)
            job.save()

            return HttpResponseRedirect(reverse('job-by-id-and-nonce',
                                                args=[job.id, job.nonce]))
    else:
        form = forms.MapRenderingJobForm()

    return render_to_response('maposmatic/new.html',
                              { 'form' : form },
                              context_instance=RequestContext(request))

def job(request, job_id, job_nonce=None):
    """The job details page.

    Args:
        job_id (int): the job ID in the database.
    """

    job = get_object_or_404(models.MapRenderingJob, id=job_id)
    isredirected = request.session.get('redirected', False)
    request.session.pop('redirected', None)

    refresh = www.settings.REFRESH_JOB_WAITING
    if job.is_rendering():
        refresh = www.settings.REFRESH_JOB_RENDERING

    return render_to_response('maposmatic/job-page.html',
                              { 'job': job, 'single': True,
                                'redirected': isredirected, 'nonce': job_nonce,
                                'refresh': refresh, 'refresh_ms': (refresh*1000) },
                              context_instance=RequestContext(request))

def all_jobs(request):
    """Displays all jobs from the last 24 hours."""

    one_day_before = datetime.datetime.now() - datetime.timedelta(1)
    job_list = (models.MapRenderingJob.objects.all()
                .order_by('-submission_time')
                .filter(submission_time__gte=one_day_before))
    paginator = Paginator(job_list, www.settings.ITEMS_PER_PAGE)

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        jobs = paginator.page(page)
    except (EmptyPage, InvalidPage):
        jobs = paginator.page(paginator.num_pages)

    return render_to_response('maposmatic/all_jobs.html',
                              { 'jobs': jobs,
                                'pages': helpers.get_pages_list(jobs, paginator) },
                              context_instance=RequestContext(request))

def all_maps(request):
    """Displays all maps, sorted alphabetically, eventually matching the search
    terms, when provided."""

    map_list = None
    form = forms.MapSearchForm(request.GET)

    if form.is_valid():
        map_list = (models.MapRenderingJob.objects
                    .order_by('maptitle')
                    .filter(status=2)
                    .filter(maptitle__icontains=form.cleaned_data['query']))
        if len(map_list) == 1:
            return HttpResponseRedirect(reverse('job-by-id',
                                                args=[map_list[0].id]))
    else:
        form = forms.MapSearchForm()

    if map_list is None:
        map_list = (models.MapRenderingJob.objects.filter(status=2)
                    .filter(resultmsg="ok")
                    .order_by('maptitle'))
    paginator = Paginator(map_list, www.settings.ITEMS_PER_PAGE)

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        maps = paginator.page(page)
    except (EmptyPage, InvalidPage):
        maps = paginator.page(paginator.num_pages)

    return render_to_response('maposmatic/all_maps.html',
                              { 'maps': maps, 'form': form,
                                'is_search': form.is_valid(),
                                'pages': helpers.get_pages_list(maps, paginator) },
                              context_instance=RequestContext(request))

def query_nominatim(request):
    """Nominatim query gateway."""
    exclude = request.GET.get('exclude', '')
    squery = request.GET.get('q', '')

    try:
        contents = nominatim.query(squery, exclude, with_polygons=False)
    except:
        contents = []

    return HttpResponse(content=json_encode(contents),
                        mimetype='text/json')

def nominatim_reverse(request, lat, lon):
    """Nominatim reverse geocoding query gateway."""
    lat = float(lat)
    lon = float(lon)
    return HttpResponse(json_encode(nominatim.reverse_geo(lat, lon)),
                        mimetype='text/json')

def query_papersize(request):
    """AJAX query handler to get the compatible paper sizes for the provided
    layout and bounding box."""

    if request.method == 'POST':
        f = forms.MapPaperSizeForm(request.POST)
        if f.is_valid():
            osmid = f.cleaned_data.get('osmid')
            layout = f.cleaned_data.get('layout')
            if osmid is not None:
                bbox = helpers.get_bbox_from_osm_id(osmid)
            else:
                lat_upper_left = f.cleaned_data.get("lat_upper_left")
                lon_upper_left = f.cleaned_data.get("lon_upper_left")
                lat_bottom_right = f.cleaned_data.get("lat_bottom_right")
                lon_bottom_right = f.cleaned_data.get("lon_bottom_right")
                bbox = coords.BoundingBox(lat_upper_left, lon_upper_left,
                                          lat_bottom_right, lon_bottom_right)

            renderer_cls = renderers.get_renderer_class_by_name(layout)
            paper_sizes = renderer_cls.get_compatible_paper_sizes(
                    bbox, OCitySMap.DEFAULT_ZOOM_LEVEL)
            print 'here:', paper_sizes

            return HttpResponse(content=json_encode(paper_sizes),
                                mimetype='text/json')

    return HttpResponseBadRequest("ERROR: Invalid arguments")

def recreate(request):
    if request.method == 'POST':
        form = forms.MapRecreateForm(request.POST)
        if form.is_valid():
            job = get_object_or_404(models.MapRenderingJob,
                                    id=form.cleaned_data['jobid'])

            newjob = models.MapRenderingJob()
            newjob.maptitle = job.maptitle
<commit_msg>Remove debugging message<commit_after># coding: utf-8

# maposmatic, the web front-end of the MapOSMatic city map generation system
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>
# Copyright (C) 2009  <NAME>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Views for MapOSMatic

import datetime

from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _

from ocitysmap2 import OCitySMap, coords, renderers
from www.maposmatic import helpers, forms, nominatim, models
import www.settings

try:
    from json import dumps as json_encode
except ImportError:
    try:
        from cjson import encode as json_encode
    except ImportError:
        from json import write as json_encode

def index(request):
    """The main page."""
    form = forms.MapSearchForm(request.GET)
    return render_to_response('maposmatic/index.html',
                              { 'form': form },
                              context_instance=RequestContext(request))

def about(request):
    """The about page."""
    return render_to_response('maposmatic/about.html',
                              context_instance=RequestContext(request))

def new(request):
    """The map creation page and form."""

    if request.method == 'POST':
        form = forms.MapRenderingJobForm(request.POST)
        if form.is_valid():
            job = form.save(commit=False)
            job.administrative_osmid = form.cleaned_data.get('administrative_osmid')
            job.stylesheet = form.cleaned_data.get('stylesheet')
            job.layout = form.cleaned_data.get('layout')
            job.paper_width_mm = form.cleaned_data.get('paper_width_mm')
            job.paper_height_mm = form.cleaned_data.get('paper_height_mm')
            job.status = 0 # Submitted
            job.submitterip = request.META['REMOTE_ADDR']
            job.map_language = form.cleaned_data.get('map_language')
            job.index_queue_at_submission = (models.MapRenderingJob.objects
                                             .queue_size())
            job.nonce = helpers.generate_nonce(models.MapRenderingJob.NONCE_SIZE)
            job.save()

            return HttpResponseRedirect(reverse('job-by-id-and-nonce',
                                                args=[job.id, job.nonce]))
    else:
        form = forms.MapRenderingJobForm()

    return render_to_response('maposmatic/new.html',
                              { 'form' : form },
                              context_instance=RequestContext(request))

def job(request, job_id, job_nonce=None):
    """The job details page.

    Args:
        job_id (int): the job ID in the database.
    """

    job = get_object_or_404(models.MapRenderingJob, id=job_id)
    isredirected = request.session.get('redirected', False)
    request.session.pop('redirected', None)

    refresh = www.settings.REFRESH_JOB_WAITING
    if job.is_rendering():
        refresh = www.settings.REFRESH_JOB_RENDERING

    return render_to_response('maposmatic/job-page.html',
                              { 'job': job, 'single': True,
                                'redirected': isredirected, 'nonce': job_nonce,
                                'refresh': refresh, 'refresh_ms': (refresh*1000) },
                              context_instance=RequestContext(request))

def all_jobs(request):
    """Displays all jobs from the last 24 hours."""

    one_day_before = datetime.datetime.now() - datetime.timedelta(1)
    job_list = (models.MapRenderingJob.objects.all()
                .order_by('-submission_time')
                .filter(submission_time__gte=one_day_before))
    paginator = Paginator(job_list, www.settings.ITEMS_PER_PAGE)

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        jobs = paginator.page(page)
    except (EmptyPage, InvalidPage):
        jobs = paginator.page(paginator.num_pages)

    return render_to_response('maposmatic/all_jobs.html',
                              { 'jobs': jobs,
                                'pages': helpers.get_pages_list(jobs, paginator) },
                              context_instance=RequestContext(request))

def all_maps(request):
    """Displays all maps, sorted alphabetically, eventually matching the search
    terms, when provided."""

    map_list = None
    form = forms.MapSearchForm(request.GET)

    if form.is_valid():
        map_list = (models.MapRenderingJob.objects
                    .order_by('maptitle')
                    .filter(status=2)
                    .filter(maptitle__icontains=form.cleaned_data['query']))
        if len(map_list) == 1:
            return HttpResponseRedirect(reverse('job-by-id',
                                                args=[map_list[0].id]))
    else:
        form = forms.MapSearchForm()

    if map_list is None:
        map_list = (models.MapRenderingJob.objects.filter(status=2)
                    .filter(resultmsg="ok")
                    .order_by('maptitle'))
    paginator = Paginator(map_list, www.settings.ITEMS_PER_PAGE)

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        maps = paginator.page(page)
    except (EmptyPage, InvalidPage):
        maps = paginator.page(paginator.num_pages)

    return render_to_response('maposmatic/all_maps.html',
                              { 'maps': maps, 'form': form,
                                'is_search': form.is_valid(),
                                'pages': helpers.get_pages_list(maps, paginator) },
                              context_instance=RequestContext(request))

def query_nominatim(request):
    """Nominatim query gateway."""
    exclude = request.GET.get('exclude', '')
    squery = request.GET.get('q', '')

    try:
        contents = nominatim.query(squery, exclude, with_polygons=False)
    except:
        contents = []

    return HttpResponse(content=json_encode(contents),
                        mimetype='text/json')

def nominatim_reverse(request, lat, lon):
    """Nominatim reverse geocoding query gateway."""
    lat = float(lat)
    lon = float(lon)
    return HttpResponse(json_encode(nominatim.reverse_geo(lat, lon)),
                        mimetype='text/json')

def query_papersize(request):
    """AJAX query handler to get the compatible paper sizes for the provided
    layout and bounding box."""

    if request.method == 'POST':
        f = forms.MapPaperSizeForm(request.POST)
        if f.is_valid():
            osmid = f.cleaned_data.get('osmid')
            layout = f.cleaned_data.get('layout')
            if osmid is not None:
                bbox = helpers.get_bbox_from_osm_id(osmid)
            else:
                lat_upper_left = f.cleaned_data.get("lat_upper_left")
                lon_upper_left = f.cleaned_data.get("lon_upper_left")
                lat_bottom_right = f.cleaned_data.get("lat_bottom_right")
                lon_bottom_right = f.cleaned_data.get("lon_bottom_right")
                bbox = coords.BoundingBox(lat_upper_left, lon_upper_left,
                                          lat_bottom_right, lon_bottom_right)

            renderer_cls = renderers.get_renderer_class_by_name(layout)
            paper_sizes = renderer_cls.get_compatible_paper_sizes(
                    bbox, OCitySMap.DEFAULT_ZOOM_LEVEL)

            return HttpResponse(content=json_encode(paper_sizes),
                                mimetype='text/json')

    return HttpResponseBadRequest("ERROR: Invalid arguments")

def recreate(request):
    if request.method == 'POST':
        form = forms.MapRecreateForm(request.POST)
        if form.is_valid():
            job = get_object_or_404(models.MapRenderingJob,
                                    id=form.cleaned_data['jobid'])

            newjob = models.MapRenderingJob()
            newjob.maptitle = job.maptitle

{'fraction_non_alphanumeric': 0.09793988517392772, 'fraction_numerical': 0.006304176516942475, 'mean_word_length': 2.332145536384096, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 14, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6925043', 'n_tokens_mistral': 5179, 'n_tokens_neox': 4969, 'n_words': 1135}