From 6a4f95465294b77ed7818cbb71948be7f4e76ec3 Mon Sep 17 00:00:00 2001 From: Fang Lin Date: Thu, 4 Jan 2024 14:46:00 -0800 Subject: [PATCH] roled back 8a6d2e894 (origin/bug/MUWM-5324) --- myuw/dao/notice.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/myuw/dao/notice.py b/myuw/dao/notice.py index 1e5406b9f..870a57071 100644 --- a/myuw/dao/notice.py +++ b/myuw/dao/notice.py @@ -78,19 +78,34 @@ def _get_user_notices(request, notices): notice_dict[notice_hash] = notice # Set read status for notices already in db - for user_notice in UserNotices.objects.filter( - user=user, notice_hash__in=notice_dict.keys()): + keys = notice_dict.keys() + user_notices = UserNotices.objects.filter(user=user, + notice_hash__in=keys) + for user_notice in user_notices: matched_notice = notice_dict[user_notice.notice_hash] matched_notice.is_read = user_notice.is_read notices_with_read_status.append(matched_notice) del notice_dict[user_notice.notice_hash] # Create UserNotices for new notices + user_notices_to_create = [] for notice in notice_dict.values(): - user_notice, _ = UserNotices.objects.get_or_create( - notice_hash=notice.id_hash, user=user, defaults={ - 'notice_cattype': notice.notice_category + notice.notice_type}) - notice.is_read = user_notice.is_read - notices_with_read_status.append(notice) - + user_notice = UserNotices() + user_notice.notice_hash = notice.id_hash + user_notice.user = user + cattype = notice.notice_category + notice.notice_type + user_notice.notice_cattype = cattype + + user_notices_to_create.append(user_notice) + + try: + UserNotices.objects.bulk_create(user_notices_to_create) + except IntegrityError: + # MUWM-2016. This should be rare - 2 processes running at just about + # exactly the same time. In that case especially, the bulk create list + # should be the same. And if it isn't, big deal? + pass + + # Add newly created UserNotices into returned list + notices_with_read_status.extend(list(notice_dict.values())) return notices_with_read_status