<commit_before>// Molly Geolocation code
function submitAutomaticLocation(position, method) {
  $('.location').html('Location found; please wait while we put a name to it.')
  $('.location-accuracy').hide()
  $.post(base+'geolocation/', {
    longitude: position.coords.longitude,
    latitude: position.coords.latitude,
    accuracy: position.coords.accuracy,
    method: method,
    format: 'json',
    return_url: $('#return_url').val(),
    force: 'True'
  }, locationFound, 'json');
}

function automaticLocation(position) {
  submitAutomaticLocation(position, 'html5')
}

function automaticLocationAndSave(position) {
  submitAutomaticLocation(position, 'html5request')
}

function locationFailure(d) {
  $('.location-accuracy').hide()
  // Show error
  if (d.code == 1) { // PERMISSION_DENIED
    $('.location').html('<i>You did not give permission for the site to know your location.</i>');
    $.post(base+'geolocation/', {
      method: 'denied'
    });
  } else if (d.code == 2 || d.code == 3) { // POSITION_UNAVAILABLE / TIMEOUT
    $('.location').html('<i>We couldn\'t get a fix on your location right now</i>')
    $.post(base+'geolocation/', {
      method: 'error'
    });
  } else {
    // Unknown error
    $('.location').html('<i>An error occurred: ' + d.message + '</i>')
  }
  // Default back to location after 5 seconds
  window.setTimeout(function() {
    $('.location').text(locationName);
    $('.location-accuracy').show()
  }, 5000);
}

$(function(){
  
  if(geo_position_js.init()) {
    // Add automatic location buttons if geolocation API is available
    $('.update-location-form').append('<ul class="link-list location-automatic-list"><li><input type="submit" value="Get location automatically" class="automatic-update as-text-link" /></li></ul>');
    if ($('.favourite-locations-list').length + $('.historic-locations-list').length) {
      $('.location-automatic-list').addClass('no-round-bottom')
    }
    $('.current-location-box p').prepend('<input type="submit" value="Determine location automatically" class="automatic-update automatic-update-button" />')
    $('.automatic-update, .location-automatic-list').click(function(){
      $('.update-location-box').slideUp();
      $('.current-location-box').slideDown();
      $('.alternate-location-box').slideUp();
      $('.location').html('Please wait while we attempt to determine your location&hellip;')    
      $('.location-accuracy').hide()
      geo_position_js.getCurrentPosition(automaticLocationAndSave, locationFailure, {
        enableHighAccuracy: true,
        maximumAge: 30000
      });
      return false;
    });
    // Attempt to do location if available
    if (locationRequired) {
      geo_position_js.getCurrentPosition(automaticLocation, locationFailure, {
        enableHighAccuracy: true,
        maximumAge: 30000
      });
    }
    // If this page is open a while, then automatically send geolocation updates back
    if (autoLocationUpdating) {
      setTimeout(function(){
        geo_position_js.getCurrentPosition(automaticLocation, locationFailure, {
          enableHighAccuracy: true,
          maximumAge: 30000
        });
      }, 600000)
    }
  }
  
  // Switch to update view from display
  $('.update-location-form').append('<input type="button" value="Cancel Update" class="cancel-update as-text-link" />');
  $('.current-location-box form input').click(function(){
    $('.current-location-box').hide();
    $('.cancel-update').click(function(){
      $('.update-location-box').slideUp();
      $('.alternate-location-box').slideUp();
      $('.current-location-box').slideDown();
    })
    $('.alternate-location-box').hide();
    $('.update-location-box').slideDown();
    $(window).scrollTop($('#location-box').offset().top)
    return false;
  });
  $('.update-location-form').submit(function() {
    $('.update-location-box').slideUp();
    $('.alternate-location-box').slideUp();
    $('.current-location-box').slideDown();
    $('.location').html('Please wait while we attempt to determine your location&hellip;')
    $.post(base+'geolocation/', {
      format: 'json',
      method: 'geocoded',
      name: $(this).find('.update-location-name').val()
    }, locationFound, 'json');
    return false;
  })
  $('.specific-location-form').submit(specificLocationFormSubmit)
  $('.favourite-location-form').submit(favouriteLocationFormSubmit)

  $('.update-location-name').focus(function(){
    $(this).val('');
    $(this).css('color', '#000000')
    $(this).unbind('focus')
  });
  $('.update-location-name').val('e.g., OX2 6NN, kebl, St Clements')
  $('.update-location-name').css('color', '#a3a3a3')

});

function specificLocationForm(location, favourite) {
  f  = '  <form class="specific-location-form" method="post" action="'+base+'geolocation/">'
     +      csrfToken
     + '    <input type="hidden" name="method" value="manual"/>'
     + '    <input type="hidden" name="accuracy" value="'+location.accuracy+'"/>'
     + '    <input type="hidden" name="longitude" value="'+location.location[0]+'"/>'
     + '    <input type="hidden" name="latitude" value="'+location.location[1]+'"/>'
     + '    <input type="hidden" name="return_url" value="'+window.location.pathname+'"/>'
     + '    <input type="hidden" name="name" value="'+location.name+'"/>'
     + '    <input type="submit" class="as-text-link" value="'+location.name+'" style="font-weight: normal;" />'
     + '  </form>'
  if (favourite != null)
  {
    f += '  <form class="favourite-location-form" method="post" action="'+base+'geolocation/favourites/">'
       +      csrfToken
    if (favourite) {
      f += '    <input type="hidden" name="action" value="remove"/>'
         + '    <input type="hidden" name="id" value="'+location.id+'"/>'
         + '    <input type="hidden" name="return_url" value="'+window.location.pathname+'"/>'
         + '    <input type="submit" class="unfavourite" value="(Remove from favourites)"/>'
    } else {
      f += '    <input type="hidden" name="action" value="add"/>'
         + '    <input type="hidden" name="accuracy" value="'+location.accuracy+'"/>'
         + '    <input type="hidden" name="longitude" value="'+location.location[0]+'"/>'
         + '    <input type="hidden" name="latitude" value="'+location.location[1]+'"/>'
         + '    <input type="hidden" name="return_url" value="'+window.location.pathname+'"/>'
         + '    <input type="hidden" name="name" value="'+location.name+'"/>'
         + '    <input type="submit" class="favourite" value="(Add as favourite)"/>'
    }
    f += '  </form>'
  }
  return f
}

function specificLocationFormSubmit() {
  $('.update-location-box').slideUp();
  $('.alternate-location-box').slideUp();
  $('.current-location-box').slideDown();
  $.post($(this).attr('action'), {
        longitude: $(this).find('[name=longitude]').val(),
        latitude: $(this).find('[name=latitude]').val(),
        accuracy: $(this).find('[name=accuracy]').val(),
        name: $(this).find('[name=name]').val(),
        return_url: $(this).find('[name=return_url]').val(),
        method: $(this).find('[name=method]').val(),
        id: $(this).find('[name=id]').val(),
        action: $(this).find('[name=action]').val(),
        format: 'json',
        force: 'True'
    }, locationFound, 'json');
  return false;
}

function favouriteLocationFormSubmit() {
  $.post($(this).attr('action'), {
        longitude: $(this).find('[name=longitude]').val(),
        latitude: $(this).find('[name=latitude]').val(),
        accuracy: $(this).find('[name=accuracy]').val(),
        name: $(this).find('[name=name]').val(),
        return_url: $(this).find('[name=return_url]').val(),
        method: $(this).find('[name=method]').val(),
        id: $(this).find('[name=id]').val(),
        action: $(this).find('[name=action]').val(),
        format: 'json',
        force: 'True'
    }, locationFound, 'json');
  return false;
}

