def test_hash(self): regid = "9136CCB8F66711D5BE060004AC494FFE" notices = get_notices_by_regid(regid) notice = notices[0] model = UserNotices() hash = model.generate_hash(notice) self.assertEquals(hash, "fbb05e8edaf403b7c5eb5a9ac8d7cc3b")
def _get_user_notices(notices): user = get_user_model() notice_dict = {} notices_with_read_status = [] # Get all notice hashes for notice in notices: notice_hash = UserNotices().generate_hash(notice) notice.id_hash = notice_hash notice.is_read = False notice_dict[notice_hash] = notice # Set read status for notices already in db 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() 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 = notices_with_read_status + notice_dict.values() return notices_with_read_status
def mark_notices_read_for_current_user(notice_hashes): user = get_user_model() UserNotices().mark_notices_read(notice_hashes, user)