From 27acec196ab2660151a5cbb9382f225a1e9d4dd3 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Thu, 23 Feb 2017 12:13:55 +0100 Subject: [PATCH] comment notifications --- rowers/models.py | 3 +- rowers/tasks.py | 31 ++++++++++++ rowers/templates/workout_comments.html | 2 +- rowers/urls.py | 1 + rowers/views.py | 66 ++++++++++++++++++++++++-- static/css/rowsandall.css | 7 +-- 6 files changed, 101 insertions(+), 9 deletions(-) diff --git a/rowers/models.py b/rowers/models.py index 11a13f40..e9dc8228 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -891,6 +891,7 @@ class WorkoutComment(models.Model): comment = models.TextField(max_length=300) created = models.DateTimeField(default=timezone.now) read = models.BooleanField(default=False) + notification = models.BooleanField(default=False,verbose_name="Send me notifications") user = models.ForeignKey(User) workout = models.ForeignKey(Workout) @@ -905,7 +906,7 @@ class WorkoutComment(models.Model): class WorkoutCommentForm(ModelForm): class Meta: model = WorkoutComment - fields = ['comment',] + fields = ['comment','notification'] widgets = { 'comment': forms.Textarea, } diff --git a/rowers/tasks.py b/rowers/tasks.py index e0367862..40d8747b 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -273,6 +273,37 @@ def handle_sendemail_invite(email,name,code,teamname,manager): return 1 +@app.task +def handle_sendemailnewresponse(first_name,last_name, + email, + commenter_first_name, + commenter_last_name, + comment, + workoutname,workoutid,commentid): + fullemail = first_name+' '+last_name+' <'+email+'>' + subject = 'New comment on workout '+workoutname + message = 'Dear '+first_name+',\n\n' + message += commenter_first_name+' '+commenter_last_name + message += ' has written a new comment on the workout ' + message += workoutname+'\n\n' + message += comment + message += '\n\n' + message += 'You can read the comment here:\n' + message += 'https://rowsandall.com/rowers/workout/'+str(workoutid)+'/comment' + message += '\n\n' + message += 'You are receiving this email because you are subscribed ' + message += 'to comments on this workout. To unsubscribe, follow this link:\n' + message += 'https://rowsandall.com/rowers/workout/unsubscribe/'+str(workoutid)+'/' + + email = EmailMessage(subject, message, + 'Rowsandall ', + [fullemail]) + + + res = email.send() + + return 1 + @app.task def handle_sendemailnewcomment(first_name, last_name, diff --git a/rowers/templates/workout_comments.html b/rowers/templates/workout_comments.html index 438388a5..f9dbfe7c 100644 --- a/rowers/templates/workout_comments.html +++ b/rowers/templates/workout_comments.html @@ -57,7 +57,7 @@ {% endfor %}
-
+ {{ form.as_table }}
diff --git a/rowers/urls.py b/rowers/urls.py index c756414f..24945f8e 100644 --- a/rowers/urls.py +++ b/rowers/urls.py @@ -151,6 +151,7 @@ urlpatterns = [ url(r'^workout/upload/(.+.*)$',views.workout_upload_view), url(r'^workout/(?P\d+)/histo$',views.workout_histo_view), url(r'^workout/(?P\d+)/forcecurve$',views.workout_forcecurve_view), + url(r'^workout/(?P\d+)/unsubscribe$',views.workout_unsubscribe_view), url(r'^workout/(?P\d+)/export/c/(?P\w+.*)/s/(?P\w+.*)$',views.workout_export_view), url(r'^workout/(?P\d+)/export/c/(?P\w+.*)$',views.workout_export_view), url(r'^workout/(?P\d+)/export/s/(?P\w+.*)$',views.workout_export_view), diff --git a/rowers/views.py b/rowers/views.py index 929edfb7..e4c80ddf 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -60,7 +60,8 @@ from rest_framework.parsers import JSONParser from rowers.rows import handle_uploaded_file from rowers.tasks import handle_makeplot,handle_otwsetpower,handle_sendemailtcx,handle_sendemailcsv from rowers.tasks import ( - handle_sendemail_unrecognized,handle_sendemailnewcomment + handle_sendemail_unrecognized,handle_sendemailnewcomment, + handle_sendemailnewresponse, ) from scipy.signal import savgol_filter @@ -3586,6 +3587,37 @@ def workout_export_view(request,id=0, message="", successmessage=""): 'c2userid':c2userid, }) +# +@login_required() +def workout_unsubscribe_view(request,id=0): + try: + w = Workout.objects.get(id=id) + except Workout.DoesNotExist: + raise Http404("Workout doesn't exist") + + if w.privacy == 'private' and w.user.user != request.user: + return HttpResponseForbidden("Permission error") + + comments = WorkoutComment.objects.filter(workout=w, + user=request.user).order_by("created") + + for c in comments: + c.notify = False + c.save() + + form = WorkoutCommentForm() + + message = 'You have been unsubscribed from new comment notifications for this workout' + + return render(request, + 'workout_comments.html', + {'workout':w, + 'comments':comments, + 'successmessage':message, + 'form':form, + }) + + # list of comments to a workout @login_required() def workout_comment_view(request,id=0): @@ -3597,6 +3629,8 @@ def workout_comment_view(request,id=0): if w.privacy == 'private' and w.user.user != request.user: return HttpResponseForbidden("Permission error") + comments = WorkoutComment.objects.filter(workout=w).order_by("created") + # ok we're permitted if request.method == 'POST': r = w.user @@ -3604,7 +3638,9 @@ def workout_comment_view(request,id=0): if form.is_valid(): cd = form.cleaned_data comment = cd['comment'] - c = WorkoutComment(workout=w,user=request.user,comment=comment) + notification = cd['notification'] + c = WorkoutComment(workout=w,user=request.user,comment=comment, + notification=notification) c.save() if settings.DEBUG: res = handle_sendemailnewcomment.delay(r.user.first_name, @@ -3615,6 +3651,7 @@ def workout_comment_view(request,id=0): comment,w.name, w.id) + elif request.user != r.user: res = queuehigh.enqueue(handle_sendemailnewcomment,r.user.first_name, r.user.last_name, @@ -3622,9 +3659,30 @@ def workout_comment_view(request,id=0): request.user.first_name, request.user.last_name, comment,w.name,w.id) - - comments = WorkoutComment.objects.filter(workout=w).order_by("created") + commenters = {oc.user for oc in comments if oc.notification} + for u in commenters: + if settings.DEBUG: + res = handle_sendemailnewresponse.delay(u.first_name, + u.last_name, + u.email, + request.user.first_name, + request.user.last_name, + comment, + w.name, + w.id, + c.id) + else: + res = queuelow.enqueue(handle_sendemailnewresponse, + u.first_name, + u.last_name, + u.email, + request.user.first_name, + request.user.last_name, + comment, + w.name, + w.id, + c.id) form = WorkoutCommentForm() diff --git a/static/css/rowsandall.css b/static/css/rowsandall.css index 09e1f881..24a757de 100644 --- a/static/css/rowsandall.css +++ b/static/css/rowsandall.css @@ -799,9 +799,10 @@ a.wh:hover { /* talk bubble contents */ .talktext{ - padding: 1em; - text-align: left; - line-height: 1.5em; + padding: 1em; + text-align: left; + line-height: 1.5em; + word-wrap: break-word; } .talktext p{ /* remove webkit p margins */