This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import path | |
from django.conf.urls import include | |
from django.conf import settings | |
from django.contrib import admin | |
from rest_framework_swagger.views import get_swagger_view | |
from .views import api_root | |
schema_view = get_swagger_view(title='DRF by Example') | |
# url list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .models import Location | |
from .serializers import LocationSerializer | |
from rest_framework.response import Response | |
from rest_framework import generics, permissions | |
from rest_framework import status | |
from rest_framework.decorators import api_view, permission_classes | |
from rest_framework.permissions import IsAuthenticated | |
import GeoIP | |
import ipaddress |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from .models import Location | |
# Register your models here. | |
class LocationAdmin(admin.ModelAdmin): | |
list_display = ['ip_addr', 'region_name', 'country_name', 'time_zone', 'latitude', 'longitude'] | |
search_fields = ['ip_addr', 'region_name', 'latitude', 'longitude'] | |
list_filter = ['region_name', 'time_zone'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import path | |
from rest_framework.urlpatterns import format_suffix_patterns | |
from . import views | |
urlpatterns = [ | |
path('', views.LocationList.as_view(), name='location-list'), | |
path('<int:pk>/', views.LocationDetail.as_view()), | |
path('geolocate/<str:ip_addr>', views.get_location, name='geolocate') | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from .models import Location | |
class LocationSerializer(serializers.Serializer): | |
id = serializers.ReadOnlyField() | |
ip_addr = serializers.CharField(required=True, allow_blank=False, max_length=15) | |
time_zone = serializers.CharField(required=True, allow_blank=False) | |
latitude = serializers.FloatField(required=True) | |
longitude = serializers.FloatField(required=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
# Create your models here. | |
class Location(models.Model): | |
""" | |
The Location data model for geo-coding | |
""" | |
ip_addr = models.CharField(max_length=15, null=False, blank=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!.env/bin/python | |
# -*- coding: utf-8 -*- | |
import csv | |
from db import db_session | |
from sqlalchemy import exc | |
from models import IPData | |
from datetime import datetime | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!.env/bin/python | |
# -*- code: utf-8 -*- | |
import sys | |
import csv | |
import config | |
import MySQLdb | |
import logging | |
log_format = '%(asctime)s %(levelname)s:%(message)s' | |
logging.basicConfig( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!.env/bin/python | |
# -*- coding:utf-8 -*- | |
import config | |
import logging | |
import os | |
import csv | |
import sys | |
import MySQLdb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import responder | |
import random | |
import logging | |
import uuid | |
from datetime import datetime | |
from marshmallow import Schema, fields | |
api = responder.API( | |
title="ThingsAPI", |