diff --git a/events/migrations/0014_add_searchable_uri.py b/events/migrations/0014_add_searchable_uri.py new file mode 100644 index 0000000..2ec2185 --- /dev/null +++ b/events/migrations/0014_add_searchable_uri.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0 on 2018-03-07 17:37 + +from django.db import migrations, models +import pytz + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0013_auto_20180304_1649'), + ] + + operations = [ + migrations.AddField( + model_name='searchable', + name='event_uri', + field=models.CharField(max_length=256, null=True, serialize=False), + ), + ] diff --git a/events/migrations/0015_populate_searchable_uri.py b/events/migrations/0015_populate_searchable_uri.py new file mode 100644 index 0000000..e7bbe35 --- /dev/null +++ b/events/migrations/0015_populate_searchable_uri.py @@ -0,0 +1,26 @@ +# Generated by Django 2.0 on 2018-03-11 06:32 + +from django.db import migrations +import hashlib + +def gen_event_uris(apps, schema_editor): + Event = MyModel = apps.get_model('events', 'Event') + Searchable = MyModel = apps.get_model('events', 'Searchable') + for searchable in Searchable.objects.all(): + md5 = hashlib.md5() + federation_url = searchable.event_url.split('/') + federation_node = '/'.join(federation_url[:3]) + federation_id = '/'.join(federation_url[:5]) + md5.update(bytes(federation_id, 'utf8')) + searchable.event_uri = federation_node + '/' + md5.hexdigest() + searchable.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0014_add_searchable_uri'), + ] + + operations = [ + migrations.RunPython(gen_event_uris, reverse_code=migrations.RunPython.noop) + ] diff --git a/events/migrations/0016_make_searchable_uri_unique.py b/events/migrations/0016_make_searchable_uri_unique.py new file mode 100644 index 0000000..f6a3be4 --- /dev/null +++ b/events/migrations/0016_make_searchable_uri_unique.py @@ -0,0 +1,23 @@ +# Generated by Django 2.0 on 2018-03-11 06:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0015_populate_searchable_uri'), + ] + + operations = [ + migrations.AlterField( + model_name='searchable', + name='event_uri', + field=models.CharField(max_length=256, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='searchable', + name='event_url', + field=models.URLField(), + ), + ] diff --git a/events/models/events.py b/events/models/events.py index d085f82..90cc63f 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -14,6 +14,7 @@ import re import pytz import datetime import unicodedata +import hashlib SLUG_OK = '-_~' @@ -92,14 +93,24 @@ def update_event_searchable(event): site = Site.objects.get(id=1) event_url = "https://%s%s" % (site.domain, event.get_absolute_url()) origin_url = "https://%s%s" % (site.domain, reverse('searchables')) + + md5 = hashlib.md5() + federation_url = event_url.split('/') + federation_node = '/'.join(federation_url[:3]) + federation_id = '/'.join(federation_url[:5]) + md5.update(bytes(federation_id, 'utf8')) + event_uri = federation_node + '/' + md5.hexdigest() + try: - searchable = Searchable.objects.get(event_url=event_url) + searchable = Searchable.objects.get(event_uri=event_uri) except: - searchable = Searchable(event_url) + searchable = Searchable(event_uri) searchable.origin_node = origin_url searchable.federation_node = origin_url searchable.federation_time = datetime.datetime.now() + searchable.event_url = event_url + if event.team.category: searchable.img_url = event.team.category.img_url else: diff --git a/events/models/search.py b/events/models/search.py index d21edc6..6745c9a 100644 --- a/events/models/search.py +++ b/events/models/search.py @@ -6,7 +6,8 @@ import datetime # Provides a searchable index of events that may belong to this site or a federated site class Searchable(models.Model): - event_url = models.URLField(primary_key=True, null=False, blank=False) + event_uri = models.CharField(primary_key=True, max_length=256, null=False, blank=False) + event_url = models.URLField(null=False, blank=False) event_title = models.CharField(max_length=256, null=False, blank=False) img_url = models.URLField(null=False, blank=False) location_name = models.CharField(max_length=256, null=False, blank=False)