diff --git a/arcgis/README.md b/arcgis/README.md deleted file mode 100644 index 2dd0126..0000000 --- a/arcgis/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Arcgis - free reverse geocoding - -This package is optional. - -If a Telegram user sends you his/her location, you may want to have more information than just coordinates (lat, lng). Arcgis is a free reverse geocoder which can give you more details about a location: City, Country, Region, ... Read `models.py` for more information. \ No newline at end of file diff --git a/arcgis/__init__.py b/arcgis/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/arcgis/admin.py b/arcgis/admin.py deleted file mode 100644 index 135173c..0000000 --- a/arcgis/admin.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.contrib import admin - -# Register your models here. -from arcgis.models import Arcgis - - -@admin.register(Arcgis) -class UserAdmin(admin.ModelAdmin): - list_display = ('location', 'city', 'country_code') - list_filter = ('country_code',) diff --git a/arcgis/apps.py b/arcgis/apps.py deleted file mode 100644 index 77bd163..0000000 --- a/arcgis/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class ArcgisConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'arcgis' diff --git a/arcgis/migrations/0001_initial.py b/arcgis/migrations/0001_initial.py deleted file mode 100644 index da45575..0000000 --- a/arcgis/migrations/0001_initial.py +++ /dev/null @@ -1,49 +0,0 @@ -# Generated by Django 3.2.6 on 2021-08-25 10:43 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ('users', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Arcgis', - fields=[ - ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)), - ('location', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='users.location')), - ('match_addr', models.CharField(max_length=200)), - ('long_label', models.CharField(max_length=200)), - ('short_label', models.CharField(max_length=128)), - ('addr_type', models.CharField(max_length=128)), - ('location_type', models.CharField(max_length=64)), - ('place_name', models.CharField(max_length=128)), - ('add_num', models.CharField(max_length=50)), - ('address', models.CharField(max_length=128)), - ('block', models.CharField(max_length=128)), - ('sector', models.CharField(max_length=128)), - ('neighborhood', models.CharField(max_length=128)), - ('district', models.CharField(max_length=128)), - ('city', models.CharField(max_length=64)), - ('metro_area', models.CharField(max_length=64)), - ('subregion', models.CharField(max_length=64)), - ('region', models.CharField(max_length=128)), - ('territory', models.CharField(max_length=128)), - ('postal', models.CharField(max_length=128)), - ('postal_ext', models.CharField(max_length=128)), - ('country_code', models.CharField(max_length=32)), - ('lng', models.DecimalField(decimal_places=18, max_digits=21)), - ('lat', models.DecimalField(decimal_places=18, max_digits=21)), - ], - options={ - 'ordering': ('-created_at',), - 'abstract': False, - }, - ), - ] diff --git a/arcgis/migrations/__init__.py b/arcgis/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/arcgis/models.py b/arcgis/models.py deleted file mode 100644 index 6548d06..0000000 --- a/arcgis/models.py +++ /dev/null @@ -1,95 +0,0 @@ -from typing import Dict - -import requests -from django.db import models - -# Create your models here. -from users.models import Location -from utils.models import CreateTracker, GetOrNoneManager - - -class Arcgis(CreateTracker): - location = models.OneToOneField(Location, on_delete=models.CASCADE, primary_key=True) - - match_addr = models.CharField(max_length=200) - long_label = models.CharField(max_length=200) - short_label = models.CharField(max_length=128) - - addr_type = models.CharField(max_length=128) - location_type = models.CharField(max_length=64) - place_name = models.CharField(max_length=128) - - add_num = models.CharField(max_length=50) - address = models.CharField(max_length=128) - block = models.CharField(max_length=128) - sector = models.CharField(max_length=128) - neighborhood = models.CharField(max_length=128) - district = models.CharField(max_length=128) - city = models.CharField(max_length=64) - metro_area = models.CharField(max_length=64) - subregion = models.CharField(max_length=64) - region = models.CharField(max_length=128) - territory = models.CharField(max_length=128) - postal = models.CharField(max_length=128) - postal_ext = models.CharField(max_length=128) - - country_code = models.CharField(max_length=32) - - lng = models.DecimalField(max_digits=21, decimal_places=18) - lat = models.DecimalField(max_digits=21, decimal_places=18) - - objects = GetOrNoneManager() - - def __str__(self): - return f"{self.location}, city: {self.city}, country_code: {self.country_code}" - - @classmethod - def from_json(cls, arcgis_json: Dict, location_id: int) -> None: - address, location = arcgis_json.get("address"), arcgis_json.get("location") - - if "address" not in arcgis_json or "location" not in arcgis_json: - return - - arcgis_data = { - "match_addr": address.get("Match_addr"), - "long_label": address.get("LongLabel"), - "short_label": address.get("ShortLabel"), - "addr_type": address.get("Addr_type"), - "location_type": address.get("Type"), - "place_name": address.get("PlaceName"), - "add_num": address.get("AddNum"), - "address": address.get("Address"), - "block": address.get("Block"), - "sector": address.get("Sector"), - "neighborhood": address.get("Neighborhood"), - "district": address.get("District"), - "city": address.get("City"), - "metro_area": address.get("MetroArea"), - "subregion": address.get("Subregion"), - "region": address.get("Region"), - "territory": address.get("Territory"), - "postal": address.get("Postal"), - "postal_ext": address.get("PostalExt"), - "country_code": address.get("CountryCode"), - "lng": location.get("x"), - "lat": location.get("y") - } - - arc, _ = cls.objects.update_or_create(location_id=location_id, defaults=arcgis_data) - return - - @staticmethod - def reverse_geocode(lat: float, lng: float) -> Dict: - r = requests.post( - "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode", - params={ - 'f': 'json', - 'location': f'{lng}, {lat}', - 'distance': 50000, - 'outSR': '', - }, - headers={ - 'Content-Type': 'application/json', - } - ) - return r.json() diff --git a/arcgis/tasks.py b/arcgis/tasks.py deleted file mode 100644 index 8354b35..0000000 --- a/arcgis/tasks.py +++ /dev/null @@ -1,7 +0,0 @@ -from arcgis.models import Arcgis -from dtb.celery import app - - -@app.task(ignore_result=True) -def save_data_from_arcgis(latitude: float, longitude: float, location_id: int) -> None: - Arcgis.from_json(Arcgis.reverse_geocode(latitude, longitude), location_id=location_id) diff --git a/dtb/settings.py b/dtb/settings.py index 6eb9e03..c03f5cd 100644 --- a/dtb/settings.py +++ b/dtb/settings.py @@ -45,7 +45,6 @@ # local apps 'users.apps.UsersConfig', - 'arcgis', ] MIDDLEWARE = [ diff --git a/users/models.py b/users/models.py index 5ba5a47..db60d48 100644 --- a/users/models.py +++ b/users/models.py @@ -7,7 +7,6 @@ from telegram import Update from telegram.ext import CallbackContext -from dtb.settings import DEBUG from tgbot.handlers.utils.info import extract_user_data_from_update from utils.models import CreateUpdateTracker, nb, CreateTracker, GetOrNoneManager @@ -84,12 +83,3 @@ class Location(CreateTracker): def __str__(self): return f"user: {self.user}, created at {self.created_at.strftime('(%H:%M, %d %B %Y)')}" - - def save(self, *args, **kwargs): - super(Location, self).save(*args, **kwargs) - # Parse location with arcgis - from arcgis.tasks import save_data_from_arcgis - if DEBUG: - save_data_from_arcgis(latitude=self.latitude, longitude=self.longitude, location_id=self.pk) - else: - save_data_from_arcgis.delay(latitude=self.latitude, longitude=self.longitude, location_id=self.pk)