From 80006cb93430f84f63bf801c84b6a0932fb5d04b Mon Sep 17 00:00:00 2001 From: SamuelmdLow Date: Tue, 27 Aug 2024 09:31:58 +0000 Subject: [PATCH] Use a hash for events instead of url --- events/models.py | 49 +++++++++++++++++-- events/views.py | 14 +++++- home/templates/home/stream_blocks/links.html | 4 +- .../src/js/components/Events/calendar.jsx | 12 +++-- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/events/models.py b/events/models.py index bf31be423..3cf7634ea 100644 --- a/events/models.py +++ b/events/models.py @@ -14,11 +14,16 @@ import asyncio from asgiref.sync import sync_to_async import aiohttp +import hashlib +import base64 # Create your models here. class EventManager(models.Manager): + def hashing(self, string): + return base64.b64encode(hashlib.blake2b(string.encode(), digest_size=6).hexdigest().encode()).decode() + async def read_ical(self, name, file, create_function): try: #print("Requesting " + name) @@ -63,6 +68,7 @@ async def wp_events_api_create_event(self, event_json, api, host, categorize): event = await self.acreate( title=event_json['title'], event_url=event_json['link'], + hash=self.hashing(event_json['link'] + str(event_json['start'])) ) else: event = await self.filter(event_url=event_json['link'], start_time=datetime.fromisoformat(event_json['start']).astimezone(timezone.get_current_timezone())).afirst() @@ -70,6 +76,10 @@ async def wp_events_api_create_event(self, event_json, api, host, categorize): return None event.title = str(event_json['title']['rendered'].encode('utf-8'), 'UTF-8') + + if event.hash == "": + event.hash = self.hashing(event_json['link'] + str(event_json['start'])) + event.description = str(event_json['excerpt']['rendered'].encode('utf-8'), 'UTF-8') event.start_time = datetime.fromisoformat(event_json['start']).astimezone(timezone.get_current_timezone()) @@ -157,12 +167,17 @@ async def ubcevents_create_event(self, ical_component): event = await self.acreate( title=ical_component.get('summary'), event_url=ical_component.decoded('url'), + hash=self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) ) else: event = await self.filter(event_url=ical_component.get('url')).afirst() if event.update_mode != 2: return None + + if event.hash == "": + event.hash = self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) + # Split location and address location = ical_component.get('location') address = "" @@ -299,16 +314,22 @@ def ubcevents_category(self, event): async def gothunderbirds_create_event(self, ical_component): - if not await self.filter(event_url=ical_component.get('url').replace("&", "__AND__")).aexists(): + if not await self.filter(event_url=ical_component.get('url')).aexists(): event = await self.acreate( title=ical_component.get('summary'), - event_url=ical_component.decoded('url').replace("&", "__AND__"), + event_url=ical_component.decoded('url'), + hash=self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) ) else: - event = await self.filter(event_url=ical_component.get('url').replace("&", "__AND__")).afirst() + event = await self.filter(event_url=ical_component.get('url')).afirst() if event.update_mode != 2: return None + + if event.hash == "": + event.hash = self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) + + # Split location and address address = ical_component.get('location') location = address.replace('Vancouver, B.C., ', '') @@ -345,7 +366,7 @@ async def gothunderbirds_create_event(self, ical_component): event.address=address event.location=location - event.event_url=ical_component.decoded('url').replace("&", "__AND__") + event.event_url=ical_component.decoded('url') event.category='sports' event.hidden=self.gothunderbirds_judge_hidden(ical_component) @@ -419,12 +440,16 @@ async def phas_ubc_create_event(self, event_component): event = await self.acreate( title=title, event_url=event_url, + hash=self.hashing(title + str(event_component.find('span', class_='start').get_text(strip=True))) ) else: event = await self.filter(event_url=event_url).afirst() if event.update_mode != 2: return None + if event.hash == "": + event.hash = self.hashing(title + str(event_component.find('span', class_='start').get_text(strip=True))) + # Extract start time start_time_str = event_component.find('span', class_='start').get_text(strip=True) parsed_start_time = datetime.fromisoformat(start_time_str) @@ -474,6 +499,7 @@ async def cs_ubc_create_event(self, ical_component): event = await self.acreate( title=ical_component.get('summary'), event_url=ical_component.decoded('url'), + hash=self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) ) else: event = await self.filter(event_url=ical_component.get('url')).afirst() @@ -481,6 +507,10 @@ async def cs_ubc_create_event(self, ical_component): return None event.title=ical_component.get('summary') + + if event.hash == "": + event.hash = self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) + event.description= "
" + ical_component.get('description').replace("&", "&") if "
Name:" in event.description and "\nTitle" in event.description: if event.description.index("
Name:") < event.description.index("\nTitle"): @@ -550,6 +580,7 @@ async def stats_ubc_create_event(self, ical_component): event = await self.acreate( title=ical_component.get('summary'), event_url=ical_component.decoded('url'), + hash=self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) ) else: event = await self.filter(event_url=ical_component.get('url')).afirst() @@ -560,6 +591,9 @@ async def stats_ubc_create_event(self, ical_component): event.title=ical_component.get('summary') + if event.hash == "": + event.hash = self.hashing(ical_component.get('summary') + str(ical_component.decoded('dtstart'))) + # Clean up event description because they are so messy and have unnecessary information description = str(ical_component.decoded('description'), 'UTF-8') safety = 10 @@ -663,6 +697,12 @@ class Event(models.Model): null=True, blank=True, ) + hash = models.CharField( + max_length=50, + blank=False, + null=True, + default='' + ) image = models.CharField( max_length=255, blank=True, @@ -696,6 +736,7 @@ class Event(models.Model): FieldPanel("address"), FieldPanel("host"), FieldPanel("event_url"), + FieldPanel("hash"), FieldPanel("image"), FieldPanel( "category", diff --git a/events/views.py b/events/views.py index 29a223bcf..c5582b54b 100644 --- a/events/views.py +++ b/events/views.py @@ -72,6 +72,18 @@ def react(self, request): 'description': "Events Around Campus collected by The Ubyssey", 'url': 'https://ubyssey.ca/events/', } + + if request.GET.get("event"): + if Event.objects.filter(hash=request.GET.get("event")).exists(): + event = Event.objects.filter(hash=request.GET.get("event")).first() + + meta = { + 'title': event.title, + 'description': event.description, + 'url': "https://ubyssey.ca/events/?event=" + event.hash, + 'noindex': True, + } + return render(request, "events/event_page_react.html", {'ical':ical, 'rss':rss, 'meta':meta}) def landing(self, request): @@ -593,7 +605,7 @@ def item_link(self, item): class EventsSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Event - fields = ['id', 'title', 'description', 'start_time', 'end_time', 'location', 'address', 'host', 'email', 'event_url', 'category'] + fields = ['id', 'title', 'description', 'start_time', 'end_time', 'location', 'address', 'host', 'email', 'event_url', 'hash', 'category'] class EventsViewSet(viewsets.ModelViewSet): serializer_class = EventsSerializer diff --git a/home/templates/home/stream_blocks/links.html b/home/templates/home/stream_blocks/links.html index 308c38325..02268b6af 100644 --- a/home/templates/home/stream_blocks/links.html +++ b/home/templates/home/stream_blocks/links.html @@ -21,7 +21,7 @@