function locationFound(data) {<commit_msg>Add CSRF tokens on AJAX callbacks (for Django 1.2.5 - issue #50)<commit_after>// Molly Geolocation code
function submitAutomaticLocation(position, method) {
  $('.location').html('Location found; please wait while we put a name to it.')
  $('.location-accuracy').hide()
  $.post(base+'geolocation/', {
    csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
    longitude: position.coords.longitude,
    latitude: position.coords.latitude,
    accuracy: position.coords.accuracy,
    method: method,
    format: 'json',
    return_url: $('#return_url').val(),
    force: 'True'
  }, locationFound, 'json');
}

function automaticLocation(position) {
  submitAutomaticLocation(position, 'html5')
}

function automaticLocationAndSave(position) {
  submitAutomaticLocation(position, 'html5request')
}

function locationFailure(d) {
  $('.location-accuracy').hide()
  // Show error
  if (d.code == 1) { // PERMISSION_DENIED
    $('.location').html('<i>You did not give permission for the site to know your location.</i>');
    $.post(base+'geolocation/', {
      csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
      method: 'denied'
    });
  } else if (d.code == 2 || d.code == 3) { // POSITION_UNAVAILABLE / TIMEOUT
    $('.location').html('<i>We couldn\'t get a fix on your location right now</i>')
    $.post(base+'geolocation/', {
      csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
      method: 'error'
    });
  } else {
    // Unknown error
    $('.location').html('<i>An error occurred: ' + d.message + '</i>')
  }
  // Default back to location after 5 seconds
  window.setTimeout(function() {
    $('.location').text(locationName);
    $('.location-accuracy').show()
  }, 5000);
}

$(function(){
  
  if(geo_position_js.init()) {
    // Add automatic location buttons if geolocation API is available
    $('.update-location-form').append('<ul class="link-list location-automatic-list"><li><input type="submit" value="Get location automatically" class="automatic-update as-text-link" /></li></ul>');
    if ($('.favourite-locations-list').length + $('.historic-locations-list').length) {
      $('.location-automatic-list').addClass('no-round-bottom')
    }
    $('.current-location-box p').prepend('<input type="submit" value="Determine location automatically" class="automatic-update automatic-update-button" />')
    $('.automatic-update, .location-automatic-list').click(function(){
      $('.update-location-box').slideUp();
      $('.current-location-box').slideDown();
      $('.alternate-location-box').slideUp();
      $('.location').html('Please wait while we attempt to determine your location&hellip;')    
      $('.location-accuracy').hide()
      geo_position_js.getCurrentPosition(automaticLocationAndSave, locationFailure, {
        enableHighAccuracy: true,
        maximumAge: 30000
      });
      return false;
    });
    // Attempt to do location if available
    if (locationRequired) {
      geo_position_js.getCurrentPosition(automaticLocation, locationFailure, {
        enableHighAccuracy: true,
        maximumAge: 30000
      });
    }
    // If this page is open a while, then automatically send geolocation updates back
    if (autoLocationUpdating) {
      setTimeout(function(){
        geo_position_js.getCurrentPosition(automaticLocation, locationFailure, {
          enableHighAccuracy: true,
          maximumAge: 30000
        });
      }, 600000)
    }
  }
  
  // Switch to update view from display
  $('.update-location-form').append('<input type="button" value="Cancel Update" class="cancel-update as-text-link" />');
  $('.current-location-box form input').click(function(){
    $('.current-location-box').hide();
    $('.cancel-update').click(function(){
      $('.update-location-box').slideUp();
      $('.alternate-location-box').slideUp();
      $('.current-location-box').slideDown();
    })
    $('.alternate-location-box').hide();
    $('.update-location-box').slideDown();
    $(window).scrollTop($('#location-box').offset().top)
    return false;
  });
  $('.update-location-form').submit(function() {
    $('.update-location-box').slideUp();
    $('.alternate-location-box').slideUp();
    $('.current-location-box').slideDown();
    $('.location').html('Please wait while we attempt to determine your location&hellip;')
    $.post(base+'geolocation/', {
      csrfmiddlewaretoken: $(this).find('[name=csrfmiddlewaretoken]').val(),
      format: 'json',
      method: 'geocoded',
      name: $(this).find('.update-location-name').val()
    }, locationFound, 'json');
    return false;
  })
  $('.specific-location-form').submit(specificLocationFormSubmit)
  $('.favourite-location-form').submit(favouriteLocationFormSubmit)

  $('.update-location-name').focus(function(){
    $(this).val('');
    $(this).css('color', '#000000')
    $(this).unbind('focus')
  });
  $('.update-location-name').val('e.g., OX2 6NN, kebl, St Clements')
  $('.update-location-name').css('color', '#a3a3a3')

});

function specificLocationForm(location, favourite) {
  f  = '  <form class="specific-location-form" method="post" action="'+base+'geolocation/">'
     +      csrfToken
     + '    <input type="hidden" name="method" value="manual"/>'
     + '    <input type="hidden" name="accuracy" value="'+location.accuracy+'"/>'
     + '    <input type="hidden" name="longitude" value="'+location.location[0]+'"/>'
     + '    <input type="hidden" name="latitude" value="'+location.location[1]+'"/>'
     + '    <input type="hidden" name="return_url" value="'+window.location.pathname+'"/>'
     + '    <input type="hidden" name="name" value="'+location.name+'"/>'
     + '    <input type="submit" class="as-text-link" value="'+location.name+'" style="font-weight: normal;" />'
     + '  </form>'
  if (favourite != null)
  {
    f += '  <form class="favourite-location-form" method="post" action="'+base+'geolocation/favourites/">'
       +      csrfToken
    if (favourite) {
      f += '    <input type="hidden" name="action" value="remove"/>'
         + '    <input type="hidden" name="id" value="'+location.id+'"/>'
         + '    <input type="hidden" name="return_url" value="'+window.location.pathname+'"/>'
         + '    <input type="submit" class="unfavourite" value="(Remove from favourites)"/>'
    } else {
      f += '    <input type="hidden" name="action" value="add"/>'
         + '    <input type="hidden" name="accuracy" value="'+location.accuracy+'"/>'
         + '    <input type="hidden" name="longitude" value="'+location.location[0]+'"/>'
         + '    <input type="hidden" name="latitude" value="'+location.location[1]+'"/>'
         + '    <input type="hidden" name="return_url" value="'+window.location.pathname+'"/>'
         + '    <input type="hidden" name="name" value="'+location.name+'"/>'
         + '    <input type="submit" class="favourite" value="(Add as favourite)"/>'
    }
    f += '  </form>'
  }
  return f
}

function specificLocationFormSubmit() {
  $('.update-location-box').slideUp();
  $('.alternate-location-box').slideUp();
  $('.current-location-box').slideDown();
  $.post($(this).attr('action'), {
        csrfmiddlewaretoken: $(this).find('[name=csrfmiddlewaretoken]').val(),
        longitude: $(this).find('[name=longitude]').val(),
        latitude: $(this).find('[name=latitude]').val(),
        accuracy: $(this).find('[name=accuracy]').val(),
        name: $(this).find('[name=name]').val(),
        return_url: $(this).find('[name=return_url]').val(),
        method: $(this).find('[name=method]').val(),
        id: $(this).find('[name=id]').val(),
        action: $(this).find('[name=action]').val(),
        format: 'json',
        force: 'True'
    }, locationFound, 'json');
  return false;
}

function favouriteLocationFormSubmit() {
  $.post($(this).attr('action'), {
        csrfmiddlewaretoken: $(this).find('[name=csrfmiddlewaretoken]').val(),
        longitude: $(this).find('[name=longitude]').val(),
        latitude: $(this).find('[name=latitude]').val(),
        accuracy: $(this).find('[name=accuracy]').val(),
        name: $(this).find('[name=name]').val(),
        return_url: $(this).find('[name=return_url]').val(),
        method: $(this).find('[name=method]').val(),
        id: $(this).find('[name=id]').val(),
        action: $(this).find('[name=action]').val(),
        format: 'json',
        force: 'True'
    }, locationFound, 'json');
  return false;
}

function locationFound(data) {
{'fraction_non_alphanumeric': 0.209587038616153, 'fraction_numerical': 0.0059081495919113165, 'mean_word_length': 3.5709910913140313, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, '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': '333258', 'n_tokens_mistral': 5202, 'n_tokens_neox': 5089, 'n_words': 882}