2019-04-17 07:49:18 +00:00
|
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
2019-04-02 20:36:41 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
from ..util import parse_date
|
|
|
|
from ..index.schema import Link
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2019-04-17 07:49:18 +00:00
|
|
|
|
|
|
|
class Page(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
|
|
|
|
url = models.URLField(unique=True)
|
|
|
|
timestamp = models.CharField(unique=True, max_length=32, null=True, default=None)
|
|
|
|
|
|
|
|
title = models.CharField(max_length=128, null=True, default=None)
|
|
|
|
tags = models.CharField(max_length=256, null=True, default=None)
|
|
|
|
|
|
|
|
added = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(null=True, default=None)
|
|
|
|
# bookmarked = models.DateTimeField()
|
|
|
|
|
2019-04-17 07:50:41 +00:00
|
|
|
keys = ('url', 'timestamp', 'title', 'tags', 'updated')
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f'[{self.timestamp}] {self.url[:64]} ({self.title[:64]})'
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f'[{self.timestamp}] {self.url[:64]} ({self.title[:64]})'
|
|
|
|
|
2019-04-17 07:49:18 +00:00
|
|
|
@classmethod
|
|
|
|
def from_json(cls, info: dict):
|
2019-04-17 07:50:41 +00:00
|
|
|
info = {k: v for k, v in info.items() if k in cls.keys}
|
2019-04-17 07:49:18 +00:00
|
|
|
return cls(**info)
|
|
|
|
|
|
|
|
def as_json(self, *args) -> dict:
|
2019-04-17 07:50:41 +00:00
|
|
|
args = args or self.keys
|
2019-04-17 07:49:18 +00:00
|
|
|
return {
|
|
|
|
key: getattr(self, key)
|
|
|
|
for key in args
|
|
|
|
}
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
|
|
def as_link(self) -> Link:
|
|
|
|
return Link.from_json(self.as_json())
|
|
|
|
|
2019-04-23 01:40:42 +00:00
|
|
|
@property
|
|
|
|
def bookmarked(self):
|
|
|
|
return parse_date(self.timestamp)
|
|
|
|
|
2019-04-22 17:20:19 +00:00
|
|
|
@property
|
|
|
|
def is_archived(self):
|
|
|
|
return self.as_link().is_archived
|
|
|
|
|
|
|
|
@property
|
|
|
|
def num_outputs(self):
|
|
|
|
return self.as_link().num_outputs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url_hash(self):
|
|
|
|
return self.as_link().url_hash
|
|
|
|
|
|
|
|
@property
|
|
|
|
def base_url(self):
|
|
|
|
return self.as_link().base_url
|
2019-05-01 03:13:04 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def link_dir(self):
|
|
|
|
return self.as_link().link_dir
|