-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from DanSheps/develop-3.7
Fix issue with annotations
- Loading branch information
Showing
6 changed files
with
98 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from django.db import models | ||
from django.urls import reverse | ||
|
||
from netbox_config_backup.choices import FileTypeChoices | ||
from netbox_config_backup.models import Backup | ||
from netbox_config_backup.models.abstract import BigIDModel | ||
|
||
|
||
class BackupCommit(BigIDModel): | ||
sha = models.CharField(max_length=64) | ||
time = models.DateTimeField() | ||
|
||
def __str__(self): | ||
return self.sha | ||
|
||
|
||
class BackupObject(BigIDModel): | ||
sha = models.CharField(max_length=64, unique=True) | ||
|
||
def __str__(self): | ||
return f'{self.sha}' | ||
|
||
|
||
class BackupFile(BigIDModel): | ||
backup = models.ForeignKey(to=Backup, on_delete=models.CASCADE, null=False, blank=False, related_name='files') | ||
type = models.CharField(max_length=10, choices=FileTypeChoices, null=False, blank=False) | ||
|
||
class Meta: | ||
unique_together = ['backup', 'type'] | ||
|
||
def __str__(self): | ||
return f'{self.name}.{self.type}' | ||
|
||
@property | ||
def name(self): | ||
return f'{self.backup.uuid}' | ||
|
||
@property | ||
def path(self): | ||
return f'{self.name}.{self.type}' | ||
|
||
|
||
class BackupCommitTreeChange(BigIDModel): | ||
backup = models.ForeignKey(to=Backup, on_delete=models.CASCADE, null=False, blank=False, related_name='changes') | ||
file = models.ForeignKey(to=BackupFile, on_delete=models.CASCADE, null=False, blank=False, related_name='changes') | ||
|
||
commit = models.ForeignKey(to=BackupCommit, on_delete=models.PROTECT, related_name='changes') | ||
type = models.CharField(max_length=10) | ||
old = models.ForeignKey(to=BackupObject, on_delete=models.PROTECT, related_name='previous', null=True) | ||
new = models.ForeignKey(to=BackupObject, on_delete=models.PROTECT, related_name='changes', null=True) | ||
|
||
def __str__(self): | ||
return f'{self.commit.sha}-{self.type}' | ||
|
||
def filename(self): | ||
return f'{self.backup.uuid}.{self.type}' | ||
|
||
def get_absolute_url(self): | ||
return reverse('plugins:netbox_config_backup:backup_config', kwargs={'backup': self.backup.pk, 'current': self.pk}) | ||
|
||
@property | ||
def previous(self): | ||
return self.backup.changes.filter(file__type=self.file.type, commit__time__lt=self.commit.time).last() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.db import models | ||
|
||
from extras.choices import JobResultStatusChoices | ||
from utilities.querysets import RestrictedQuerySet | ||
|
||
|
||
|
||
class BackupQuerySet(RestrictedQuerySet): | ||
def default_annotate(self): | ||
from netbox_config_backup.models import BackupJob, BackupCommitTreeChange | ||
|
||
return self.prefetch_related('jobs').annotate( | ||
last_backup=models.Subquery( | ||
BackupJob.objects.filter(backup=models.OuterRef('id'), status=JobResultStatusChoices.STATUS_COMPLETED).order_by('completed').values('completed')[:1] | ||
), | ||
next_attempt=models.Subquery( | ||
BackupJob.objects.filter(backup=models.OuterRef('id'), status__in=['pending', 'running']).order_by('scheduled').values('scheduled')[:1] | ||
), | ||
last_change=models.Subquery( | ||
BackupCommitTreeChange.objects.filter(backup=models.OuterRef('id')).values('commit__time')[:1] | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters