From d124585079e35804df9c041de0797a0921ec264d Mon Sep 17 00:00:00 2001 From: Femi Lamptey Date: Tue, 17 Apr 2018 11:47:38 +0000 Subject: [PATCH 1/3] Created function to delete Searchables when event is deleted Added the function delete_event_searchable in events\model\events.py, which deletes the searchable for an event, and called that function in get_together\views\events.py in the delete_event function to delete the searchable for the event before deleting the event --- events/models/events.py | 23 +++++++++++++++++++++++ get_together/views/events.py | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/events/models/events.py b/events/models/events.py index f856eef..a5c48d4 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -173,6 +173,29 @@ 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) + except: + searchable = Searchable(event_uri) + searchable.origin_node = origin_url + searchable.federation_node = origin_url + searchable.federation_time = timezone.now() + + searchable.event_url = event_url + searchable.delete() + 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/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: From d42272ce63c06833845308513518c077e2db276a Mon Sep 17 00:00:00 2001 From: Femi Lamptey Date: Tue, 17 Apr 2018 15:15:28 +0000 Subject: [PATCH 2/3] Refactoring delete_event_searchable Removed unnecessary code --- events/models/events.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/events/models/events.py b/events/models/events.py index a5c48d4..5e0d7cb 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -187,14 +187,10 @@ def delete_event_searchable(event): try: searchable = Searchable.objects.get(event_uri=event_uri) + searchable.delete() except: - searchable = Searchable(event_uri) - searchable.origin_node = origin_url - searchable.federation_node = origin_url - searchable.federation_time = timezone.now() + pass - searchable.event_url = event_url - searchable.delete() def slugify(s, ok=SLUG_OK, lower=True, spaces=False): # L and N signify letter/number. From 72f6afb1c4a830d1bf47c33e11d3fdbbe0360cb5 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Tue, 17 Apr 2018 11:31:47 -0400 Subject: [PATCH 3/3] Add test case for searchable deletion --- events/tests/federation.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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) + +