Source code for addons.functions
from core.models import Country,Region,SubRegion,Addition,Account,Style
from .models import Vintages,Addons
[docs]
def is_valid_year(year_str):
"""
Check if the input string is a valid year.
Args:
year_str (str): The year as a string.
Returns:
bool: True if the year is valid, False otherwise.
"""
from datetime import datetime
# Check if the input string is a digit and has a length of 4
if year_str.isdigit() and len(year_str) == 4:
year = int(year_str)
current_year = datetime.now().year
# Check if the year is within a reasonable range
if 1900 <= year <= current_year:
return True
return False
# Example usage
#print(is_valid_year("2025")) # Output: True
#print(is_valid_year("1899")) # Output: False
#print(is_valid_year("abcd")) # Output: False
[docs]
def vintage_exists(country,year):
#check if the vintage exists in the database
try:
vintage = Vintages.objects.filter(year=year,country_id=country)
return vintage.count()
return 1
except:
return 0
[docs]
def get_vintage(year,country,region,subregion='1'):
#return the vintage record from arguments provided
# we need to include region so we can easily process records where there is only 1 subregion
if subregion == '1':
vintage = Vintages.objects.get(year=year,region_id=region)
else:
vintage = Vintages.objects.get(year=year,region_id=region,subregion_id=subregion)
return vintage
[docs]
def update_addon_details(wine_id,addon_id,vintage_id):
#update the addon details - return 1 if successful
print('vintage_id=',vintage_id)
vintage = Vintages.objects.get(id=vintage_id)
print('vintage=',vintage)
addon_update = Addons.objects.get(id=addon_id)
print('addon_update=',addon_update)
addon_update.subregion_id=vintage.subregion_id
print('addon_update.subregion_id=',addon_update.subregion_id)
addon_update.region_id=vintage.region_id
print('addon_update.region_id=',addon_update.region_id)
addon_update.load_status='2'
addon_update.quality=vintage.quality
print('addon_update.quality=',addon_update.quality)
addon_update.drink=vintage.life
print('addon_update.drink=',addon_update.drink)
addon_update.status=vintage.status
print('addon_update.status=',addon_update.status)
try:
addon_update.save()
return 1
print('addon updated')
except:
return 0