On 3/12/24 11:12 PM, peter--- via Mailman-users wrote:
FOr the other issue, I changed https://gitlab.com/mailman/hyperkitty/-/blob/master/hyperkitty/views/thread....
From:
> if request.user.is_authenticated:
> last_view_obj, created = LastView.objects.get_or_create(
> thread=thread, user=request.user)
> if not created:
> last_view = last_view_obj.view_date
> last_view_obj.save() # update timestamp
to:
> if request.user.is_authenticated:
> try:
> last_view_obj, created = LastView.objects.get_or_create(
> thread=thread, user=request.user)
> except LastView.MultipleObjectsReturned:
> last_view_duplicate, last_view_obj = LastView.objects.filter(
> thread=thread, user=request.user).order_by("view_date").all()
> last_view_duplicate.delete()
> created = False
>
> if not created:
> last_view = last_view_obj.view_date
> last_view_obj.save() # update timestamp
and that seems to have fixed it.
Wrapping the last_view_obj, created = LastView.objects.get_or_create() in a try, except seems appropriate, but your code in the except clause assumes there will be exactly 2 objects returned. Something like
except LastView.MultipleObjectsReturned:
last_view_objects = LastView.objects.filter(
thread=thread,
user=request.user).order_by("view_date").all()
last_view_obj = last_view_objects[-1]
for object in last_view_objects[:-1]:
object.delete()
might be better.
I've been thinking though that it'd be better to fix duplicates in the LastView class rather than on every get() or get_or_create() call.
That might be good, but these exceptions are rare. I'm not sure how they even occur, so catching them on the get() or get_or_create() calls seems OK.
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan