Skip to content

Commit

Permalink
Keep track of reasons for changes.
Browse files Browse the repository at this point in the history
JIRA: PDC-923
  • Loading branch information
simozhan committed Sep 7, 2015
1 parent 87d27e8 commit cef6ec5
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pdc/apps/changeset/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ChangesetFilterSet(django_filters.FilterSet):
widget=widgets.DateTimeInput)
changed_until = django_filters.MethodFilter(action='filter_committed_until',
widget=widgets.DateTimeInput)
pdc_change_comment = django_filters.CharFilter(name="pdc_change_comment", lookup_type="contains")

@value_is_not_empty
def filter_author(self, qs, value):
Expand All @@ -42,4 +43,4 @@ def filter_committed_until(self, qs, value):

class Meta:
model = models.Changeset
fields = ('author', 'resource', 'changed_since', 'changed_until')
fields = ('author', 'resource', 'changed_since', 'changed_until', 'pdc_change_comment')
8 changes: 8 additions & 0 deletions pdc/apps/changeset/fixtures/tests/changeset.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
"committed_on": "2015-02-03T05:51:17.262Z"
}
},
{
"pk": 3,
"model": "changeset.changeset",
"fields": {
"committed_on": "2015-01-03T10:00:00.000Z",
"pdc_change_comment": "Test for pdc change comment"
}
},
{
"pk": 1,
"model": "changeset.change",
Expand Down
3 changes: 2 additions & 1 deletion pdc/apps/changeset/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def process_view(self, request, view_func, view_args, view_kwargs):
logger.debug("Start write request on the view %s." % view_func.__name__)
try:
with transaction.atomic():
request.changeset = models.Changeset(author=user)
pdc_change_comment = request.META.get("HTTP_PDC_CHANGE_COMMENT", None)
request.changeset = models.Changeset(author=user, pdc_change_comment=pdc_change_comment)
response = view_func(request, *view_args, **view_kwargs)
# response.exception=True means there is an error occurs.
if getattr(response, 'exception', 0) or (
Expand Down
19 changes: 19 additions & 0 deletions pdc/apps/changeset/migrations/0003_changeset_pdc_change_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('changeset', '0002_auto_20150525_1410'),
]

operations = [
migrations.AddField(
model_name='changeset',
name='pdc_change_comment',
field=models.TextField(null=True, blank=True),
),
]
1 change: 1 addition & 0 deletions pdc/apps/changeset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Changeset(models.Model):
"""
author = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
committed_on = models.DateTimeField(auto_now_add=True)
pdc_change_comment = models.TextField(null=True, blank=True)

def __init__(self, *args, **kwargs):
self.tmp_changes = []
Expand Down
2 changes: 1 addition & 1 deletion pdc/apps/changeset/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class ChangesetSerializer(StrictSerializerMixin, serializers.ModelSerializer):

class Meta:
model = Changeset
fields = ('id', 'author', 'committed_on', 'changes')
fields = ('id', 'author', 'committed_on', 'changes', 'pdc_change_comment')
28 changes: 25 additions & 3 deletions pdc/apps/changeset/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def setUp(self):

def test_passing_arguments(self):
self.request.user.is_authenticated = lambda: False
self.request.META.get = lambda x, y: y
func = Mock()
func.__name__ = "Mock"
func.return_value = 123
Expand All @@ -31,23 +32,25 @@ def test_passing_arguments(self):
self.assertTrue(func.called)
self.assertEqual(ret, 123)
self.assertEqual(func.call_args, call(self.request, 1, 2, 3, arg='val'))
self.assertEqual(changeset.mock_calls, [call(author=None), call().commit()])
self.assertEqual(changeset.mock_calls, [call(author=None, pdc_change_comment=None), call().commit()])

def test_no_commit_with_exception(self):
self.request.user.is_authenticated = lambda: False
self.request.META.get = lambda x, y: y
func = Mock()
func.__name__ = "Mock"
func.side_effect = Exception("Boom!")
changeset_logger.error = Mock()
with patch("pdc.apps.changeset.models.Changeset") as changeset:
self.assertRaises(Exception, self.cm.process_view, self.request, func, [], {})
self.assertTrue(func.called)
self.assertEqual(changeset.mock_calls, [call(author=None)])
self.assertEqual(changeset.mock_calls, [call(author=None, pdc_change_comment=None)])
self.assertTrue(changeset_logger.error.called)


class ChangesetRESTTestCase(APITestCase):
fixtures = ['pdc/apps/changeset/fixtures/tests/changeset.json', ]
fixtures = ['pdc/apps/changeset/fixtures/tests/changeset.json',
"pdc/apps/component/fixtures/tests/bugzilla_component.json"]

def test_get(self):
url = reverse('changeset-detail', args=[1])
Expand Down Expand Up @@ -98,3 +101,22 @@ def test_query_with_incorrect_datetimeformat(self):
response = self.client.get(url + '?changed_since=20150203T02:55:18', format='json')

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_query_with_pdc_change_comment(self):
url = reverse('changeset-list')
response = self.client.get(url + '?pdc_change_comment=change', format='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 1)

def test_create_with_pdc_change_comment(self):
url = reverse('bugzillacomponent-list')
data = {'name': 'bin', 'parent_pk': 1}
extra = {'HTTP_PDC_CHANGE_COMMENT': 'New bugzilla component'}
response = self.client.post(url, data, format='json', **extra)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

url1 = reverse('changeset-list')
response1 = self.client.get(url1 + '?pdc_change_comment=new', format='json')
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 1)
2 changes: 2 additions & 0 deletions pdc/apps/common/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
**Please remember to use your token as HTTP header for every requests that need authentication.**
If you want to record the reason for change, you can add Header (-H "PDC-Change-Comment: reasonforchange") in request.
Responses are available in JSON format.
**NOTE:** in order to use secure HTTPS connections, you'd better to add server's certificate as trusted.
Expand Down
6 changes: 5 additions & 1 deletion pdc_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"token": None,
"insecure": True,
"develop": False,
"comment": None,
}

GLOBAL_CONFIG_FILE = '/etc/pdc/client_config.json'
Expand Down Expand Up @@ -76,7 +77,7 @@ def obtain_token(pdc):
raise Exception('Could not obtain token from any known URL.')


def pdc_client(url, token=None, insecure=False, develop=False, debug=False):
def pdc_client(url, token=None, insecure=False, develop=False, debug=False, comment=None):
session = requests.Session()

if not develop:
Expand Down Expand Up @@ -106,4 +107,7 @@ def pdc_client(url, token=None, insecure=False, develop=False, debug=False):
token = obtain_token(pdc)
session.headers["Authorization"] = "Token %s" % token

if comment:
session.headers["PDC-Change-Comment"] = comment

return pdc, session
3 changes: 3 additions & 0 deletions pdc_client/bin/pdc_client
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ if __name__ == "__main__":
parser.add_option("--debug", help="Show request headers and path for "
"debugging.",
action="store_true", default=False)
parser.add_option("-c", "--comment", help="Reasons for the PDC change.")
options, args = parser.parse_args()

if len(args):
Expand Down Expand Up @@ -166,6 +167,8 @@ if __name__ == "__main__":
set_option("insecure", config.get(CONFIG_INSECURE_KEY_NAME))
set_option("develop", config.get(CONFIG_DEVELOP_KEY_NAME))
set_option("token", config.get(CONFIG_TOKEN_KEY_NAME))
if options.comment:
set_option("comment", options.comment)

data = load_data(options)

Expand Down

0 comments on commit cef6ec5

Please sign in to comment.