|
| 1 | +# Copyright 2026 Marcus Furlong <furlongm@gmail.com> |
| 2 | +# |
| 3 | +# This file is part of Patchman. |
| 4 | +# |
| 5 | +# Patchman is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, version 3 only. |
| 8 | +# |
| 9 | +# Patchman is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with Patchman. If not, see <http://www.gnu.org/licenses/> |
| 16 | + |
| 17 | +from django.db.models.signals import m2m_changed |
| 18 | +from django.dispatch import receiver |
| 19 | + |
| 20 | +from hosts.models import Host |
| 21 | + |
| 22 | + |
| 23 | +@receiver(m2m_changed, sender=Host.packages.through) |
| 24 | +def update_host_packages_count(sender, instance, action, **kwargs): |
| 25 | + """Update packages_count when Host.packages M2M changes.""" |
| 26 | + if action in ('post_add', 'post_remove', 'post_clear'): |
| 27 | + instance.packages_count = instance.packages.count() |
| 28 | + instance.save(update_fields=['packages_count']) |
| 29 | + |
| 30 | + |
| 31 | +@receiver(m2m_changed, sender=Host.updates.through) |
| 32 | +def update_host_updates_count(sender, instance, action, **kwargs): |
| 33 | + """Update sec_updates_count and bug_updates_count when Host.updates M2M changes.""" |
| 34 | + if action in ('post_add', 'post_remove', 'post_clear'): |
| 35 | + instance.sec_updates_count = instance.updates.filter(security=True).count() |
| 36 | + instance.bug_updates_count = instance.updates.filter(security=False).count() |
| 37 | + instance.save(update_fields=['sec_updates_count', 'bug_updates_count']) |
| 38 | + |
| 39 | + |
| 40 | +@receiver(m2m_changed, sender=Host.errata.through) |
| 41 | +def update_host_errata_count(sender, instance, action, **kwargs): |
| 42 | + """Update errata_count when Host.errata M2M changes.""" |
| 43 | + if action in ('post_add', 'post_remove', 'post_clear'): |
| 44 | + instance.errata_count = instance.errata.count() |
| 45 | + instance.save(update_fields=['errata_count']) |
0 commit comments