Skip to content

Commit

Permalink
refactor: deprecate Record.contents and FileContents
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Mar 3, 2023
1 parent 2a665d0 commit 0dbb318
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lektor/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from lektor.sourceobj import VirtualSourceObject
from lektor.utils import cleanup_path
from lektor.utils import cleanup_url_path
from lektor.utils import deprecated
from lektor.utils import fs_enc
from lektor.utils import locate_executable
from lektor.utils import make_relative_url
Expand Down Expand Up @@ -366,6 +367,7 @@ def pagination(self):
return self.datamodel.pagination_config.get_pagination_controller(self)

@cached_property
@deprecated(version="3.4.0", stacklevel=2)
def contents(self):
return FileContents(self.source_filename)

Expand Down Expand Up @@ -754,6 +756,7 @@ def parent(self):
)

@cached_property
@deprecated(version="3.4.0", stacklevel=2)
def contents(self):
return FileContents(self.attachment_filename)

Expand Down
3 changes: 3 additions & 0 deletions lektor/filecontents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import mimetypes
import os

from lektor.utils import deprecated


class FileContents:
@deprecated(name="FileContents", version="3.4.0")
def __init__(self, filename):
self.filename = filename
self._md5 = None
Expand Down
14 changes: 14 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from lektor.db import Image
from lektor.db import Query
from lektor.db import Video
from lektor.filecontents import FileContents


def test_root(pad):
Expand Down Expand Up @@ -522,3 +523,16 @@ def test_Page_url_path_raise_error_if_paginated_and_dotted(scratch_pad):
def test_Attachment_url_path_is_for_primary_alt(scratch_pad, alt):
attachment = scratch_pad.get("/test.txt")
assert attachment.url_path == "/en/test.txt"


@pytest.mark.parametrize(
"path",
[
"/", # Page
"hello.txt", # Attachment
],
)
def test_Record_contents_is_deprecated(pad, path):
with pytest.deprecated_call(match=r"contents") as warnings:
assert isinstance(pad.get(path).contents, FileContents)
assert all(w.filename == __file__ for w in warnings)
13 changes: 13 additions & 0 deletions tests/test_filecontents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from lektor.filecontents import FileContents


def test_FileContents_is_deprecated():
with pytest.deprecated_call(match=r"FileContents") as warnings:
FileContents(__file__)
assert warnings[0].filename == __file__


def test_FileContents_is_a_type():
assert isinstance(FileContents, type)

0 comments on commit 0dbb318

Please sign in to comment.