Events Around Campus

- + @@ -30,7 +30,7 @@

Upcoming

{% for event in events %} {% if not forloop.counter0 == 0 %}
  • - + {{event.display_time}} {{event.title}}
  • diff --git a/ubyssey/static_src/src/js/components/Events/calendar.jsx b/ubyssey/static_src/src/js/components/Events/calendar.jsx index 5149f1319..73768d335 100644 --- a/ubyssey/static_src/src/js/components/Events/calendar.jsx +++ b/ubyssey/static_src/src/js/components/Events/calendar.jsx @@ -221,9 +221,10 @@ function EventsOptions() { 'description': capitalize(category) + " Around Campus collected by The Ubyssey", 'url': 'https://ubyssey.ca/events/?category=' + category, }; - } + document.getElementsByTagName("title")[0].innerHTML = meta.title + return ( <>
    @@ -428,11 +429,11 @@ function EventsCalendar({events}) { {day.day_of_week} {day.day}.
      {day.events.map((event) => -
    • - ", ", ")} className="calendar-item" to={"?event=" + event.event_url} event-url={event.event_url} +
    • + ", ", ")} className="calendar-item" to={"?event=" + event.hash} event-url={event.event_url} onClick={(e) => { e.preventDefault(); - searchParams.set("event", event.event_url); + searchParams.set("event", event.hash); setSearchParams(searchParams); }} dangerouslySetInnerHTML={ @@ -468,8 +469,9 @@ function EventInfo({events}) { if (query.get("event") != null){ let eventHash = query.get("event"); for (let i=0; i