Skip to content

Commit c1d7948

Browse files
committed
add withdrawal notifications and bulletin deactivation
1 parent 9b4b03c commit c1d7948

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

questions/services.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from rest_framework.exceptions import ValidationError
1111

1212
from coherence.models import CoherenceLink
13+
from misc.models import Bulletin, BulletinViewedBy
1314
from notifications.constants import MailingTags
1415
from notifications.services import delete_scheduled_question_resolution_notifications
1516
from posts.models import PostUserSnapshot, PostSubscription, Notebook, Post
@@ -899,6 +900,9 @@ def update_forecast_notification(
899900
900901
When created=True: Creates/updates notification if forecast has future end_time
901902
When created=False: Deletes existing notification for user/question pair
903+
904+
In condition where forecast is during a grace period, deactivate scheduled emails
905+
and treat Bulletin as viewed.
902906
"""
903907

904908
user = forecast.author
@@ -945,6 +949,14 @@ def update_forecast_notification(
945949
},
946950
)
947951

952+
post_bulletins = Bulletin.objects.filter(
953+
# since this is an automated bulletin, should it get
954+
# another field we can filter on like "type"?
955+
post=question.get_post(),
956+
)
957+
for bulletin in post_bulletins:
958+
BulletinViewedBy.objects.create(bulletin=bulletin, user=user)
959+
948960

949961
@sentry_sdk.trace
950962
def get_questions_cutoff(

questions/tasks.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from posts.models import Post
2020
from posts.services.subscriptions import notify_post_status_change
21-
from questions.models import Question, UserForecastNotification
21+
from questions.models import Forecast, Question, UserForecastNotification
2222
from questions.services import (
2323
build_question_forecasts,
2424
get_forecasts_per_user,
@@ -283,7 +283,7 @@ def multiple_choice_delete_option_notificiations(
283283
),
284284
)
285285

286-
# send out an email
286+
# send out an immediate email
287287
forecaster_emails = (
288288
User.objects.filter(
289289
forecast__in=question.user_forecasts.filter(
@@ -331,6 +331,23 @@ def multiple_choice_add_option_notificiations(
331331
options_history = question.options_history
332332
added_options = list(set(options_history[-1][1]) - set(options_history[-2][1]))
333333

334+
forecasters = (
335+
User.objects.filter(
336+
forecast__in=question.user_forecasts.filter(
337+
end_time=grace_period_end
338+
) # all effected forecasts have their end_time set to grace_period_end
339+
)
340+
.exclude(
341+
ubsubscribed_mailing_tags__contains=[
342+
MailingTags.BEFORE_PREDICTION_AUTO_WITHDRAWAL # seems most reasonable
343+
]
344+
)
345+
.exclude(email__isnull=True)
346+
.exclude(email="")
347+
.distinct("id")
348+
.order_by("id")
349+
)
350+
334351
# send out a comment
335352
comment_author = User.objects.get(id=comment_author_id)
336353
create_comment(
@@ -347,24 +364,8 @@ def multiple_choice_add_option_notificiations(
347364
),
348365
)
349366

350-
# send out an email
351-
forecaster_emails = (
352-
User.objects.filter(
353-
forecast__in=question.user_forecasts.filter(
354-
Q(end_time__isnull=True) | Q(end_time__gt=timestep)
355-
)
356-
)
357-
.exclude(
358-
ubsubscribed_mailing_tags__contains=[
359-
MailingTags.BEFORE_PREDICTION_AUTO_WITHDRAWAL # seems most reasonable
360-
]
361-
)
362-
.exclude(email__isnull=True)
363-
.exclude(email="")
364-
.values_list("email", flat=True)
365-
.distinct("id")
366-
.order_by("id")
367-
)
367+
# send out an immediate email
368+
forecaster_emails = forecasters.values_list("email", flat=True)
368369
start = 0
369370
batch_size = 300
370371
while True:
@@ -384,7 +385,24 @@ def multiple_choice_add_option_notificiations(
384385

385386
# schedule a followup email for 1 day before grace period
386387
# (if grace period is more than 1 day away)
387-
...
388+
if grace_period_end - timedelta(days=1) > timestep:
389+
for forecaster in forecasters:
390+
UserForecastNotification.objects.filter(
391+
user=forecaster, question=question
392+
).delete() # is this necessary?
393+
UserForecastNotification.objects.update_or_create(
394+
user=forecaster,
395+
question=question,
396+
defaults={
397+
"trigger_time": grace_period_end - timedelta(days=1),
398+
"email_sent": False,
399+
"forecast": Forecast.objects.filter(
400+
question=question, author=forecaster
401+
)
402+
.order_by("-start_time")
403+
.first(),
404+
},
405+
)
388406

389407
# add Post bulletin
390408
Bulletin.objects.create(

0 commit comments

Comments
 (0)