mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-24 23:39:37 +02:00
feat: Add location_name to ReverseGeocode type and implement location fetching in stats view
This commit is contained in:
parent
60b5bbb3c8
commit
b5d6788c11
21 changed files with 1048 additions and 901 deletions
|
@ -21,10 +21,14 @@ class ReverseGeocodeViewSet(viewsets.ViewSet):
|
|||
country_code = None
|
||||
city = None
|
||||
visited_city = None
|
||||
location_name = None
|
||||
|
||||
# town = None
|
||||
# city = None
|
||||
# county = None
|
||||
|
||||
if 'name' in data.keys():
|
||||
location_name = data['name']
|
||||
|
||||
if 'address' in data.keys():
|
||||
keys = data['address'].keys()
|
||||
|
@ -58,7 +62,7 @@ class ReverseGeocodeViewSet(viewsets.ViewSet):
|
|||
if visited_city:
|
||||
city_visited = True
|
||||
if region:
|
||||
return {"region_id": iso_code, "region": region.name, "country": region.country.name, "region_visited": region_visited, "display_name": display_name, "city": city.name if city else None, "city_id": city.id if city else None, "city_visited": city_visited}
|
||||
return {"region_id": iso_code, "region": region.name, "country": region.country.name, "region_visited": region_visited, "display_name": display_name, "city": city.name if city else None, "city_id": city.id if city else None, "city_visited": city_visited, 'location_name': location_name}
|
||||
return {"error": "No region found"}
|
||||
|
||||
@action(detail=False, methods=['get'])
|
||||
|
|
|
@ -48,4 +48,11 @@ class StatsViewSet(viewsets.ViewSet):
|
|||
'total_regions': total_regions,
|
||||
'visited_country_count': visited_country_count,
|
||||
'total_countries': total_countries
|
||||
})
|
||||
})
|
||||
|
||||
# locations - returns a list of all of the latitude, longitude locations of all public adventrues on the server
|
||||
@action(detail=False, methods=['get'], url_path='locations')
|
||||
def locations(self, request):
|
||||
adventures = Adventure.objects.filter(
|
||||
is_public=True).values('latitude', 'longitude', 'id', 'name')
|
||||
return Response(adventures)
|
|
@ -61,7 +61,7 @@ INSTALLED_APPS = (
|
|||
'users',
|
||||
'integrations',
|
||||
'django.contrib.gis',
|
||||
'achievements',
|
||||
# 'achievements', # Not done yet, will be added later in a future update
|
||||
# 'widget_tweaks',
|
||||
# 'slippers',
|
||||
|
||||
|
@ -303,4 +303,7 @@ LOGGING = {
|
|||
},
|
||||
}
|
||||
|
||||
ADVENTURELOG_CDN_URL = getenv('ADVENTURELOG_CDN_URL', 'https://cdn.adventurelog.app')
|
||||
# ADVENTURELOG_CDN_URL = getenv('ADVENTURELOG_CDN_URL', 'https://cdn.adventurelog.app')
|
||||
|
||||
# https://github.com/dr5hn/countries-states-cities-database/tags
|
||||
COUNTRY_REGION_JSON_VERSION = 'v2.5'
|
|
@ -68,6 +68,9 @@ class PublicUserListView(APIView):
|
|||
for user in users:
|
||||
user.email = None
|
||||
serializer = PublicUserSerializer(users, many=True)
|
||||
# for every user, remove the field has_password
|
||||
for user in serializer.data:
|
||||
user.pop('has_password', None)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
|
||||
class PublicUserDetailView(APIView):
|
||||
|
@ -87,6 +90,8 @@ class PublicUserDetailView(APIView):
|
|||
else:
|
||||
user = get_object_or_404(User, username=username, public_profile=True)
|
||||
serializer = PublicUserSerializer(user)
|
||||
# for every user, remove the field has_password
|
||||
serializer.data.pop('has_password', None)
|
||||
|
||||
# remove the email address from the response
|
||||
user.email = None
|
||||
|
|
|
@ -8,7 +8,7 @@ import ijson
|
|||
|
||||
from django.conf import settings
|
||||
|
||||
ADVENTURELOG_CDN_URL = settings.ADVENTURELOG_CDN_URL
|
||||
COUNTRY_REGION_JSON_VERSION = settings.COUNTRY_REGION_JSON_VERSION
|
||||
|
||||
media_root = settings.MEDIA_ROOT
|
||||
|
||||
|
@ -27,7 +27,7 @@ def saveCountryFlag(country_code):
|
|||
print(f'Flag for {country_code} already exists')
|
||||
return
|
||||
|
||||
res = requests.get(f'{ADVENTURELOG_CDN_URL}/data/flags/{country_code}.png'.lower())
|
||||
res = requests.get(f'https://flagcdn.com/h240/{country_code}.png'.lower())
|
||||
if res.status_code == 200:
|
||||
with open(flag_path, 'wb') as f:
|
||||
f.write(res.content)
|
||||
|
@ -39,56 +39,30 @@ class Command(BaseCommand):
|
|||
help = 'Imports the world travel data'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--force', action='store_true', help='Force re-download of AdventureLog setup content from the CDN')
|
||||
parser.add_argument('--force', action='store_true', help='Force download the countries+regions+states.json file')
|
||||
|
||||
def handle(self, **options):
|
||||
force = options['force']
|
||||
batch_size = 100
|
||||
current_version_json = os.path.join(settings.MEDIA_ROOT, 'data_version.json')
|
||||
try:
|
||||
cdn_version_json = requests.get(f'{ADVENTURELOG_CDN_URL}/data/version.json')
|
||||
cdn_version_json.raise_for_status()
|
||||
cdn_version = cdn_version_json.json().get('version')
|
||||
if os.path.exists(current_version_json):
|
||||
with open(current_version_json, 'r') as f:
|
||||
local_version = f.read().strip()
|
||||
self.stdout.write(self.style.SUCCESS(f'Local version: {local_version}'))
|
||||
countries_json_path = os.path.join(settings.MEDIA_ROOT, f'countries+regions+states-{COUNTRY_REGION_JSON_VERSION}.json')
|
||||
if not os.path.exists(countries_json_path) or force:
|
||||
res = requests.get(f'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/{COUNTRY_REGION_JSON_VERSION}/json/countries%2Bstates%2Bcities.json')
|
||||
if res.status_code == 200:
|
||||
with open(countries_json_path, 'w') as f:
|
||||
f.write(res.text)
|
||||
self.stdout.write(self.style.SUCCESS('countries+regions+states.json downloaded successfully'))
|
||||
else:
|
||||
local_version = None
|
||||
|
||||
if force or local_version != cdn_version:
|
||||
with open(current_version_json, 'w') as f:
|
||||
f.write(cdn_version)
|
||||
self.stdout.write(self.style.SUCCESS('Version updated successfully to ' + cdn_version))
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS('Data is already up-to-date. Run with --force to re-download'))
|
||||
self.stdout.write(self.style.ERROR('Error downloading countries+regions+states.json'))
|
||||
return
|
||||
except requests.RequestException as e:
|
||||
self.stdout.write(self.style.ERROR(f'Error fetching version from the CDN: {e}, skipping data import. Try restarting the container once CDN connection has been restored.'))
|
||||
elif not os.path.isfile(countries_json_path):
|
||||
self.stdout.write(self.style.ERROR('countries+regions+states.json is not a file'))
|
||||
return
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Fetching latest data from the AdventureLog CDN located at: ' + ADVENTURELOG_CDN_URL))
|
||||
|
||||
# Delete the existing flags
|
||||
flags_dir = os.path.join(media_root, 'flags')
|
||||
if os.path.exists(flags_dir):
|
||||
for file in os.listdir(flags_dir):
|
||||
os.remove(os.path.join(flags_dir, file))
|
||||
|
||||
# Delete the existing countries, regions, and cities json files
|
||||
countries_json_path = os.path.join(media_root, 'countries_states_cities.json')
|
||||
if os.path.exists(countries_json_path):
|
||||
os.remove(countries_json_path)
|
||||
self.stdout.write(self.style.SUCCESS('countries_states_cities.json deleted successfully'))
|
||||
|
||||
# Download the latest countries, regions, and cities json file
|
||||
res = requests.get(f'{ADVENTURELOG_CDN_URL}/data/countries_states_cities.json')
|
||||
if res.status_code == 200:
|
||||
with open(countries_json_path, 'w') as f:
|
||||
f.write(res.text)
|
||||
self.stdout.write(self.style.SUCCESS('countries_states_cities.json downloaded successfully'))
|
||||
elif os.path.getsize(countries_json_path) == 0:
|
||||
self.stdout.write(self.style.ERROR('countries+regions+states.json is empty'))
|
||||
elif Country.objects.count() == 0 or Region.objects.count() == 0 or City.objects.count() == 0:
|
||||
self.stdout.write(self.style.WARNING('Some region data is missing. Re-importing all data.'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR('Error downloading countries_states_cities.json'))
|
||||
self.stdout.write(self.style.SUCCESS('Latest country, region, and state data already downloaded.'))
|
||||
return
|
||||
|
||||
with open(countries_json_path, 'r') as f:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue