-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbasic_feed_impl.py
More file actions
63 lines (53 loc) · 2.5 KB
/
basic_feed_impl.py
File metadata and controls
63 lines (53 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from shared.database_gen.sqlacodegen_models import Feed
from shared.db_models.external_id_impl import ExternalIdImpl
from shared.db_models.redirect_impl import RedirectImpl
from feeds_gen.models.basic_feed import BasicFeed
from feeds_gen.models.source_info import SourceInfo
class BaseFeedImpl(BasicFeed):
"""Base implementation of the feeds models.
This class converts a SQLAlchemy row DB object with common feed fields to a Pydantic model.
"""
class Config:
"""Pydantic configuration.
Enabling `from_attributes` method to create a model instance from a SQLAlchemy row object."""
from_attributes = True
@classmethod
def from_orm(cls, feed: Feed | None) -> BasicFeed | None:
if not feed:
return None
# Determine license_is_spdx from the related License ORM if available
license_is_spdx = None
license_tags = None
if getattr(feed, "license", None) is not None:
license_is_spdx = feed.license.is_spdx
license_tags = sorted([tag.id for tag in getattr(feed.license, "tags", [])]) or None
return cls(
id=feed.stable_id,
data_type=feed.data_type,
created_at=feed.created_at,
external_ids=sorted(
[ExternalIdImpl.from_orm(item) for item in feed.externalids], key=lambda x: x.external_id
),
provider=feed.provider,
feed_contact_email=feed.feed_contact_email,
source_info=SourceInfo(
producer_url=feed.producer_url,
authentication_type=None if feed.authentication_type is None else int(feed.authentication_type),
authentication_info_url=feed.authentication_info_url,
api_key_parameter_name=feed.api_key_parameter_name,
license_url=feed.license_url,
license_id=feed.license_id,
license_is_spdx=license_is_spdx,
license_notes=feed.license_notes,
license_tags=license_tags,
),
redirects=sorted([RedirectImpl.from_orm(item) for item in feed.redirectingids], key=lambda x: x.target_id),
)
class BasicFeedImpl(BaseFeedImpl, BasicFeed):
"""Implementation of the `BasicFeed` model.
This class converts a SQLAlchemy row DB object to a Pydantic model.
"""
class Config:
"""Pydantic configuration.
Enabling `from_orm` method to create a model instance from a SQLAlchemy row object."""
from_attributes = True