Skip to content

Commit

Permalink
Merge pull request #142 from seanmorley15/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
seanmorley15 authored Jul 24, 2024
2 parents efa13d8 + 13590c5 commit c10aa87
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 32 deletions.
106 changes: 75 additions & 31 deletions backend/server/worldtravel/management/commands/worldtravel-seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from worldtravel.models import Country, Region
from django.db import transaction


class Command(BaseCommand):
help = 'Imports the world travel data'

def handle(self, *args, **kwargs):

# if the country or regions tables are not empty, do not insert data
if Country.objects.exists() or Region.objects.exists():
self.stdout.write(self.style.NOTICE(
'Countries or regions already exist in the database!'))
return
def add_arguments(self, parser):
parser.add_argument(
'-f', '--force',
action='store_true',
help='Force import even if data already exists'
)

def handle(self, *args, **options):
force = options['force']

countries = [
('United States', 'us', 'NA'),
('Canada', 'ca', 'NA'),
Expand All @@ -38,21 +41,6 @@ def handle(self, *args, **kwargs):
('Switzerland', 'ch', 'EU'),
('Italy', 'it', 'EU'),
]

for name, country_code, continent in countries:
country, created = Country.objects.get_or_create(
name=name,
country_code=country_code,
defaults={'continent': continent}
)
if created:
print(f'Inserted {name} into worldtravel countries')
else:
print(f'{name} already exists in worldtravel countries')

self.stdout.write(self.style.SUCCESS(
'Successfully inserted worldtravel countries!'
))

regions = [
('US-AL', 'Alabama', 'us'),
Expand Down Expand Up @@ -136,7 +124,7 @@ def handle(self, *args, **kwargs):
('DE-TH', 'Thüringen', 'de'),
('FR-ARA', 'Auvergne-Rhône-Alpes', 'fr'),
('FR-BFC', 'Bourgogne-Franche-Comté', 'fr'),
('FR-BRE', 'Brittany', 'fr'),
('FR-BRE', 'Bretagne', 'fr'),
('FR-CVL', 'Centre-Val de Loire', 'fr'),
('FR-GES', 'Grand Est', 'fr'),
('FR-HDF', 'Hauts-de-France', 'fr'),
Expand Down Expand Up @@ -501,17 +489,73 @@ def handle(self, *args, **kwargs):
('IT-34', 'Veneto', 'it'),
]

for code, name, country_code in regions:
if not force and (Country.objects.exists() or Region.objects.exists()):
self.stdout.write(self.style.WARNING(
'Countries or regions already exist in the database. Use --force to override.'
))
return

try:
with transaction.atomic():
if force:
self.sync_countries(countries)
self.sync_regions(regions)
else:
self.insert_countries(countries)
self.insert_regions(regions)

self.stdout.write(self.style.SUCCESS('Successfully imported world travel data'))
except Exception as e:
self.stdout.write(self.style.ERROR(f'Error importing data: {str(e)}'))

def sync_countries(self, countries):
country_codes = [code for _, code, _ in countries]
Country.objects.exclude(country_code__in=country_codes).delete()

for name, country_code, continent in countries:
country, created = Country.objects.update_or_create(
country_code=country_code,
defaults={'name': name, 'continent': continent}
)
if created:
self.stdout.write(f'Inserted {name} into worldtravel countries')
else:
self.stdout.write(f'Updated {name} in worldtravel countries')

def sync_regions(self, regions):
region_ids = [id for id, _, _ in regions]
Region.objects.exclude(id__in=region_ids).delete()

for id, name, country_code in regions:
country = Country.objects.get(country_code=country_code)
region, created = Region.objects.get_or_create(
id=code,
region, created = Region.objects.update_or_create(
id=id,
defaults={'name': name, 'country': country}
)
if created:
print(f'Inserted region: {name} ({code})')
self.stdout.write(f'Inserted {name} into worldtravel regions')
else:
self.stdout.write(f'Updated {name} in worldtravel regions')

def insert_countries(self, countries):
for name, country_code, continent in countries:
country, created = Country.objects.get_or_create(
country_code=country_code,
defaults={'name': name, 'continent': continent}
)
if created:
self.stdout.write(f'Inserted {name} into worldtravel countries')
else:
print(f'Region already exists: {name} ({code})')
self.stdout.write(f'{name} already exists in worldtravel countries')

self.stdout.write(self.style.SUCCESS(
'Successfully inserted worldtravel regions!'
))
def insert_regions(self, regions):
for id, name, country_code in regions:
country = Country.objects.get(country_code=country_code)
region, created = Region.objects.get_or_create(
id=id,
defaults={'name': name, 'country': country}
)
if created:
self.stdout.write(f'Inserted {name} into worldtravel regions')
else:
self.stdout.write(f'{name} already exists in worldtravel regions')
35 changes: 34 additions & 1 deletion frontend/src/lib/components/NewAdventure.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
export let type: string = 'visited';
export let longitude: number | null = null;
export let latitude: number | null = null;
import Wikipedia from '~icons/mdi/wikipedia';
import ClipboardList from '~icons/mdi/clipboard-list';
import ActivityComplete from './ActivityComplete.svelte';
Expand All @@ -31,6 +34,11 @@
collection: null
};
if (longitude && latitude) {
newAdventure.latitude = latitude;
newAdventure.longitude = longitude;
}
let image: File;
let fileInput: HTMLInputElement;
Expand Down Expand Up @@ -137,6 +145,8 @@
query={newAdventure.name}
on:close={() => (isPointModalOpen = false)}
on:submit={setLongLat}
latitude={newAdventure.latitude || null}
longitude={newAdventure.longitude || null}
/>
{/if}

Expand Down Expand Up @@ -164,6 +174,28 @@
on:submit={handleSubmit}
action="/adventures?/create"
>
<div class="join">
<input
class="join-item btn btn-neutral"
type="radio"
name="type"
id="visited"
value="visited"
aria-label="Visited"
checked
on:click={() => (type = 'visited')}
/>
<input
class="join-item btn btn-neutral"
type="radio"
name="type"
id="planned"
value="planned"
aria-label="Planned"
on:click={() => (type = 'planned')}
/>
</div>

<input
type="text"
name="type"
Expand Down Expand Up @@ -310,7 +342,8 @@
<button
type="button"
class="btn btn-secondary"
on:click={() => (isPointModalOpen = true)}>Define Location</button
on:click={() => (isPointModalOpen = true)}
>{newAdventure.latitude && newAdventure.longitude ? 'Change' : 'Select'} Location</button
>
</div>
<button type="submit" class="btn btn-primary mr-4 mt-4">Create</button>
Expand Down
45 changes: 45 additions & 0 deletions frontend/src/routes/map/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script>
// @ts-nocheck
import NewAdventure from '$lib/components/NewAdventure.svelte';
import {
DefaultMarker,
MapEvents,
Expand All @@ -16,6 +17,19 @@
let clickedName = '';
let newMarker = [];
let newLongitude = null;
let newLatitude = null;
function addMarker(e) {
newMarker = [];
newMarker = [...newMarker, { lngLat: e.detail.lngLat, name: 'Marker 1' }];
console.log(newMarker);
newLongitude = e.detail.lngLat.lng;
newLatitude = e.detail.lngLat.lat;
}
let markers = data.props.markers;
let visitedRegions = data.props.visitedRegions;
Expand All @@ -29,10 +43,37 @@
visitArray.push(el.region);
});
function clearMarkers() {
newMarker = [];
newLatitude = null;
newLongitude = null;
}
// mapped to the checkbox
let showGEO = true;
let createModalOpen = false;
</script>

{#if newMarker.length > 0}
<button type="button" class="btn btn-primary mb-2" on:click={() => (createModalOpen = true)}
>Add New Adventure at Marker</button
>
<button type="button" class="btn btn-neutral mb-2" on:click={clearMarkers}>Clear Marker</button>
{:else}
<button type="button" class="btn btn-primary mb-2" on:click={() => (createModalOpen = true)}
>Add New Adventure</button
>
{/if}

{#if createModalOpen}
<NewAdventure
on:close={() => (createModalOpen = false)}
longitude={newLongitude}
latitude={newLatitude}
/>
{/if}

<label for="show-geo">Show Borders?</label>
<input type="checkbox" id="shpw-gep" name="show-geo" class="checkbox" bind:checked={showGEO} />

Expand Down Expand Up @@ -109,6 +150,10 @@
/> -->
</GeoJSON>
{/if}
<MapEvents on:click={addMarker} />
{#each newMarker as marker}
<DefaultMarker lngLat={marker.lngLat} />
{/each}
</MapLibre>

<style>
Expand Down

0 comments on commit c10aa87

Please sign in to comment.