<commit_before>from django.contrib import admin
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.geos import LineString

from atlas.models import Location, City, Country, Region
from atlas.utils import get_city


class LocationAdminForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(LocationAdminForm, self).__init__(*args, **kwargs)
        self.fields['country'].required = False
        self.fields['city'].required = False

    def clean(self, *args, **kwargs):
        cd = super(LocationAdminForm, self).clean(*args, **kwargs)
        if cd['city'] == 'no_city':
            del cd['city']

        # fill in the blanks on the form, if possible
        if cd.get('coordinates', None) and not cd.get('city', None):
            cd['city'] = get_city(position=cd['coordinates'])
        if cd.get('city', None):
            if not cd.get('country', None):
                cd['country'] = cd['city'].country
        else:
            self.errors.clear()
            raise forms.ValidationError("A city or coordinates are required.")

        # check that fields are consistent
        success, msg = self.check_consistency(cd)
        if not success:
            raise forms.ValidationError(msg)

        return cd

    def clean_city(self):
        if self.cleaned_data.get('city', None):
            return self.cleaned_data['city']
        return 'no_city'

    # checks that fields are geographically consistent
    def check_consistency(self, cleaned_data):
        if cleaned_data['city'].country != cleaned_data['country']:
            return (False, "The location's city and country do not match.")
        if cleaned_data['coordinates']:
            line = LineString(cleaned_data['coordinates'], cleaned_data['city'].coordinates, srid=4326)
            line.transform(53031)  # transform to two-point equidistant project
            # if coordinate is more than 500km away from city
            if line.length > 500000:
                return (False, "The location's coordinates are more than 500km away from its city.")
        return (True, "")


class LocationAdmin(admin.ModelAdmin):
    form = LocationAdminForm


admin.site.register(Location, LocationAdmin)
admin.site.register(City)
admin.site.register(Country)
admin.site.register(Region)<commit_msg>make cities, regions, countries and locations searchable by name in admin interface<commit_after>from django.contrib import admin
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.geos import LineString

from atlas.models import Location, City, Country, Region
from atlas.utils import get_city


class LocationAdminForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(LocationAdminForm, self).__init__(*args, **kwargs)
        self.fields['country'].required = False
        self.fields['city'].required = False

    def clean(self, *args, **kwargs):
        cd = super(LocationAdminForm, self).clean(*args, **kwargs)
        if cd['city'] == 'no_city':
            del cd['city']

        # fill in the blanks on the form, if possible
        if cd.get('coordinates', None) and not cd.get('city', None):
            cd['city'] = get_city(position=cd['coordinates'])
        if cd.get('city', None):
            if not cd.get('country', None):
                cd['country'] = cd['city'].country
        else:
            self.errors.clear()
            raise forms.ValidationError("A city or coordinates are required.")

        # check that fields are consistent
        success, msg = self.check_consistency(cd)
        if not success:
            raise forms.ValidationError(msg)

        return cd

    def clean_city(self):
        if self.cleaned_data.get('city', None):
            return self.cleaned_data['city']
        return 'no_city'

    # checks that fields are geographically consistent
    def check_consistency(self, cleaned_data):
        if cleaned_data['city'].country != cleaned_data['country']:
            return (False, "The location's city and country do not match.")
        if cleaned_data['coordinates']:
            line = LineString(cleaned_data['coordinates'], cleaned_data['city'].coordinates, srid=4326)
            line.transform(53031)  # transform to two-point equidistant project
            # if coordinate is more than 500km away from city
            if line.length > 500000:
                return (False, "The location's coordinates are more than 500km away from its city.")
        return (True, "")


class SearchByNameAdmin(admin.ModelAdmin):
    search_fields = ['name']


class LocationAdmin(SearchByNameAdmin):
    form = LocationAdminForm


admin.site.register(Location, LocationAdmin)
admin.site.register(City, SearchByNameAdmin)
admin.site.register(Country, SearchByNameAdmin)
admin.site.register(Region, SearchByNameAdmin)
{'fraction_non_alphanumeric': 0.10814663951120163, 'fraction_numerical': 0.008553971486761711, 'mean_word_length': 2.9733009708737863, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5580655', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1302, 'n_words': 385}