Add unique URI to Searchable that won't change if the event name changes

This commit is contained in:
Michael Hall 2018-03-10 23:13:57 -08:00
parent 4debf8e255
commit e0df6b3f5b
5 changed files with 82 additions and 3 deletions

View file

@ -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),
),
]

View file

@ -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)
]

View file

@ -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(),
),
]

View file

@ -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:

View file

@ -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)