• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

allura


Commit MetaInfo

Revisionfc15e4fe6571d61a0f9bf29105ca9b1482bebd02 (tree)
Time2012-07-13 21:41:52
AuthorIgor Bondarenko <jetmind2@gmai...>
CommiterIgor Bondarenko

Log Message

[#4534] ticket:114 Add log_if_changed helper function

Change Summary

Incremental Difference

--- a/Allura/allura/controllers/project.py
+++ b/Allura/allura/controllers/project.py
@@ -479,42 +479,32 @@ class NeighborhoodAdminController(object):
479479 def update(self, name=None, css=None, homepage=None, project_template=None, icon=None, **kw):
480480 nbhd = self.neighborhood
481481 c.project = nbhd.neighborhood_project
482- if nbhd.name != name:
483- M.AuditLog.log('change neighborhood name to %s', name)
484- nbhd.name = name
482+ h.log_if_changed(nbhd, 'name', name,
483+ 'change neighborhood name to %s' % name)
485484 nbhd_redirect = kw.pop('redirect', '')
486- if nbhd.redirect != nbhd_redirect:
487- M.AuditLog.log('change neighborhood redirect to %s', nbhd_redirect)
488- nbhd.redirect = nbhd_redirect
489- if nbhd.homepage != homepage:
490- M.AuditLog.log('change neighborhood homepage to %s', homepage)
491- nbhd.homepage = homepage
492- if nbhd.css != css:
493- M.AuditLog.log('change neighborhood css to %s', css)
494- nbhd.css = css
495- if nbhd.project_template != project_template:
496- M.AuditLog.log('change neighborhood project template to %s',
497- project_template)
498- nbhd.project_template = project_template
485+ h.log_if_changed(nbhd, 'redirect', nbhd_redirect,
486+ 'change neighborhood redirect to %s' % nbhd_redirect)
487+ h.log_if_changed(nbhd, 'homepage', homepage,
488+ 'change neighborhood homepage to %s' % homepage)
489+ h.log_if_changed(nbhd, 'css', css,
490+ 'change neighborhood css to %s' % css)
491+ h.log_if_changed(nbhd, 'project_template', project_template,
492+ 'change neighborhood project template to %s'
493+ % project_template)
499494 allow_browse = kw.get('allow_browse', False)
500- if nbhd.allow_browse != allow_browse:
501- M.AuditLog.log('change neighborhood allow browse to %s',
502- allow_browse)
503- nbhd.allow_browse = allow_browse
495+ h.log_if_changed(nbhd, 'allow_browse', allow_browse,
496+ 'change neighborhood allow browse to %s'
497+ % allow_browse)
504498 show_title = kw.get('show_title', False)
505- if nbhd.show_title != show_title:
506- M.AuditLog.log('change neighborhood show title to %s',
507- show_title)
508- nbhd.show_title = show_title
499+ h.log_if_changed(nbhd, 'show_title', show_title,
500+ 'change neighborhood show title to %s' % show_title)
509501 project_list_url = kw.get('project_list_url', '')
510- if nbhd.project_list_url != project_list_url:
511- M.AuditLog.log('change neighborhood project list url to %s',
512- project_list_url)
513- nbhd.project_list_url = project_list_url
502+ h.log_if_changed(nbhd, 'project_list_url', project_list_url,
503+ 'change neighborhood project list url to %s'
504+ % project_list_url)
514505 tracking_id = kw.get('tracking_id', '')
515- if tracking_id != nbhd.tracking_id:
516- M.AuditLog.log('update neighborhood tracking_id')
517- nbhd.tracking_id = tracking_id
506+ h.log_if_changed(nbhd, 'tracking_id', tracking_id,
507+ 'update neighborhood tracking_id')
518508 if icon is not None and icon != '':
519509 if self.neighborhood.icon:
520510 self.neighborhood.icon.delete()
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -621,3 +621,13 @@ prefixes are used (Mebi, Gibi).
621621 if bytes < unit:
622622 return '%.1f %s' % ((base * bytes / unit), prefix)
623623 return '%.1f %s' % ((base * bytes / unit), prefix)
624+
625+
626+def log_if_changed(artifact, attr, new_val, message):
627+ """Set `artifact.attr` to `new_val` if changed. Add AuditLog record."""
628+ from allura import model as M
629+ if not hasattr(artifact, attr):
630+ return
631+ if getattr(artifact, attr) != new_val:
632+ M.AuditLog.log(message)
633+ setattr(artifact, attr, new_val)
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -1,4 +1,5 @@
11 from os import path
2+from mock import Mock, patch
23
34 from pylons import c
45 from nose.tools import eq_, assert_equals
@@ -125,3 +126,27 @@ def test_render_any_markup_formatting():
125126 assert_equals(h.render_any_markup('README.md', '### foo\n<script>alert(1)</script> bar'),
126127 '<h3>foo</h3>\n<p>&lt;script&gt;alert(1)&lt;/script&gt; bar</p>')
127128
129+
130+class AuditLogMock(Mock):
131+ logs = list()
132+
133+ @classmethod
134+ def log(cls, message):
135+ cls.logs.append(message)
136+
137+
138+@patch('allura.model.AuditLog', new=AuditLogMock)
139+def test_log_if_changed():
140+ artifact = Mock()
141+ artifact.value = 'test'
142+ # change
143+ h.log_if_changed(artifact, 'value', 'changed', 'updated value')
144+ assert artifact.value == 'changed'
145+ assert len(AuditLogMock.logs) == 1
146+ assert AuditLogMock.logs[0] == 'updated value'
147+
148+ # don't change
149+ h.log_if_changed(artifact, 'value', 'changed', 'updated value')
150+ assert artifact.value == 'changed'
151+ assert len(AuditLogMock.logs) == 1
152+ assert AuditLogMock.logs[0] == 'updated value'