borg-web/borgweb/borg/models/repo.py
George Lacey 2aa5917691 Move models to separate files
- Implement last backup method
2021-05-06 13:29:20 +01:00

54 lines
1.7 KiB
Python

from django.db import models
from datetime import datetime
class Repo(models.Model):
fingerprint = models.TextField()
location = models.TextField()
last_modified = models.DateTimeField()
class Meta:
db_table = 'repo'
def last_backup(self):
latest = self.archives.order_by('-start')[0].start.replace(tzinfo=None)
seconds_since = int((datetime.utcnow() - latest).total_seconds())
return f"{self.seconds_to_string(seconds_since)} ago"
@staticmethod
def seconds_to_string(seconds: int, short=False, truncate=False):
seconds = int(seconds)
increments = [('year', 'yr', 31557600),
('week', 'wk', 604800),
('day', 'day', 86400),
('hour', 'hr', 3600),
('minute', 'min', 60),
('second', 'sec', 1)]
if seconds == 0:
if short:
return f"0 {increments[-1][1]}s"
else:
return f"0 {increments[-1][0]}s"
time_string = ""
remainder = seconds
for st, sst, s in increments:
if remainder == 0:
break
if short:
st = sst
if remainder < s or remainder == 0:
continue
else:
exact, remainder = divmod(remainder, s)
if exact > 1:
time_string += f"{exact} {st}s, "
else:
time_string += f"{exact} {st}, "
if truncate:
break
return time_string.strip().strip(',')[::-1].replace(' ,', ' dna ', 1)[::-1] # lmao