diff options
-rw-r--r-- | mediagoblin/db/mongo/migrations.py | 8 | ||||
-rw-r--r-- | mediagoblin/db/mongo/models.py | 4 | ||||
-rw-r--r-- | mediagoblin/db/sql/models.py | 1 | ||||
-rw-r--r-- | mediagoblin/edit/forms.py | 7 | ||||
-rw-r--r-- | mediagoblin/edit/views.py | 71 | ||||
-rw-r--r-- | mediagoblin/templates/mediagoblin/user_pages/comment_email.txt | 26 | ||||
-rw-r--r-- | mediagoblin/tests/test_edit.py | 1 | ||||
-rw-r--r-- | mediagoblin/user_pages/lib.py | 54 | ||||
-rw-r--r-- | mediagoblin/user_pages/views.py | 7 |
9 files changed, 146 insertions, 33 deletions
diff --git a/mediagoblin/db/mongo/migrations.py b/mediagoblin/db/mongo/migrations.py index 732f5846..569dec88 100644 --- a/mediagoblin/db/mongo/migrations.py +++ b/mediagoblin/db/mongo/migrations.py @@ -198,3 +198,11 @@ def convert_exif_media_data(database): del media_data['exif'] collection.save(document) + + +@RegisterMigration(13) +def user_add_wants_comment_notification(database): + """ + Add wants_comment_notification to user model + """ + add_table_field(database, 'users', 'wants_comment_notification', True) diff --git a/mediagoblin/db/mongo/models.py b/mediagoblin/db/mongo/models.py index 2e35a2b8..3f1363d5 100644 --- a/mediagoblin/db/mongo/models.py +++ b/mediagoblin/db/mongo/models.py @@ -62,6 +62,8 @@ class User(Document, UserMixin): we'll change this to a boolean with a key of 'active' and have a separate field for a reason the user's been disabled if that's appropriate... email_verified is already separate, after all.) + - wants_comment_notification: The user has selected that they want to be + notified when comments are posted on their media. - verification_key: If the user is awaiting email verification, the user will have to provide this key (which will be encoded in the presented URL) in order to confirm their email as active. @@ -80,6 +82,7 @@ class User(Document, UserMixin): 'pw_hash': unicode, 'email_verified': bool, 'status': unicode, + 'wants_comment_notification': bool, 'verification_key': unicode, 'is_admin': bool, 'url': unicode, @@ -93,6 +96,7 @@ class User(Document, UserMixin): default_values = { 'created': datetime.datetime.utcnow, 'email_verified': False, + 'wants_comment_notification': True, 'status': u'needs_email_verification', 'is_admin': False} diff --git a/mediagoblin/db/sql/models.py b/mediagoblin/db/sql/models.py index e87aaddb..ba28ab7b 100644 --- a/mediagoblin/db/sql/models.py +++ b/mediagoblin/db/sql/models.py @@ -70,6 +70,7 @@ class User(Base, UserMixin): pw_hash = Column(Unicode, nullable=False) email_verified = Column(Boolean, default=False) status = Column(Unicode, default=u"needs_email_verification", nullable=False) + wants_comment_notification = Column(Boolean, default=True, nullable=False) verification_key = Column(Unicode) is_admin = Column(Boolean, default=False, nullable=False) url = Column(Unicode) diff --git a/mediagoblin/edit/forms.py b/mediagoblin/edit/forms.py index ca52f298..e8fb450e 100644 --- a/mediagoblin/edit/forms.py +++ b/mediagoblin/edit/forms.py @@ -61,14 +61,15 @@ class EditProfileForm(wtforms.Form): class EditAccountForm(wtforms.Form): old_password = wtforms.PasswordField( _('Old password'), - [wtforms.validators.Required()], description=_( "Enter your old password to prove you own this account.")) new_password = wtforms.PasswordField( _('New password'), - [wtforms.validators.Required(), - wtforms.validators.Length(min=6, max=30)], + [wtforms.validators.Length(min=6, max=30)], id="password") + wants_comment_notification = wtforms.BooleanField( + _('Comment notification?'), + description=_("Check this box to be emailed when someone else comments on your media.")) class EditAttachmentsForm(wtforms.Form): diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py index 60a4cb5b..59f6824f 100644 --- a/mediagoblin/edit/views.py +++ b/mediagoblin/edit/views.py @@ -181,39 +181,50 @@ def edit_profile(request): @require_active_login def edit_account(request): user = request.user - - form = forms.EditAccountForm(request.POST) - - if request.method == 'POST' and form.validate(): - password_matches = auth_lib.bcrypt_check_password( - request.POST['old_password'], - user.pw_hash) - - if (request.POST['old_password'] or request.POST['new_password']) and not \ - password_matches: - form.old_password.errors.append(_('Wrong password')) - - return render_to_response( - request, - 'mediagoblin/edit/edit_account.html', - {'user': user, - 'form': form}) - - if password_matches: - user.pw_hash = auth_lib.bcrypt_gen_password_hash( - request.POST['new_password']) - - user.save() - - messages.add_message(request, - messages.SUCCESS, - _("Account settings saved")) - return redirect(request, - 'mediagoblin.user_pages.user_home', - user=user.username) + form = forms.EditAccountForm(request.POST, + wants_comment_notification=user.get('wants_comment_notification')) + + if request.method == 'POST': + form_validated = form.validate() + + #if the user has not filled in the new or old password fields + if not form.new_password.data and not form.old_password.data: + if form.wants_comment_notification.validate(form): + user.wants_comment_notification = \ + form.wants_comment_notification.data + user.save() + messages.add_message(request, + messages.SUCCESS, + _("Account settings saved")) + return redirect(request, + 'mediagoblin.user_pages.user_home', + user=user.username) + + #so the user has filled in one or both of the password fields + else: + if form_validated: + password_matches = auth_lib.bcrypt_check_password( + form.old_password.data, + user.pw_hash) + if password_matches: + #the entire form validates and the password matches + user.pw_hash = auth_lib.bcrypt_gen_password_hash( + form.new_password.data) + user.wants_comment_notification = \ + form.wants_comment_notification.data + user.save() + messages.add_message(request, + messages.SUCCESS, + _("Account settings saved")) + return redirect(request, + 'mediagoblin.user_pages.user_home', + user=user.username) + else: + form.old_password.errors.append(_('Wrong password')) return render_to_response( request, 'mediagoblin/edit/edit_account.html', {'user': user, 'form': form}) + diff --git a/mediagoblin/templates/mediagoblin/user_pages/comment_email.txt b/mediagoblin/templates/mediagoblin/user_pages/comment_email.txt new file mode 100644 index 00000000..7bf649cf --- /dev/null +++ b/mediagoblin/templates/mediagoblin/user_pages/comment_email.txt @@ -0,0 +1,26 @@ +{# +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +-#} + +{% trans username=username, comment_author=comment_author -%} + +Hi {{ username }}, +{{ comment_author }} commented on your post ({{ comment_url }}) at GNU MediaGoblin: +{% endtrans %} +{{ comment_content }} + +GNU MediaGoblin diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py index 398a5d25..6c4020da 100644 --- a/mediagoblin/tests/test_edit.py +++ b/mediagoblin/tests/test_edit.py @@ -37,6 +37,7 @@ def test_change_password(test_app): '/edit/account/', { 'old_password': 'toast', 'new_password': '123456', + 'wants_comment_notification': 'y' }) # test_user has to be fetched again in order to have the current values diff --git a/mediagoblin/user_pages/lib.py b/mediagoblin/user_pages/lib.py new file mode 100644 index 00000000..79f9a2b2 --- /dev/null +++ b/mediagoblin/user_pages/lib.py @@ -0,0 +1,54 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from mediagoblin.tools.mail import send_email +from mediagoblin.tools.template import render_template +from mediagoblin.tools.translate import pass_to_ugettext as _ +from mediagoblin import mg_globals + +def send_comment_email(user, comment, media, request): + """ + Sends comment email to user when a comment is made on their media. + + Args: + - user: the user object to whom the email is sent + - comment: the comment object referencing user's media + - media: the media object the comment is about + - request: the request + """ + + comment_url = request.urlgen( + 'mediagoblin.user_pages.media_home.view_comment', + comment = comment._id, + user = media.get_uploader.username, + media = media.slug_or_id, + qualified = True) + '#comment' + + comment_author = comment.get_author['username'] + + rendered_email = render_template( + request, 'mediagoblin/user_pages/comment_email.txt', + {'username':user.username, + 'comment_author':comment_author, + 'comment_content':comment.content, + 'comment_url':comment_url}) + + send_email( + mg_globals.app_config['email_sender_address'], + [user.email], + 'GNU MediaGoblin - {comment_author} '.format( + comment_author=comment_author) + _('commented on your post'), + rendered_email) diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index e69a6ffe..4c5e7e9a 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -23,6 +23,7 @@ from mediagoblin.tools.translate import pass_to_ugettext as _ from mediagoblin.tools.pagination import Pagination from mediagoblin.tools.files import delete_media_files from mediagoblin.user_pages import forms as user_forms +from mediagoblin.user_pages.lib import send_comment_email from mediagoblin.decorators import (uses_pagination, get_user_media_entry, require_active_login, user_may_delete_media) @@ -158,6 +159,12 @@ def media_post_comment(request, media): request, messages.SUCCESS, _('Your comment has been posted!')) + media_uploader = media.get_uploader + #don't send email if you comment on your own post + if (comment.author != media_uploader and + media_uploader['wants_comment_notification']): + send_comment_email(media_uploader, comment, media, request) + return exc.HTTPFound( location=media.url_for_self(request.urlgen)) |