Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary user field from geotag #1820

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def setUp(self):
self.sound.moderation_state = "OK"
self.sound.processing_state = "OK"
self.sound.similarity_state = "OK"
self.sound.geotag = GeoTag.objects.create(user=user, lat=45.8498, lon=-62.6879, zoom=9)
GeoTag.objects.create(
sound=self.sound,
lat=45.8498,
lon=-62.6879,
zoom=9
)
self.sound.save()
SoundOfTheDay.objects.create(sound=self.sound, date_display=datetime.date.today())
self.download = Download.objects.create(user=self.user, sound=self.sound, license=self.sound.license,
Expand Down
2 changes: 1 addition & 1 deletion apiv2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def get_comments(self, obj):

geotag = serializers.SerializerMethodField()
def get_geotag(self, obj):
if obj.geotag:
if hasattr(obj, 'geotag'):
return str(obj.geotag.lat) + " " + str(obj.geotag.lon)
else:
return None
Expand Down
4 changes: 2 additions & 2 deletions apiv2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def test_oauth2_authorization_code_grant_flow(self):
}, secure=True)
self.assertEqual(resp.status_code, 302)
resp = self.client.get(resp.request['PATH_INFO'] + '?' + resp.request['QUERY_STRING'], secure=True)
self.assertEquals(resp.url.startswith(client.get_default_redirect_uri()), True)
self.assertEqual(resp.url.startswith(client.get_default_redirect_uri()), True)
resp_params = self.get_params_from_url(resp.url)
self.check_dict_has_fields(resp_params, ['error'])
self.assertEqual(resp_params['error'], 'unauthorized_client')
Expand Down Expand Up @@ -674,7 +674,7 @@ def test_oauth2_authorization_code_grant_flow(self):
}, secure=True)
self.assertTrue(resp.url.startswith(client.get_default_redirect_uri()))
resp_params = self.get_params_from_url(resp.url)
self.assertEquals(resp_params['state'], 'an_optional_state') # Check state is returned and preserved
self.assertEqual(resp_params['state'], 'an_optional_state') # Check state is returned and preserved
self.check_dict_has_fields(resp_params, ['code']) # Check code is there

# Return 200 OK when requesting access token setting client_id and client_secret in body params
Expand Down
5 changes: 2 additions & 3 deletions apiv2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,11 @@ def post(self, request, *args, **kwargs):
if 'geotag' in serializer.data:
if serializer.data['geotag']:
lat, lon, zoom = serializer.data['geotag'].split(',')
geotag = GeoTag(user=self.user,
geotag = GeoTag.objects.create(
sound=sound,
lat=float(lat),
lon=float(lon),
zoom=int(zoom))
geotag.save()
sound.geotag = geotag
if 'pack' in serializer.data:
if serializer.data['pack']:
if Pack.objects.filter(name=serializer.data['pack'], user=self.user)\
Expand Down
17 changes: 17 additions & 0 deletions geotags/migrations/0006_remove_geotag_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.23 on 2025-01-30 20:10

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('geotags', '0005_alter_geotag_information'),
]

operations = [
migrations.RemoveField(
model_name='geotag',
name='user',
),
]
20 changes: 20 additions & 0 deletions geotags/migrations/0007_geotag_sound2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.23 on 2025-02-04 10:58

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('sounds', '0052_alter_sound_type'),
('geotags', '0006_remove_geotag_user'),
]

operations = [
migrations.AddField(
model_name='geotag',
name='sound2',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='geotags2', to='sounds.sound'),
),
]
31 changes: 31 additions & 0 deletions geotags/migrations/0008_geotag_migrate_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This needs to be run in a separate transaction due to indexes

from django.db import migrations


def migrate_geotags(apps, schema_editor):
GeoTag = apps.get_model('geotags', 'GeoTag')
Sound = apps.get_model('sounds', 'Sound')
to_update = []
to_delete = []
for geotag in GeoTag.objects.prefetch_related('sound_set').all():
sounds = geotag.sound_set.all()
if len(sounds) == 1:
geotag.sound2_id = sounds.first().id
to_update.append(geotag)
else:
to_delete.append(geotag)
GeoTag.objects.bulk_update(to_update, ['sound2'])
print(f"Deleting {len(to_delete)} geotags with 0 sounds")
GeoTag.objects.filter(id__in=[gt.id for gt in to_delete]).delete()


