From a8f1ac6a62e1405840bed3865b2ec65698188c6e Mon Sep 17 00:00:00 2001 From: Mark Reynolds Date: Mar 06 2017 15:35:57 +0000 Subject: Add option to custom field for email notification --- diff --git a/alembic/versions/11470abae0d6_.py b/alembic/versions/11470abae0d6_.py new file mode 100644 index 0000000..d5d8691 --- /dev/null +++ b/alembic/versions/11470abae0d6_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: 11470abae0d6 +Revises: 987edda096f5 +Create Date: 2017-03-04 10:19:14.842910 + +""" + +# revision identifiers, used by Alembic. +revision = '11470abae0d6' +down_revision = '987edda096f5' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ''' Add a column to record if the custom field should trigger a email + notification. + ''' + op.add_column( + 'issue_keys', + sa.Column('key_notify', sa.Boolean, default=False, nullable=False) + ) + + +def downgrade(): + ''' Remove the key_notify column. + ''' + op.drop_column('issue_keys', 'key_notify') diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 69e2686..66f57ba 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -3730,7 +3730,7 @@ def save_report(session, repo, name, url, username): session.add(repo) -def set_custom_key_fields(session, project, fields, types, data): +def set_custom_key_fields(session, project, fields, types, data, notify): """ Set or update the custom key fields of a project with the values provided. "data" is currently only used for lists """ @@ -3750,16 +3750,23 @@ def set_custom_key_fields(session, project, fields, types, data): for item in data[idx].split(',') ] + if notify[idx] == "on": + notify_flag = True + else: + notify_flag = False + if key in current_keys: issuekey = current_keys[key] issuekey.key_type = types[idx] issuekey.data = data[idx] + issuekey.key_notify = notify_flag else: issuekey = model.IssueKeys( project_id=project.id, name=key, key_type=types[idx], - data=data[idx] + data=data[idx], + key_notify=notify_flag ) session.add(issuekey) diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 34c2bc6..2167167 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -1141,6 +1141,7 @@ class IssueKeys(BASE): name = sa.Column(sa.Text(), nullable=False) key_type = sa.Column(sa.String(255), nullable=False) key_data = sa.Column(sa.Text()) + key_notify = sa.Column(sa.Boolean, default=False, nullable=False) __table_args__ = (sa.UniqueConstraint('project_id', 'name'),) diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index 83c6ecb..7da3079 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -434,6 +434,35 @@ The status of the issue: `%s` of project: `%s` has been updated to: %s by %s. ) +def notify_meta_change_issue(issue, user, msg): + ''' Notify that a custom field changed + ''' + text = u""" +`%s` updated issue. + +%s + +%s +""" % (user.username, + msg, + _build_url( + pagure.APP.config['APP_URL'], + _fullname_to_url(issue.project.fullname), + 'issue', + issue.id)) + mail_to = _get_emails_for_obj(issue) + uid = time.mktime(datetime.datetime.now().timetuple()) + send_email( + text, + 'Issue #%s `%s`' % (issue.id, issue.title), + ','.join(mail_to), + mail_id='%s/close/%s' % (issue.mail_id, uid), + in_reply_to=issue.mail_id, + project_name=issue.project.fullname, + user_from=user.fullname or user.user, + ) + + def notify_assigned_request(request, new_assignee, user): ''' Notify the people following a pull-request that the assignee changed. ''' diff --git a/pagure/templates/settings.html b/pagure/templates/settings.html index 713261b..2ff690f 100644 --- a/pagure/templates/settings.html +++ b/pagure/templates/settings.html @@ -792,7 +792,7 @@ {{ tag_form.csrf_token }}
-
+
Fields
@@ -801,11 +801,14 @@
Field Values (Lists only)
+
+ Notify +
{% for field in repo.issue_keys or [dict(key_type="", name="")] | sort %}
-
+
@@ -829,6 +832,13 @@
+
+ +
{% endfor %}
diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index 82457bc..215f4c2 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -289,6 +289,10 @@ def update_issue(repo, issueid, username=None, namespace=None): msg = pagure.lib.set_custom_key_value( SESSION, issue, key, value) + if key.key_notify and msg is not None: + # Custom field changed that is set for notifications + pagure.lib.notify.notify_meta_change_issue( + issue, flask.g.fas_user, msg) if msg: messages.add(msg) diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index e4f70a7..a6e0ee5 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -2562,10 +2562,15 @@ def update_custom_keys(repo, username=None, namespace=None): custom_keys_data = [ w.strip() for w in flask.request.form.getlist('custom_keys_data') ] + custom_keys_notify = [] + for idx in range(len(custom_keys)): + custom_keys_notify.append(str( + flask.request.form.get('custom_keys_notify-%s' % (idx + 1)))) try: msg = pagure.lib.set_custom_key_fields( - SESSION, repo, custom_keys, custom_keys_type, custom_keys_data) + SESSION, repo, custom_keys, custom_keys_type, custom_keys_data, + custom_keys_notify) SESSION.commit() flask.flash(msg) except SQLAlchemyError as err: # pragma: no cover diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py index ba93efd..2fc5eca 100644 --- a/tests/test_pagure_flask_api_issue.py +++ b/tests/test_pagure_flask_api_issue.py @@ -1952,7 +1952,8 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.session, repo, ['bugzilla', 'upstream', 'reviewstatus'], ['link', 'boolean', 'list'], - ['unused data for non-list type', '', 'ack, nack , needs review']) + ['unused data for non-list type', '', 'ack, nack , needs review'], + [None, None, None]) self.session.commit() self.assertEqual(msg, 'List of custom fields updated') diff --git a/tests/test_pagure_flask_ui_issues.py b/tests/test_pagure_flask_ui_issues.py index b52b350..c788243 100644 --- a/tests/test_pagure_flask_ui_issues.py +++ b/tests/test_pagure_flask_ui_issues.py @@ -304,7 +304,8 @@ class PagureFlaskIssuestests(tests.Modeltests): project=repo, fields=['test1'], types=['text'], - data=[None] + data=[None], + notify=[None] ) self.session.commit() self.assertEqual(msg, 'List of custom fields updated') @@ -690,7 +691,8 @@ class PagureFlaskIssuestests(tests.Modeltests): project=repo, fields=['test1'], types=['list'], - data=[None] + data=[None], + notify=[None] ) self.session.commit() self.assertEqual(msg, 'List of custom fields updated') diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index c73df6c..c6336a1 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -1224,7 +1224,7 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn( '''
-
+
@@ -1240,6 +1240,10 @@ class PagureFlaskRepotests(tests.Modeltests):
+
+ +
''', output.data) def test_view_forks(self):