Enhance add_card function to include power, charm, and wit parameters with validation checks
This commit is contained in:
parent
7c7d299fc4
commit
91dfe77ffa
2 changed files with 11 additions and 4 deletions
|
@ -19,7 +19,7 @@ import config
|
||||||
from fediverse_factory import get_fediverse_service
|
from fediverse_factory import get_fediverse_service
|
||||||
import db_utils
|
import db_utils
|
||||||
|
|
||||||
def add_card(name: str, rarity: int, image_url: str) -> tuple[int, str]:
|
def add_card(name: str, rarity: int, image_url: str, power: int, charm: int, wit: int) -> tuple[int, str]:
|
||||||
"""
|
"""
|
||||||
Adds a card to the database, uploading the image from a public URL to the Fediverse instance.
|
Adds a card to the database, uploading the image from a public URL to the Fediverse instance.
|
||||||
|
|
||||||
|
@ -27,6 +27,9 @@ def add_card(name: str, rarity: int, image_url: str) -> tuple[int, str]:
|
||||||
name (str): Card name.
|
name (str): Card name.
|
||||||
rarity (int): Card rarity (e.g., 1-5).
|
rarity (int): Card rarity (e.g., 1-5).
|
||||||
image_url (str): Public URL of the image from the post.
|
image_url (str): Public URL of the image from the post.
|
||||||
|
power (int): Card power value.
|
||||||
|
charm (int): Card charm value.
|
||||||
|
wit (int): Card wit value.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple[int, str]: Card ID and file_id.
|
tuple[int, str]: Card ID and file_id.
|
||||||
|
@ -47,7 +50,8 @@ def add_card(name: str, rarity: int, image_url: str) -> tuple[int, str]:
|
||||||
raise ValueError(f'Invalid rarity: {rarity}')
|
raise ValueError(f'Invalid rarity: {rarity}')
|
||||||
if not image_url:
|
if not image_url:
|
||||||
raise ValueError('Image URL must be provided.')
|
raise ValueError('Image URL must be provided.')
|
||||||
|
if power < 0 or charm < 0 or wit < 0:
|
||||||
|
raise ValueError('Power, charm, and wit must be non-negative integers.')
|
||||||
try:
|
try:
|
||||||
# Download image
|
# Download image
|
||||||
response = requests.get(image_url, stream=True, timeout=30)
|
response = requests.get(image_url, stream=True, timeout=30)
|
||||||
|
@ -66,7 +70,10 @@ def add_card(name: str, rarity: int, image_url: str) -> tuple[int, str]:
|
||||||
card_id = db_utils.insert_card(
|
card_id = db_utils.insert_card(
|
||||||
stripped_name,
|
stripped_name,
|
||||||
rarity,
|
rarity,
|
||||||
file_id
|
file_id,
|
||||||
|
power,
|
||||||
|
charm,
|
||||||
|
wit
|
||||||
)
|
)
|
||||||
|
|
||||||
return card_id, file_id
|
return card_id, file_id
|
||||||
|
|
|
@ -140,7 +140,7 @@ be a number between 1 and 5',
|
||||||
if len(arguments) == 2:
|
if len(arguments) == 2:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if not all(is_float(arg) for arg in arguments[2:]):
|
if not all(arg.isnumeric() for arg in arguments[2:]):
|
||||||
return {
|
return {
|
||||||
'message': f'{author} Invalid attributes: power, charm and \
|
'message': f'{author} Invalid attributes: power, charm and \
|
||||||
wit must be numbers.',
|
wit must be numbers.',
|
||||||
|
|
Loading…
Add table
Reference in a new issue