class Migration(migrations.Migration):

dependencies = [
('geotags', '0007_geotag_sound2'),
]

operations = [
migrations.RunPython(migrate_geotags),
]
25 changes: 25 additions & 0 deletions geotags/migrations/0009_rename_sound2_geotag_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.23 on 2025-02-04 13:19

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('sounds', '0052_alter_sound_type'),
('geotags', '0008_geotag_migrate_sound'),
]

operations = [
migrations.RenameField(
model_name='geotag',
old_name='sound2',
new_name='sound',
),
migrations.AlterField(
model_name='geotag',
name='sound',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='geotags2', to='sounds.sound'),
),
]
20 changes: 20 additions & 0 deletions geotags/migrations/0010_alter_geotag_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.23 on 2025-02-10 14:21

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('sounds', '0053_remove_sound_geotag'),
('geotags', '0009_rename_sound2_geotag_sound'),
]

operations = [
migrations.AlterField(
model_name='geotag',
name='sound',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='geotag', to='sounds.sound'),
),
]
10 changes: 5 additions & 5 deletions geotags/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class GeoTag(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
sound = models.OneToOneField("sounds.Sound", on_delete=models.CASCADE, related_name="geotag")
lat = models.FloatField(db_index=True)
lon = models.FloatField(db_index=True)
zoom = models.IntegerField()
Expand All @@ -40,14 +40,14 @@ class GeoTag(models.Model):
created = models.DateTimeField(db_index=True, auto_now_add=True)

def __str__(self):
return f"{self.user} ({self.lat:f},{self.lon:f})"
return f"({self.lat:f},{self.lon:f})"

def get_absolute_url(self):
return reverse('geotag', args=[smart_str(self.id)])

def retrieve_location_information(self):
"""Use the mapbox API to retrieve information about the latitude and longitude of this geotag.
If no iformation has been retrieved from mapbox and a mapbox access token is available, retrieve and
If no information has been retrieved from mapbox and a mapbox access token is available, retrieve and
store that information. Then, pre-process that information to save a place name for display purposes.
"""
if settings.MAPBOX_ACCESS_TOKEN and (self.information is None or self.should_update_information):
Expand All @@ -67,11 +67,11 @@ def retrieve_location_information(self):
# Try with "place" feature
self.location_name = [feature for feature in features if 'place' in feature['place_type']][0]['place_name']
except IndexError:
# If "place" feature is not avialable, use "locality" feature
# If "place" feature is not available, use "locality" feature
try:
self.location_name = [feature for feature in features if 'locality' in feature['place_type']][0]['place_name']
except IndexError:
# If "place" nor "locality" features are avialable, use "region"
# If "place" nor "locality" features are available, use "region"
try:
self.location_name = [feature for feature in features if 'region' in feature['place_type']][0]['place_name']
except:
Expand Down
11 changes: 5 additions & 6 deletions geotags/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def test_browse_geotags_for_user_deleted_user(self):

def test_geotags_infowindow(self):
sound = Sound.objects.first()
gt = GeoTag.objects.create(user=sound.user, lat=45.8498, lon=-62.6879, zoom=9)
sound.geotag = gt
sound.save()
gt = GeoTag.objects.create(sound=sound, lat=45.8498, lon=-62.6879, zoom=9)
resp = self.client.get(reverse('geotags-infowindow', kwargs={'sound_id': sound.id}))
self.check_context(resp.context, {'sound': sound})
self.assertContains(resp, f'href="/people/{sound.user.username}/sounds/{sound.id}/"')
Expand All @@ -82,10 +80,11 @@ def test_browse_geotags_case_insensitive(self):
sounds[1].set_tags([tag])
sounds[0].set_tags([tag.upper()])

gt = GeoTag.objects.create(user=user, lat=45.8498, lon=-62.6879, zoom=9)
lat = 45.8498
lon = -62.6879

for sound in sounds:
sound.geotag = gt
sound.save()
GeoTag.objects.create(sound=sound, lat=lat + 0.0001, lon=lon + 0.0001, zoom=9)

resp = self.client.get(reverse('geotags-barray', kwargs={'tag': tag}))
# Response contains 3 int32 objects per sound: id, lat and lng. Total size = 3 * 4 bytes = 12 bytes
Expand Down
14 changes: 0 additions & 14 deletions sounds/fixtures/sounds.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
"created": "2005-03-09 10:45:28",
"processing_log": null,
"avg_rating": 7.375,
"geotag": null,
"original_filename": "cello-voice.wav",
"moderation_date": "2005-03-09 10:45:28",
"channels": 1,
Expand Down Expand Up @@ -167,7 +166,6 @@
"created": "2005-03-09 10:46:12",
"processing_log": null,
"avg_rating": 7.7000000000000002,
"geotag": null,
"original_filename": "voice-boat.wav",
"moderation_date": "2005-03-09 10:46:12",
"channels": 1,
Expand Down Expand Up @@ -203,7 +201,6 @@
"created": "2005-03-09 10:46:38",
"processing_log": null,
"avg_rating": 6.7083333333299997,
"geotag": null,
"original_filename": "voice-cat.wav",
"moderation_date": "2005-03-09 10:46:38",
"channels": 1,
Expand Down Expand Up @@ -239,7 +236,6 @@
"created": "2005-03-09 10:47:07",
"processing_log": null,
"avg_rating": 7.6166666666699996,
"geotag": null,
"original_filename": "voice-gong.wav",
"moderation_date": "2005-03-09 10:47:07",
"channels": 1,
Expand Down Expand Up @@ -275,7 +271,6 @@
"created": "2005-03-09 10:47:33",
"processing_log": null,
"avg_rating": 6.8181818181800002,
"geotag": null,
"original_filename": "voice-plane.wav",
"moderation_date": "2005-03-09 10:47:33",
"channels": 1,
Expand Down Expand Up @@ -324,7 +319,6 @@
"created": "2005-03-09 13:41:47",
"processing_log": null,
"avg_rating": 8.1216216216199992,
"geotag": null,
"original_filename": "sweep_log.wav",
"moderation_date": "2005-03-09 13:41:47",
"channels": 2,
Expand Down Expand Up @@ -360,7 +354,6 @@
"created": "2005-03-09 17:36:23",
"processing_log": null,
"avg_rating": 7.0,
"geotag": null,
"original_filename": "Fuzzy.wav",
"moderation_date": "2005-03-09 17:36:23",
"channels": 1,
Expand Down Expand Up @@ -396,7 +389,6 @@
"created": "2005-03-09 18:37:24",
"processing_log": null,
"avg_rating": 8.0,
"geotag": null,
"original_filename": "digighost.wav",
"moderation_date": "2005-03-09 18:37:24",
"channels": 2,
Expand Down Expand Up @@ -432,7 +424,6 @@
"created": "2005-03-09 19:01:34",
"processing_log": null,
"avg_rating": 2.5,
"geotag": null,
"original_filename": "gesprekken stoel kraak lachen bij tim.wav",
"moderation_date": "2005-03-09 19:01:34",
"channels": 2,
Expand Down Expand Up @@ -481,7 +472,6 @@
"created": "2005-03-09 20:37:55",
"processing_log": null,
"avg_rating": 8.8947368421099995,
"geotag": null,
"original_filename": "Glass A ff.wav",
"moderation_date": "2005-03-09 20:37:55",
"channels": 1,
Expand Down Expand Up @@ -517,7 +507,6 @@
"created": "2005-03-09 20:40:34",
"processing_log": null,
"avg_rating": 0.0,
"geotag": null,
"original_filename": "Glass A mf.wav",
"moderation_date": "2005-03-09 20:40:34",
"channels": 1,
Expand Down Expand Up @@ -553,7 +542,6 @@
"created": "2005-03-09 20:40:34",
"processing_log": null,
"avg_rating": 6.61538461538,
"geotag": null,
"original_filename": "Glass A pp.wav",
"moderation_date": "2005-03-09 20:40:34",
"channels": 1,
Expand Down Expand Up @@ -589,7 +577,6 @@
"created": "2005-03-09 20:40:34",
"processing_log": null,
"avg_rating": 9.0,
"geotag": null,
"original_filename": "Glass A# ff.wav",
"moderation_date": "2005-03-09 20:40:34",
"channels": 1,
Expand Down Expand Up @@ -625,7 +612,6 @@
"created": "2005-03-09 20:40:34",
"processing_log": null,
"avg_rating": 0.0,
"geotag": null,
"original_filename": "Glass A# mf.wav",
"moderation_date": "2005-03-09 20:40:34",
"channels": 1,
Expand Down
Loading
Loading