-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpublications.py
More file actions
85 lines (70 loc) · 2.44 KB
/
publications.py
File metadata and controls
85 lines (70 loc) · 2.44 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
from datetime import datetime
from typing import Optional
from db_models import Publications
from logutils import get_logger
logger = get_logger(__name__)
def create_publication_entry(
platform_name,
source,
status,
country_code=None,
gateway_client=None,
):
"""
Store a new publication entry with correct status.
Args:
country_code (str): Country code.
platform_name (str): Platform name.
source (str): Source of publication.
gateway_client (str): Gateway client.
status (str): "published" if successful, "failed" if not.
"""
publication = Publications.create(
country_code=country_code,
platform_name=platform_name,
source=source,
status=status,
gateway_client=gateway_client,
)
logger.info("Successfully logged publication")
return publication
def fetch_publication(
start_date: datetime.date,
end_date: datetime.date,
filters: dict[str, Optional[str]],
page: int = 1,
page_size: int = 10,
) -> dict[str, any]:
"""Fetch publications based on filters with pagination."""
start_datetime = datetime.combine(start_date, datetime.min.time())
end_datetime = datetime.combine(end_date, datetime.max.time())
query = (
Publications.select()
.where(
(Publications.date_created >= start_datetime)
& (Publications.date_created <= end_datetime)
)
.order_by(Publications.date_created.desc())
)
for key, value in filters.items():
if value:
query = query.where(getattr(Publications, key) == value)
total_publications = query.count()
total_published = query.where(Publications.status == "published").count()
total_failed = query.where(Publications.status == "failed").count()
offset = (page - 1) * page_size
paginated_query = query.limit(page_size).offset(offset)
return {
"data": list(paginated_query),
"total_publications": total_publications,
"total_published": total_published,
"total_failed": total_failed,
"page": page,
"page_size": page_size,
"total_pages": (total_publications + page_size - 1) // page_size,
}