-
Notifications
You must be signed in to change notification settings - Fork 8
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 #95 from Gallaecio/auto-fields
Implement auto_field field metadata
- Loading branch information
Showing
15 changed files
with
278 additions
and
189 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
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,42 @@ | ||
from typing import Callable, List, Optional | ||
|
||
from web_poet import ItemPage, field | ||
from web_poet.fields import get_fields_dict | ||
|
||
|
||
def auto_field( | ||
method=None, | ||
*, | ||
cached: bool = False, | ||
meta: Optional[dict] = None, | ||
out: Optional[List[Callable]] = None, | ||
): | ||
"""Decorator that works like :func:`web_poet.fields.field` but sets | ||
``auto_field`` to ``True`` by default in *meta*. | ||
.. code-block:: python | ||
from zyte_common_items import AutoProductPage | ||
from zyte_common_items.fields import auto_field | ||
class ProductPage(AutoProductPage): | ||
@auto_field | ||
def name(self): | ||
return super().name | ||
""" | ||
meta = meta or {} | ||
meta.setdefault("auto_field", True) | ||
return field(method, cached=cached, meta=meta, out=out) | ||
|
||
|
||
def is_auto_field(cls: ItemPage, field: str): | ||
"""Return ``True`` if the field named *field* of the *cls* page object | ||
class has ``auto_field`` set to ``True`` in its field metadata. | ||
All fields defined in :ref:`auto page object classes <auto>` meet this | ||
condition. | ||
""" | ||
fields_dict = get_fields_dict(cls) | ||
field_meta = fields_dict[field].meta or {} | ||
return field_meta.get("auto_field", False) |
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
Oops, something went wrong.