diff --git a/events/models/events.py b/events/models/events.py index f856eef..5e0d7cb 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -173,6 +173,25 @@ def update_event_searchable(event): searchable.save() +def delete_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_uri=event_uri) + searchable.delete() + except: + pass + + def slugify(s, ok=SLUG_OK, lower=True, spaces=False): # L and N signify letter/number. # http://www.unicode.org/reports/tr44/tr44-4.html#GC_Values_Table diff --git a/events/tests/federation.py b/events/tests/federation.py index 4a9c50f..d2f08fa 100644 --- a/events/tests/federation.py +++ b/events/tests/federation.py @@ -1,7 +1,7 @@ from django.test import TestCase from model_mommy import mommy -from ..models.events import Event +from ..models.events import Event, delete_event_searchable from ..models.search import Searchable # Create your tests here. @@ -38,3 +38,20 @@ class SearchableCreationTest(TestCase): assert(searchables.count() == 1) assert(searchables[0].event_title == "New Title") + def test_delete_searchable_with_event(self): + searchables = Searchable.objects.all() + assert(searchables.count() == 0) + + event = mommy.make(Event) + event.save() + + searchables = Searchable.objects.all() + assert(searchables.count() == 1) + + delete_event_searchable(event) + event.delete() + + searchables = Searchable.objects.all() + assert(searchables.count() == 0) + + diff --git a/get_together/views/events.py b/get_together/views/events.py index 98f62e4..79e7d00 100644 --- a/get_together/views/events.py +++ b/get_together/views/events.py @@ -7,7 +7,8 @@ from django.shortcuts import render, redirect, reverse, get_object_or_404 from django.http import HttpResponse, JsonResponse from django.utils import timezone -from events.models.events import Event, CommonEvent, EventSeries, EventPhoto, Place, Attendee +from events.models.events import Event, CommonEvent, EventSeries, EventPhoto, Place, Attendee, update_event_searchable, \ + delete_event_searchable from events.models.profiles import Team, Organization, UserProfile, Member from events.forms import ( TeamEventForm, @@ -276,6 +277,7 @@ def delete_event(request, event_id): form = DeleteEventForm(request.POST) if form.is_valid() and form.cleaned_data['confirm']: team_id = event.team_id + delete_event_searchable(event); event.delete() return redirect('show-team', team_id) else: