-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
101 lines (78 loc) · 2.99 KB
/
functions.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from datetime import datetime
from _config import db
from models import Changes, FeedEntry, FeedEntryTag, Tag
def update_or_create_change(record_id):
"""
Get a Changes record by ID or create a new one if it doesn't exist.
Args:
record_id (int): The ID of the Changes record.
Returns:
Changes: The retrieved or newly created Changes record.
"""
record = db.session.get(Changes, record_id)
if record:
record.updated = datetime.utcnow()
else:
record = Changes(id=record_id, updated=datetime.utcnow())
db.session.add(record)
try:
db.session.commit()
except Exception as e:
db.session.rollback()
print(f"An error occurred: {e}")
return record
def get_change_by_id(record_id):
"""Retrieve a change record by its ID.
This function queries the database for a change record with the specified
ID and returns the corresponding record if found.
Args:
record_id (int): The ID of the change record to retrieve.
Returns:
Changes or None: The change record if found, otherwise None.
"""
change_record = db.session.query(Changes).filter(Changes.id == record_id).first()
return change_record
def rfc_3339_date(date):
"""Convert a datetime object to an RFC 3339 formatted string.
This function takes a datetime object and returns a string representation
in RFC 3339 format, which is a subset of ISO 8601. The resulting string
includes a 'Z' suffix to indicate that the time is in UTC.
Args:
date (datetime): The datetime object to convert.
Returns:
str: The RFC 3339 formatted date string.
"""
return date.isoformat(sep='T') + 'Z'
def get_entries_by_tag_or_not(tag_name=None, limit=None):
"""Retrieve feed entries filtered by tag name or return all entries.
This function fetches feed entries from the database. If a tag name is
provided, it retrieves entries associated with that tag. If no tag name
is specified, it returns all feed entries. The results can be limited
by the specified limit.
Args:
tag_name (str, optional): The name of the tag to filter entries.
limit (int, optional): The maximum number of entries to return.
Returns:
list: A list of FeedEntry objects matching the criteria. If the
tag is not found, an empty list is returned.
"""
if tag_name:
tag = Tag.query.filter(Tag.name.ilike(tag_name)).first()
if tag:
query = (
FeedEntry.query.join(FeedEntryTag)
.filter(FeedEntryTag.tag_id == tag.id)
.order_by(FeedEntry.id.desc())
)
if limit:
query = query.limit(limit)
entries = query.all()
else:
print('Tag not found!')
return []
else:
query = FeedEntry.query.order_by(FeedEntry.id.desc())
if limit:
query = query.limit(limit)
entries = query.all()
return entries