allura
Revision | fc15e4fe6571d61a0f9bf29105ca9b1482bebd02 (tree) |
---|---|
Time | 2012-07-13 21:41:52 |
Author | Igor Bondarenko <jetmind2@gmai...> |
Commiter | Igor Bondarenko |
[#4534] ticket:114 Add log_if_changed helper function
@@ -479,42 +479,32 @@ class NeighborhoodAdminController(object): | ||
479 | 479 | def update(self, name=None, css=None, homepage=None, project_template=None, icon=None, **kw): |
480 | 480 | nbhd = self.neighborhood |
481 | 481 | 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) | |
485 | 484 | 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) | |
499 | 494 | 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) | |
504 | 498 | 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) | |
509 | 501 | 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) | |
514 | 505 | 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') | |
518 | 508 | if icon is not None and icon != '': |
519 | 509 | if self.neighborhood.icon: |
520 | 510 | self.neighborhood.icon.delete() |
@@ -621,3 +621,13 @@ prefixes are used (Mebi, Gibi). | ||
621 | 621 | if bytes < unit: |
622 | 622 | return '%.1f %s' % ((base * bytes / unit), prefix) |
623 | 623 | 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) |
@@ -1,4 +1,5 @@ | ||
1 | 1 | from os import path |
2 | +from mock import Mock, patch | |
2 | 3 | |
3 | 4 | from pylons import c |
4 | 5 | from nose.tools import eq_, assert_equals |
@@ -125,3 +126,27 @@ def test_render_any_markup_formatting(): | ||
125 | 126 | assert_equals(h.render_any_markup('README.md', '### foo\n<script>alert(1)</script> bar'), |
126 | 127 | '<h3>foo</h3>\n<p><script>alert(1)</script> bar</p>') |
127 | 128 | |
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' |