Skip to content

Commit 1142c41

Browse files
committed
make source_url unique; small update to context_date timezone handling
1 parent 0be6eb5 commit 1142c41

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.7 on 2025-12-04 00:39
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('events', '0033_events_comments_count_events_likes_count_and_more'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='events',
15+
name='source_url',
16+
field=models.TextField(blank=True, help_text="'https://university.edu/events/career-fair'", null=True, unique=True),
17+
),
18+
]

backend/apps/events/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class Events(models.Model):
3131
)
3232

3333
source_url = models.TextField(
34-
null=True, blank=True, help_text="'https://university.edu/events/career-fair'"
34+
null=True,
35+
blank=True,
36+
unique=True,
37+
help_text="'https://university.edu/events/career-fair'",
3538
)
3639
source_image_url = models.TextField(
3740
null=True,

backend/services/openai_service.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,20 @@ def extract_events_from_caption(
110110
- Return an array of JSON objects, each object representing a unique event.
111111
"""
112112
# Get current date and day of week for context
113-
now = datetime.now()
113+
toronto_tz = pytz_timezone("America/Toronto")
114+
now = datetime.now(toronto_tz)
114115
current_date = now.strftime("%Y-%m-%d")
115116
current_day_of_week = now.strftime("%A")
116117

117118
context_datetime = (
118119
post_created_at if isinstance(post_created_at, datetime) else now
119120
)
120-
context_datetime = pytz_timezone("America/Toronto").localize(
121-
context_datetime.replace(tzinfo=None)
122-
)
121+
122+
if context_datetime.tzinfo is None:
123+
context_datetime = toronto_tz.localize(context_datetime)
124+
else:
125+
context_datetime = context_datetime.astimezone(toronto_tz)
126+
123127
context_date = context_datetime.strftime("%Y-%m-%d")
124128
context_day = context_datetime.strftime("%A")
125129
context_time = context_datetime.strftime("%H:%M")

backend/utils/scraping_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from difflib import SequenceMatcher
55
from pathlib import Path
66

7-
from django.db import transaction
7+
from django.db import transaction, IntegrityError
88
from django.utils import timezone
99

1010
from apps.clubs.models import Clubs
@@ -179,6 +179,9 @@ def insert_event_to_db(event_data, ig_handle, source_url, club_type=None):
179179
f"{log_prefix} Created {len(event_dates)} EventDates entries for event {event.id}"
180180
)
181181
return True
182+
except IntegrityError:
183+
logger.warning(f"{log_prefix} Duplicate event detected (IntegrityError) - likely race condition")
184+
return "duplicate"
182185
except Exception as e:
183186
logger.error(f"{log_prefix} Error inserting event to DB: {e}")
184187
return False

0 commit comments

Comments
 (0)