mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-14 07:04:27 -04:00
add API support for obj.pk .uuid .abid
Some checks failed
Build Debian package / build (push) Has been cancelled
Build Docker image / buildx (push) Has been cancelled
Build Homebrew package / build (push) Has been cancelled
Run linters / lint (push) Has been cancelled
Build Pip package / build (push) Has been cancelled
Run tests / python_tests (ubuntu-22.04, 3.11) (push) Has been cancelled
Run tests / docker_tests (push) Has been cancelled
Some checks failed
Build Debian package / build (push) Has been cancelled
Build Docker image / buildx (push) Has been cancelled
Build Homebrew package / build (push) Has been cancelled
Run linters / lint (push) Has been cancelled
Build Pip package / build (push) Has been cancelled
Run tests / python_tests (ubuntu-22.04, 3.11) (push) Has been cancelled
Run tests / docker_tests (push) Has been cancelled
This commit is contained in:
parent
a4cc10d7f8
commit
406f57031a
1 changed files with 78 additions and 23 deletions
|
@ -4,6 +4,7 @@ from uuid import UUID
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from django.db.models import Q
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
from ninja import Router, Schema, FilterSchema, Field, Query
|
from ninja import Router, Schema, FilterSchema, Field, Query
|
||||||
|
@ -20,23 +21,27 @@ router = Router(tags=['Core Models'])
|
||||||
### ArchiveResult #########################################################################
|
### ArchiveResult #########################################################################
|
||||||
|
|
||||||
class ArchiveResultSchema(Schema):
|
class ArchiveResultSchema(Schema):
|
||||||
pk: str
|
|
||||||
uuid: UUID
|
|
||||||
abid: str
|
abid: str
|
||||||
|
uuid: UUID
|
||||||
|
pk: str
|
||||||
|
modified: datetime
|
||||||
|
created: datetime
|
||||||
|
created_by_id: str
|
||||||
|
|
||||||
snapshot_abid: str
|
snapshot_abid: str
|
||||||
|
|
||||||
snapshot_url: str
|
snapshot_url: str
|
||||||
snapshot_tags: str
|
snapshot_tags: str
|
||||||
|
|
||||||
extractor: str
|
extractor: str
|
||||||
|
cmd_version: str
|
||||||
cmd: List[str]
|
cmd: List[str]
|
||||||
pwd: str
|
pwd: str
|
||||||
cmd_version: str
|
|
||||||
output: str
|
|
||||||
status: str
|
status: str
|
||||||
|
output: str
|
||||||
|
|
||||||
created: datetime
|
@staticmethod
|
||||||
|
def resolve_created_by_id(obj):
|
||||||
|
return str(obj.created_by_id)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_pk(obj):
|
def resolve_pk(obj):
|
||||||
|
@ -72,9 +77,9 @@ class ArchiveResultFilterSchema(FilterSchema):
|
||||||
# abid: Optional[str] = Field(None, q='abid')
|
# abid: Optional[str] = Field(None, q='abid')
|
||||||
|
|
||||||
search: Optional[str] = Field(None, q=['snapshot__url__icontains', 'snapshot__title__icontains', 'snapshot__tags__name__icontains', 'extractor', 'output__icontains'])
|
search: Optional[str] = Field(None, q=['snapshot__url__icontains', 'snapshot__title__icontains', 'snapshot__tags__name__icontains', 'extractor', 'output__icontains'])
|
||||||
snapshot_uuid: Optional[UUID] = Field(None, q='snapshot_uuid')
|
snapshot_uuid: Optional[UUID] = Field(None, q='snapshot_uuid__icontains')
|
||||||
snapshot_url: Optional[str] = Field(None, q='snapshot__url')
|
snapshot_url: Optional[str] = Field(None, q='snapshot__url__icontains')
|
||||||
snapshot_tag: Optional[str] = Field(None, q='snapshot__tags__name')
|
snapshot_tag: Optional[str] = Field(None, q='snapshot__tags__name__icontains')
|
||||||
|
|
||||||
status: Optional[str] = Field(None, q='status')
|
status: Optional[str] = Field(None, q='status')
|
||||||
output: Optional[str] = Field(None, q='output__icontains')
|
output: Optional[str] = Field(None, q='output__icontains')
|
||||||
|
@ -91,6 +96,7 @@ class ArchiveResultFilterSchema(FilterSchema):
|
||||||
@router.get("/archiveresults", response=List[ArchiveResultSchema])
|
@router.get("/archiveresults", response=List[ArchiveResultSchema])
|
||||||
@paginate
|
@paginate
|
||||||
def list_archiveresults(request, filters: ArchiveResultFilterSchema = Query(...)):
|
def list_archiveresults(request, filters: ArchiveResultFilterSchema = Query(...)):
|
||||||
|
"""List all ArchiveResult entries matching these filters."""
|
||||||
qs = ArchiveResult.objects.all()
|
qs = ArchiveResult.objects.all()
|
||||||
results = filters.filter(qs)
|
results = filters.filter(qs)
|
||||||
return results
|
return results
|
||||||
|
@ -98,8 +104,8 @@ def list_archiveresults(request, filters: ArchiveResultFilterSchema = Query(...)
|
||||||
|
|
||||||
@router.get("/archiveresult/{archiveresult_id}", response=ArchiveResultSchema)
|
@router.get("/archiveresult/{archiveresult_id}", response=ArchiveResultSchema)
|
||||||
def get_archiveresult(request, archiveresult_id: str):
|
def get_archiveresult(request, archiveresult_id: str):
|
||||||
archiveresult = get_object_or_404(ArchiveResult, id=archiveresult_id)
|
"""Get a specific ArchiveResult by abid, uuid, or pk."""
|
||||||
return archiveresult
|
return ArchiveResult.objects.get(Q(pk__icontains=archiveresult_id) | Q(abid__icontains=archiveresult_id) | Q(uuid__icontains=archiveresult_id))
|
||||||
|
|
||||||
|
|
||||||
# @router.post("/archiveresult", response=ArchiveResultSchema)
|
# @router.post("/archiveresult", response=ArchiveResultSchema)
|
||||||
|
@ -131,21 +137,30 @@ def get_archiveresult(request, archiveresult_id: str):
|
||||||
|
|
||||||
|
|
||||||
class SnapshotSchema(Schema):
|
class SnapshotSchema(Schema):
|
||||||
pk: str
|
|
||||||
uuid: UUID
|
|
||||||
abid: str
|
abid: str
|
||||||
|
uuid: UUID
|
||||||
|
pk: str
|
||||||
|
modified: datetime
|
||||||
|
created: datetime
|
||||||
|
created_by_id: str
|
||||||
|
|
||||||
url: str
|
url: str
|
||||||
tags: str
|
tags: str
|
||||||
title: Optional[str]
|
title: Optional[str]
|
||||||
timestamp: str
|
timestamp: str
|
||||||
bookmarked: datetime
|
|
||||||
added: datetime
|
|
||||||
updated: datetime
|
|
||||||
archive_path: str
|
archive_path: str
|
||||||
|
|
||||||
|
bookmarked: datetime
|
||||||
|
added: datetime
|
||||||
|
updated: Optional[datetime]
|
||||||
|
|
||||||
|
num_archiveresults: int
|
||||||
archiveresults: List[ArchiveResultSchema]
|
archiveresults: List[ArchiveResultSchema]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def resolve_created_by_id(obj):
|
||||||
|
return str(obj.created_by_id)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_pk(obj):
|
def resolve_pk(obj):
|
||||||
return str(obj.pk)
|
return str(obj.pk)
|
||||||
|
@ -162,6 +177,10 @@ class SnapshotSchema(Schema):
|
||||||
def resolve_tags(obj):
|
def resolve_tags(obj):
|
||||||
return obj.tags_str()
|
return obj.tags_str()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def resolve_num_archiveresults(obj, context):
|
||||||
|
return obj.archiveresult_set.all().distinct().count()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_archiveresults(obj, context):
|
def resolve_archiveresults(obj, context):
|
||||||
if context['request'].with_archiveresults:
|
if context['request'].with_archiveresults:
|
||||||
|
@ -170,33 +189,58 @@ class SnapshotSchema(Schema):
|
||||||
|
|
||||||
|
|
||||||
class SnapshotFilterSchema(FilterSchema):
|
class SnapshotFilterSchema(FilterSchema):
|
||||||
id: Optional[UUID] = Field(None, q='id')
|
abid: Optional[str] = Field(None, q='abid__icontains')
|
||||||
|
uuid: Optional[str] = Field(None, q='uuid__icontains')
|
||||||
|
pk: Optional[str] = Field(None, q='pk__icontains')
|
||||||
|
created_by_id: str = Field(None, q='created_by_id__icontains')
|
||||||
|
created__gte: datetime = Field(None, q='created__gte')
|
||||||
|
created__lt: datetime = Field(None, q='created__lt')
|
||||||
|
created: datetime = Field(None, q='created')
|
||||||
|
modified: datetime = Field(None, q='modified')
|
||||||
|
modified__gte: datetime = Field(None, q='modified__gte')
|
||||||
|
modified__lt: datetime = Field(None, q='modified__lt')
|
||||||
|
|
||||||
search: Optional[str] = Field(None, q=['url__icontains', 'title__icontains', 'tags__name__icontains'])
|
search: Optional[str] = Field(None, q=['url__icontains', 'title__icontains', 'tags__name__icontains', 'abid__icontains', 'uuid__icontains'])
|
||||||
url: Optional[str] = Field(None, q='url')
|
url: Optional[str] = Field(None, q='url')
|
||||||
tag: Optional[str] = Field(None, q='tags__name')
|
tag: Optional[str] = Field(None, q='tags__name')
|
||||||
title: Optional[str] = Field(None, q='title__icontains')
|
title: Optional[str] = Field(None, q='title__icontains')
|
||||||
|
|
||||||
timestamp: Optional[str] = Field(None, q='timestamp__startswith')
|
timestamp: Optional[str] = Field(None, q='timestamp__startswith')
|
||||||
|
|
||||||
added: Optional[datetime] = Field(None, q='added')
|
|
||||||
added__gte: Optional[datetime] = Field(None, q='added__gte')
|
added__gte: Optional[datetime] = Field(None, q='added__gte')
|
||||||
added__lt: Optional[datetime] = Field(None, q='added__lt')
|
added__lt: Optional[datetime] = Field(None, q='added__lt')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/snapshots", response=List[SnapshotSchema])
|
@router.get("/snapshots", response=List[SnapshotSchema])
|
||||||
@paginate
|
@paginate
|
||||||
def list_snapshots(request, filters: SnapshotFilterSchema = Query(...), with_archiveresults: bool=True):
|
def list_snapshots(request, filters: SnapshotFilterSchema = Query(...), with_archiveresults: bool=True):
|
||||||
|
"""List all Snapshot entries matching these filters."""
|
||||||
request.with_archiveresults = with_archiveresults
|
request.with_archiveresults = with_archiveresults
|
||||||
|
|
||||||
qs = Snapshot.objects.all()
|
qs = Snapshot.objects.all()
|
||||||
results = filters.filter(qs)
|
results = filters.filter(qs)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
@router.get("/snapshot/{snapshot_uuid}", response=SnapshotSchema)
|
@router.get("/snapshot/{snapshot_id}", response=SnapshotSchema)
|
||||||
def get_snapshot(request, snapshot_uuid: str, with_archiveresults: bool=True):
|
def get_snapshot(request, snapshot_id: str, with_archiveresults: bool=True):
|
||||||
|
"""Get a specific Snapshot by abid, uuid, or pk."""
|
||||||
request.with_archiveresults = with_archiveresults
|
request.with_archiveresults = with_archiveresults
|
||||||
snapshot = get_object_or_404(Snapshot, uuid=snapshot_uuid)
|
snapshot = None
|
||||||
|
try:
|
||||||
|
snapshot = Snapshot.objects.get(Q(uuid__startswith=snapshot_id) | Q(abid__startswith=snapshot_id)| Q(pk__startswith=snapshot_id))
|
||||||
|
except Snapshot.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
snapshot = snapshot or Snapshot.objects.get()
|
||||||
|
except Snapshot.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
snapshot = snapshot or Snapshot.objects.get(Q(uuid__icontains=snapshot_id) | Q(abid__icontains=snapshot_id))
|
||||||
|
except Snapshot.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return snapshot
|
return snapshot
|
||||||
|
|
||||||
|
|
||||||
|
@ -227,10 +271,21 @@ def get_snapshot(request, snapshot_uuid: str, with_archiveresults: bool=True):
|
||||||
|
|
||||||
|
|
||||||
class TagSchema(Schema):
|
class TagSchema(Schema):
|
||||||
|
abid: Optional[UUID] = Field(None, q='abid')
|
||||||
|
uuid: Optional[UUID] = Field(None, q='uuid')
|
||||||
|
pk: Optional[UUID] = Field(None, q='pk')
|
||||||
|
modified: datetime
|
||||||
|
created: datetime
|
||||||
|
created_by_id: str
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
slug: str
|
slug: str
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def resolve_created_by_id(obj):
|
||||||
|
return str(obj.created_by_id)
|
||||||
|
|
||||||
@router.get("/tags", response=List[TagSchema])
|
@router.get("/tags", response=List[TagSchema])
|
||||||
def list_tags(request):
|
def list_tags(request):
|
||||||
return Tag.objects.all()
|
return Tag.objects.all()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue