Giter VIP home page Giter VIP logo

django-geoip's Introduction

Django GeoIP

image

image

A Django app to figure out where your visitors are using their IP address.

It detects country, region and city, querying the database with geodata. Optional high-level API provides user location in request object.

Requires Django 1.10+, supports python 2.7, 3.4, 3.5.

Docs: http://django-geoip.readthedocs.org/

django-geoip's People

Contributors

akolpakov avatar artofhuman avatar blag avatar coagulant avatar daniilr avatar ezyatev avatar ikeyurp avatar ishumihin avatar mryoda avatar olevinsky avatar st4lk avatar un1t avatar z0rr0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-geoip's Issues

Duplicate regions!

In
django-geoip / django_geoip / management / ipgeobase.py

136 Region.objects.get(name=entry['region__name'])

it can be more than 1 region with this name!

(Mayby its ipgeobase.ru problem)

Time_zone info

Hey guys. I think that it would be great to get info about time_zone using your application. May be you have some plans about it? Thanks!

Migration error in Django 1.7 / Ошибка при выполнении миграций в Django 1.7

Здравствуйте.

Столкнулся с проблемой при использовании вашего модуля в своем приложении.
При выполнении инициализирующей миграции вылетает следующая ошибка:
"AttributeError: 'module' object has no attribute 'NewBase'".

Находил на github'е обсуждение аналогичной проблемы (#41), но решение мне не помогло.

Django 1.7.1
Django-geoip 0.5.1

Полный текст ошибки:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 63, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
    self.build_graph()
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/db/migrations/loader.py", line 173, in build_graph
    self.load_disk()
  File "/Users/admin/Sites/django/pruma/.venv/lib/python2.7/site-packages/django/db/migrations/loader.py", line 103, in load_disk
    migration_module = import_module("%s.%s" % (module_name, migration_name))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Sites/django/pruma/pruma/migrations/0001_initial.py", line 11, in <module>
    class Migration(migrations.Migration):
  File "/Users/admin/Sites/django/pruma/pruma/migrations/0001_initial.py", line 72, in Migration
    bases=(django.db.models.base.NewBase, models.Model),
AttributeError: 'module' object has no attribute 'NewBase'

Все установленные приложения

Django==1.7.1
MySQL-python==1.2.5
Pillow==2.6.1
Unidecode==0.04.16
django-ajax-selects==1.3.5
django-appconf==0.6
django-ckeditor==4.4.6
django-compressor==1.4
django-extensions==1.4.6
django-geoip==0.5.1
django-haystack==2.3.1
django-mptt==0.6.1
django-sphinx==4.0.1
django-ulogin==0.2
mock==1.0.1
psycopg2==2.5.4
requests==2.4.3
six==1.8.0
sphinxit==0.3.2
wsgiref==0.1.2
xapian-haystack==2.0.0

Файл миграции

...
        migrations.CreateModel(
            name='Location',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('is_default', models.BooleanField(default=False, help_text='\u0411\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0435\u0441\u043b\u0438 \u0433\u043e\u0440\u043e\u0434 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d', verbose_name='\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e')),
                ('is_active', models.BooleanField(default=True, verbose_name='\u0410\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c')),
                ('name', models.CharField(help_text='\u0415\u043a\u0430\u0442\u0435\u0440\u0438\u043d\u0431\u0443\u0440\u0433, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0441\u043a\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0438 \u0442.\u0434.', unique=True, max_length=128, verbose_name='\u0418\u043c\u044f \u043b\u043e\u043a\u0430\u0446\u0438\u0438')),
                ('phone', models.CharField(default='', max_length=64, verbose_name='\u0422\u0435\u043b\u0435\u0444\u043e\u043d', blank=True)),
                ('email', models.EmailField(default='', max_length=64, verbose_name='Email', blank=True)),
                ('footer_contacts', models.TextField(verbose_name='\u041a\u043e\u043d\u0442\u0430\u043a\u0442\u044b \u0432 \u0444\u0443\u0442\u0435\u0440\u0435', blank=True)),
                ('city', models.ManyToManyField(related_name='location_city', verbose_name='\u041f\u0440\u0438\u0432\u044f\u0437\u043a\u0430 \u043a \u0433\u043e\u0440\u043e\u0434\u0430\u043c', to='django_geoip.City')),
                ('region', models.ManyToManyField(related_name='location_region', verbose_name='\u041f\u0440\u0438\u0432\u044f\u0437\u043a\u0430 \u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u043c', to='django_geoip.Region')),
                ('site', models.ForeignKey(verbose_name='\u0421\u0430\u0439\u0442', to='sites.Site', help_text='\u0421\u0430\u0439\u0442, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u0442\u043d\u0435\u0441\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u0430\u044f \u043b\u043e\u043a\u0430\u0446\u0438\u044f')),
            ],
            options={
                'verbose_name': '\u041b\u043e\u043a\u0430\u0446\u0438\u044f',
                'verbose_name_plural': '\u041b\u043e\u043a\u0430\u0446\u0438\u0438',
            },
            bases=(django.db.models.base.NewBase, models.Model),
        ),
...

Ошибка при выполнении geoip_update / Error with run geoip_update command

При выполнении обновления регионов (первичного), если не определять настройку IPGEOBASE_ALLOWED_COUNTRIES, вылетает следующая ошибка:

Downloading zipfile from ipgeobase.ru...
Extracting files...
Updating locations...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django_geoip/management/commands/geoip_update.py", line 40, in handle
    backend.sync_database()
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django_geoip/management/ipgeobase.py", line 46, in sync_database
    city_info['cities'])
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django_geoip/management/ipgeobase.py", line 136, in _update_geography
    region = Region.objects.get(name=entry['region__name'])
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/admin/Sites/django/flowers/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 361, in get
    num if num <= MAX_GET_RESULTS else 'more than %s' % MAX_GET_RESULTS
django_geoip.models.MultipleObjectsReturned: get() returned more than one Region -- it returned 2!

Если указать страну (IPGEOBASE_ALLOWED_COUNTRIES = ['RU']), то все отрабатывает без ошибок. Видимо есть одинаковые названия регионов.

На всякий случай прилагаю виртуальное окружение

DAWG-Python==0.7.1
Django==1.7.4
MySQL-python==1.2.5
Pillow==2.7.0
Unidecode==0.04.17
django-appconf==0.6
django-debug-toolbar==1.2.2
django-geoip==0.5.1
django-grappelli==2.6.3
docopt==0.6.2
easy-thumbnails==2.2
gunicorn==19.2.0
pymorphy2==0.8
pymorphy2-dicts==2.4.393442.3710985
python-slugify==0.1.0
requests==2.5.1
six==1.9.0
sqlparse==0.1.14
wsgiref==0.1.2

Не работает management-команда `geoip_update`

При попытке выполнить команду python manage.py geoip_update возникает ошибка:

Downloading zipfile from ipgeobase.ru...
Extracting files...
Updating locations...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/webapp/futurecolors.ru/env/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django_geoip/management/commands/geoip_update.py", line 40, in handle
    backend.sync_database()
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django_geoip/management/ipgeobase.py", line 46, in sync_database
    city_info['cities'])
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django_geoip/management/ipgeobase.py", line 131, in _update_geography
    region = Region.objects.get(name=entry['region__name'])
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/vehsyrabayli/futurecolors.ru/env/lib/python3.4/site-packages/django/db/models/query.py", line 361, in get
    num if num <= MAX_GET_RESULTS else 'more than %s' % MAX_GET_RESULTS
django_geoip.models.MultipleObjectsReturned: get() returned more than one Region -- it returned 2!

БД пустая, в таблице django_geoip_region в результате неполного выполнения команды появляются регионы с одинаковыми именами, но в разных странах:

 id  | country_id |         name         
-----+------------+----------------------
 244 | UA         | Закарпатская область
 257 | RU         | Закарпатская область
 232 | UA         | Киевская область
 256 | RU         | Киевская область
 246 | UA         | Харьковская область
 258 | RU         | Харьковская область
(6 rows)

Ошибка в Django 1.7.2 python manage.py geoip_update

'''
$ python manage.py geoip_update
Downloading zipfile from ipgeobase.ru...
Extracting files...
Updating locations...
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django/core/management/init.py", line 385, in execute_from_command_line
utility.execute()
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django/core/management/init.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(_args, *_options.dict)
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(_args, *_options)
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django_geoip/management/commands/geoip_update.py", line 40, in handle
backend.sync_database()
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django_geoip/management/ipgeobase.py", line 46, in sync_database
city_info['cities'])
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django_geoip/management/ipgeobase.py", line 136, in _update_geography
region = Region.objects.get(name=entry['region__name'])
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(_args, *_kwargs)
File "/home/beatum/charm/quickproject/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 361, in get
num if num <= MAX_GET_RESULTS else 'more than %s' % MAX_GET_RESULTS
django_geoip.models.MultipleObjectsReturned: get() returned more than one Region -- it returned 2
'''

get_by_ip_range example returns RelatedManager

In this example get_by_ip_range returns RelatedManager, while it should be MyCustomLocation, because thus we can't just write {{ request.location }} in templates.

Is it mistake in documentation or I have error in code?

Improve docs

Documentation is far from perfect

  • Add decent project description ✓
  • Add decent README ✓
  • Add django-hosts integration section ✓
  • Add reference page / merge with settings (later needs improvement)
  • Rewrite How it works page
  • Document Locator and location_model ✓

"psycopg2.DatabaseError: SSL SYSCALL error: EOF detected" when running geoip_update

Followed the installation instructions, migrated the PostgreSQL 9.3 database (wasn't causing any problems previously), but running python manage.py geoip_update repeatedly results into:

Downloading zipfile from ipgeobase.ru...
Extracting files...
Updating locations...
Updating CIDR...
100% |#########################################################################|
Traceback (most recent call last):
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.DatabaseError: SSL SYSCALL error: EOF detected


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    execute_from_command_line(sys.argv)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django_geoip/management/commands/geoip_update.py", line 40, in handle
    backend.sync_database()
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django_geoip/management/ipgeobase.py", line 49, in sync_database
    self._update_cidr(cidr_info)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django_geoip/management/ipgeobase.py", line 163, in _update_cidr
    IpRange.objects.bulk_create(new_ip_ranges)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/models/query.py", line 392, in bulk_create
    self._batched_insert(objs_without_pk, fields, batch_size)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/models/query.py", line 937, in _batched_insert
    using=self.db)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/models/query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 963, in execute_sql
    cursor.execute(sql, params)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/home/nazariyg/proj/proj_venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.DatabaseError: SSL SYSCALL error: EOF detected

coverage==3.7.1
Django==1.8
django-appconf==1.0.1
django-braces==1.8.1
django-debug-toolbar==1.3.2
django-extensions==1.5.6
django-geoip==0.5.2
django-haystack==2.4.0
django-model-utils==2.3.1
djangorestframework==3.2.3
Pillow==2.9.0
psycopg2==2.6.1
pysolr==3.3.2
python3-memcached==1.51
pytz==2015.4
regex==2015.7.19
requests==2.7.0
six==1.9.0
sqlparse==0.1.16
uWSGI==2.0.11.1
wheel==0.24.0

AttributeError: type object 'BaseCommand' has no attribute 'option_list' error

I get error:

File "/usr/local/lib/python3.4/dist-packages/django_geoip/management/commands/geoip_update.py", line 9, in <module>
    class Command(BaseCommand):
  File "/usr/local/lib/python3.4/dist-packages/django_geoip/management/commands/geoip_update.py", line 12, in Command
    option_list = BaseCommand.option_list + (
AttributeError: type object 'BaseCommand' has no attribute 'option_list'

when trying to
python3 manage.py geoip_update

Python3 issue

Hello

I followed the steps using Django 2.2 and python 3.7 (i read it was not compatible but still wanted to give it a try),

And this is the herror that comes up:

TypeError: __init__() missing 1 required positional argument: 'on_delete'

After adding django-geoip with pip and adding it to INSTALLED_APPS at settings file.
I made a python manage.py migrate and this is what is thrown:

Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/Users/bobpiter/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute django.setup() File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate app_config.import_models() File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models self.models_module = import_module(models_module_name) File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django_geoip/models.py", line 33, in <module> class Region(models.Model): File "/Users/bob/.virtualenvs/geo-vinyl/lib/python3.7/site-packages/django_geoip/models.py", line 38, in Region country = models.ForeignKey(Country, related_name='regions') TypeError: __init__() missing 1 required positional argument: 'on_delete'

I dont know it it is in your plans to make it py3.7 compatible, thanks.

IpRange.objects.by_ip(ip) generate sequential scans

I'm using django-geoip==0.3 in production and notice strange behavior of PostgreSQL 9.1, when executing queries:

SELECT relname, idx_scan, seq_scan, 100 * idx_scan / (seq_scan + idx_scan) "index_used, %", n_live_tup rows_in_table FROM pg_stat_user_tables WHERE seq_scan + idx_scan > 0 ORDER BY n_live_tup DESC;
             relname              | idx_scan | seq_scan | index_used, % | rows_in_table 
----------------------------------+----------+----------+---------------+---------------
 django_geoip_iprange             |  5323362 |  4830742 |            52 |        160320
 django_geoip_city                |  8258720 |      185 |            99 |           990
 django_geoip_country             |   164658 |  2780330 |             5 |           252
 django_geoip_region              |    44712 |   749790 |             5 |           110

52% of queries generates sequential scans, this leads to long execution time. Currently, I have 15ms in average for those types of queries which is way too long.

I tinkered a bit with you code and reduced the problem to this Stackoverflow question. Do you have thoughts about this issue?

lat / lng перепутаны или я запутался в координатной системе?

После geoip_update в базе данных видим следующие значения для Кирова:

latitude 49.662659
longitude 58.596081

Далее идем в http://itouchmap.com/latlong.html ставим точку по этим координатам http://d.pr/i/HLpP и получаем Казахстан.

Меняем местами значения у latitude и longitude и получаем Киров.
http://d.pr/i/xpJ

Что я делаю не правильно или что я не знаю о координатных системах? Пожалуйста подскажите.

"City object" instead of City.name - need __str__ methods in models

Please, create __str__ methods for py3.4 django 1.7 compatibility.
Example:

class City(models.Model):
    """ Geopoint that belongs to the Region and Country.
        Identified by name and region.
        Contains additional latitude/longitude info.
    """
    region = models.ForeignKey(Region, related_name='cities')
    name = models.CharField(_('city name'), max_length=255)
    latitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, null=True)
    longitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, null=True)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('city')
        verbose_name_plural = _('cities')
        unique_together = (('region', 'name'), )

Bulk insert of IpRange leads to out of memory

On the server with small amount of Memory, manage.py geoip_update leads to out of memory issue. Because of too big bulk insert (220 000 records)

Fix could be portional bulk insert. Also it will improve progress report in manage.py geoip_update

timestamp out of range for platform time_t

169======================================================================
170ERROR: test_do_set (django_geoip.tests.test_storage.LocationCookieStorageTest)
171----------------------------------------------------------------------
172Traceback (most recent call last):
173  File "/home/vagrant/virtualenv/python2.6/lib/python2.6/site-packages/mock.py", line 1224, in patched
174    return func(*args, **keywargs)
175  File "/home/vagrant/builds/coagulant/django-geoip/django_geoip/tests/test_storage.py", line 64, in test_do_set
176    storage._do_set(10)
177  File "/home/vagrant/builds/coagulant/django-geoip/django_geoip/storage.py", line 87, in _do_set
178    expires=datetime.now() + timedelta(seconds=settings.GEOIP_COOKIE_EXPIRES))
179  File "/home/vagrant/virtualenv/python2.6/lib/python2.6/site-packages/django/http/__init__.py", line 584, in set_cookie
180    max_age)
181  File "/home/vagrant/virtualenv/python2.6/lib/python2.6/site-packages/django/utils/http.py", line 70, in cookie_date
182    rfcdate = formatdate(epoch_seconds)
183  File "/usr/lib/python2.6/email/utils.py", line 160, in formatdate
184    now = time.gmtime(timeval)
185ValueError: timestamp out of range for platform time_t

geoip_update management command failed in django 1.11.8

error: type object 'BaseCommand' has no attribute 'option_list'
it looks like option_list is deprecated in django

https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/#django.core.management.BaseCommand.option_list

File "/var/www/venv/local/lib/python2.7/site-packages/django_geoip/management/commands/geoip_update.py", line 12, in Command option_list = BaseCommand.option_list + ( AttributeError: type object 'BaseCommand' has no attribute 'option_list'

Double-check that geography tables are not empty

TypeError at /forms/
int() argument must be a string or a number, not 'SimpleLazyObject'

Traceback:
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  136.                     response = response.render()
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/response.py" in render
  97.             self._set_content(self.rendered_content)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/response.py" in rendered_content
  75.         content = template.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  159.         return self.render_template(self.template, context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/loader_tags.py" in render_template
  141.         output = template.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in render
  797.         return _render_value_in_context(output, context)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/template/base.py" in _render_value_in_context
  777.     value = force_unicode(value)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/utils/encoding.py" in force_unicode
  71.                 s = unicode(s)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/forms/forms.py" in __unicode__
  408.         return self.as_widget()
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/forms/forms.py" in as_widget
  439.         return widget.render(name, self.value(), attrs=attrs)
File "/home/futurecolors/src/fut_testing/apps/geo/widgets.py" in render
  13.                    'country_id': self._get_country_id_of_current_value(value),
File "/home/futurecolors/src/fut_testing/apps/geo/widgets.py" in _get_country_id_of_current_value
  23.             return Location.objects.get(id=value).country.id
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/query.py" in get
  341.         clone = self.filter(*args, **kwargs)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/query.py" in filter
  550.         return self._filter_or_exclude(False, *args, **kwargs)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/query.py" in _filter_or_exclude
  568.             clone.query.add_q(Q(*args, **kwargs))
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/sql/query.py" in add_q
  1 194.                             can_reuse=used_aliases, force_having=force_having)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/sql/query.py" in add_filter
  1 129.                 connector)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/sql/where.py" in add
  67.             value = obj.prepare(lookup_type, value)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/sql/where.py" in prepare
  316.             return self.field.get_prep_lookup(lookup_type, value)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  292.             return self.get_prep_value(value)
File "/home/futurecolors/envs/fut_testing/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  479.         return int(value)

Exception Type: TypeError at /forms/
Exception Value: int() argument must be a string or a number, not 'SimpleLazyObject'```

When Location table is empty forms act unexpected

progressbar==2.3dev is not on PyPI

Downloading/unpacking progressbar==2.3dev (from django-geoip==0.3.1->-r requirements.txt (line 76))
  Could not find a version that satisfies the requirement progressbar==2.3dev (from django-geoip==0.3.1->-r requirements.txt (line 76)) (from versions: 2.1, 2.2)
  Some externally hosted files were ignored (use --allow-external to allow).
Cleaning up...
No distributions matching the version for progressbar==2.3dev (from django-geoip==0.3.1->-r requirements.txt (line 76))
Storing debug log for failure in /Users/antonagestam/.pip/pip.log

Error on 'geoip_update'

Хотел переехать на свежую версию с 0.4, и поймал такую вот ошибку, даже предположить не могу, из-за чего могло возникнуть. В сентябре 2014го было все нормально, тогда я последний раз обновлял БД ipgeobase.
VirtualBox vagrant instance (1core, RAM 369mb, SWAP 461mb), py3.4, django 1.7, django-geoip 0.5.2, PosgtreSQL 9.4

vagrant@debian8:/vagrant/src$ python3 manage.py geoip_update
Downloading zipfile from ipgeobase.ru...
INFO Downloading zipfile from ipgeobase.ru...
DEBUG Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
INFO Starting new HTTP connection (1): ipgeobase.ru
DEBUG "GET /files/db/Main/geo_files.zip HTTP/1.1" 200 2313639
Extracting files...
INFO Extracting files...
Updating locations...
INFO Updating locations...
Updating CIDR...
INFO Updating CIDR...
100% |##########################################################################################################################################################################################################|
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.DatabaseError: SSL SYSCALL error: EOF detected


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django_geoip/management/commands/geoip_update.py", line 40, in handle
    backend.sync_database()
  File "/usr/local/lib/python3.4/dist-packages/django_geoip/management/ipgeobase.py", line 49, in sync_database
    self._update_cidr(cidr_info)
  File "/usr/local/lib/python3.4/dist-packages/django_geoip/management/ipgeobase.py", line 163, in _update_cidr
    IpRange.objects.bulk_create(new_ip_ranges)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 409, in bulk_create
    self._batched_insert(objs_without_pk, fields, batch_size)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 938, in _batched_insert
    using=self.db)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 920, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 549, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.DatabaseError: SSL SYSCALL error: EOF detected

Error at CustomLocation class

Trace log:

  File "/path/.virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 223, in get_response
    response = middleware_method(request, response)
  File "/path/.virtualenv/lib/python2.7/site-packages/django_geoip/middleware.py", line 24, in process_response
    storage = storage_class(request=request, response=response)
  File "/path/.virtualenv/lib/python2.7/site-packages/django_geoip/storage.py", line 16, in __init__
    self.location_model = get_class(settings.GEOIP_LOCATION_MODEL)
  File "/path/.virtualenv/lib/python2.7/site-packages/django_geoip/utils.py", line 10, in get_class
    cls = getattr(__import__(mod_name, {}, {}, ['']), class_name)
  File "/path/src/apps/city/models.py", line 37, in <module>
    class CustomLocation(GeoLocationFacade):
  File "/path/.virtualenv/lib/python2.7/abc.py", line 87, in __new__
    cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
  File "/path/.virtualenv/lib/python2.7/site-packages/django/db/models/base.py", line 309, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/path/.virtualenv/lib/python2.7/site-packages/django/apps/registry.py", line 221, in register_model
    (model_name, app_label, app_models[model_name], model))
RuntimeError: Conflicting 'customlocation' models in application 'city': <class 'apps.city.models.CustomLocation'> and <class 'city.models.CustomLocation'>.

CustomLocation class:

class CustomLocation(GeoLocationFacade):
    name = models.CharField(max_length=100)
    is_default = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

    @classmethod
    def get_by_ip_range(cls, ip_range):
        return ip_range

    @classmethod
    def get_default_location(cls):
        return cls.objects.get(is_default=True)

    @classmethod
    def get_available_locations(cls):
        return cls.objects.all()

Run tests

В документации сейчас не правильная команда для запуска тестов, как я понял из конфига для travis - надо запускать:

$ coverage run --source=django_geoip manage.py test

?

Migration problems in Django 1.7

There is a problem during migrations MyCustomLocation model in Django 1.7 (RC2). I used embedded migration possibilities.

Following commands return successful result after a preparing of migration directory:

python manager.py makemigrations django_geoip
python manager.py migrate django_geoip

But there is a problem after a creation new model for custom location in customapp, makemigrations command returns errors:

python manager.py makemigrations customapp
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
...
 File "/home/user/customapp/migrations/0006_customlocation.py", line 8, in <module>
    class Migration(migrations.Migration):
  File "/home/user/customapp/migrations/0006_customlocation.py", line 27, in Migration
    bases=(django.db.models.base.NewBase, models.Model),
AttributeError: 'module' object has no attribute 'NewBase'

I'm ready to prepare more details if it's needed. Can it be related with a new migration mechanism in Django 1.7?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.