From a04e021d8c86f3b17b12305001be0f0e0f3ad8cd Mon Sep 17 00:00:00 2001 From: farhaanbukhsh Date: Dec 14 2015 16:08:53 +0000 Subject: [PATCH 1/46] Add functionality to edit comment in a PR With this commit, we can now edit comment on the PR. To accomplish this we have added new endpoints in ui/fork.py and made changes in templates/_formhelper.html. This commits also adds a file pull_request_comment_update.html which is a form for editing comments. Function for the same functionality is added in pagure/lib/__init__.py. We also added two new fields in pull_request_comments table in DB: editor_id, edited_on. Corresponding changes in _formheper.html are included to display 'last edited by' in the UI. --- diff --git a/alembic/versions/15ea3c2cf83d_adding_column_to_store_edited_by_and_.py b/alembic/versions/15ea3c2cf83d_adding_column_to_store_edited_by_and_.py new file mode 100644 index 0000000..0cac560 --- /dev/null +++ b/alembic/versions/15ea3c2cf83d_adding_column_to_store_edited_by_and_.py @@ -0,0 +1,39 @@ +"""Adding column to store edited_by and edited_on a commnet + +Revision ID: 15ea3c2cf83d +Revises: 21f45b08d882 +Create Date: 2015-11-09 16:18:47.192088 + +""" + +# revision identifiers, used by Alembic. +revision = '15ea3c2cf83d' +down_revision = '21f45b08d882' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + + op.add_column( + 'pull_request_comments', + sa.Column( + 'editor_id', + sa.Integer, + sa.ForeignKey('users.id', onupdate='CASCADE'), + nullable=True) + ) + + op.add_column( + 'pull_request_comments', + sa.Column( + 'edited_on', + sa.DATETIME, + nullable=True) + ) + + +def downgrade(): + op.drop_column('pull_request_comments', 'editor_id') + op.drop_column('pull_request_comments', 'edited_on') diff --git a/pagure/forms.py b/pagure/forms.py index b20500e..2b52307 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -364,3 +364,11 @@ class DefaultBranchForm(wtf.Form): self.branches.choices = [ (branch, branch) for branch in kwargs['branches'] ] + +class EditCommentForm(wtf.Form): + """ Form to verify that comment is not empty + """ + update_comment = wtforms.TextAreaField( + 'Comment*', + [wtforms.validators.Required()] + ) diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 4d20db1..b6c1fc4 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -834,6 +834,43 @@ def add_pull_request_comment(session, request, commit, filename, row, return 'Comment added' +def edit_pull_request_comment(session, request, comment, user, + updated_comment, requestfolder, redis): + '''Edit a comment in the pull request''' + user_obj = __get_user(session, user) + comment.comment = updated_comment + comment.edited_on = datetime.datetime.utcnow() + comment.editor = user_obj + + session.add(comment) + # Make sure we won't have SQLAlchemy error before we continue + session.flush() + + pagure.lib.git.update_git( + request, repo=request.project, repofolder=requestfolder) + + pagure.lib.notify.log( + request.project, + topic='pull-request.comment.edited', + msg=dict( + pullrequest=request.to_json(public=True), + agent=user_obj.username, + ) + ) + + if redis: + redis.publish(request.uid, json.dumps({ + 'request_id': len(request.comments), + 'comment_updated': text2markdown(comment.comment), + 'comment_id': comment.id, + 'comment_editor': user_obj.user, + 'avatar_url': avatar_url(comment.user.user, size=16), + 'comment_date': comment.date_created.strftime('%Y-%m-%d %H:%M'), + })) + + return "Comment updated" + + def add_pull_request_flag(session, request, username, percent, comment, url, uid, user, requestfolder): ''' Add a flag to a pull-request. ''' diff --git a/pagure/lib/model.py b/pagure/lib/model.py index bbf51fa..257b08a 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -928,6 +928,15 @@ class PullRequestComment(BASE): date_created = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow) + editor_id = sa.Column( + sa.Integer, + sa.ForeignKey('users.id', onupdate='CASCADE'), + nullable=True) + + editor = relation('User', foreign_keys=[editor_id], + remote_side=[User.id]) + + edited_on = sa.Column(sa.DATETIME, nullable=True) user = relation('User', foreign_keys=[user_id], remote_side=[User.id], diff --git a/pagure/templates/_formhelper.html b/pagure/templates/_formhelper.html index 9d7c071..d5f8fb8 100644 --- a/pagure/templates/_formhelper.html +++ b/pagure/templates/_formhelper.html @@ -74,12 +74,25 @@