custom_ui/custom_ui/services/address_service.py
2026-01-09 12:51:25 -06:00

81 lines
3.4 KiB
Python

import frappe
class AddressService:
@staticmethod
def exists(address_name: str) -> bool:
"""Check if an address with the given name exists."""
print(f"DEBUG: Checking existence of Address with name: {address_name}")
result = frappe.db.exists("Address", address_name) is not None
print(f"DEBUG: Address existence: {result}")
return result
@staticmethod
def get(address_name: str):
"""Retrieve an address document by name. Returns None if not found."""
print(f"DEBUG: Retrieving Address document with name: {address_name}")
if AddressService.exists(address_name):
address_doc = frappe.get_doc("Address", address_name)
print("DEBUG: Address document found.")
return address_doc
print("DEBUG: Address document not found.")
return None
@staticmethod
def get_or_throw(address_name: str):
"""Retrieve an address document by name or throw an error if not found."""
address_doc = AddressService.get(address_name)
if address_doc:
return address_doc
raise ValueError(f"Address with name {address_name} does not exist.")
@staticmethod
def update_value(docname: str, fieldname: str, value, save: bool = True) -> frappe._dict:
"""Update a specific field value of a document."""
print(f"DEBUG: Updating Address {docname}, setting {fieldname} to {value}")
if AddressService.exists(docname) is False:
raise ValueError(f"Address with name {docname} does not exist.")
if save:
print("DEBUG: Saving updated Address document.")
address_doc = AddressService.get_or_throw(docname)
setattr(address_doc, fieldname, value)
address_doc.save(ignore_permissions=True)
else:
print("DEBUG: Not saving Address document as save=False.")
frappe.db.set_value("Address", docname, fieldname, value)
print(f"DEBUG: Updated Address {docname}: set {fieldname} to {value}")
return address_doc
@staticmethod
def get_value(docname: str, fieldname: str):
"""Get a specific field value of a document. Returns None if document does not exist."""
print(f"DEBUG: Getting value of field {fieldname} from Address {docname}")
if not AddressService.exists(docname):
print("DEBUG: Value cannot be retrieved; Address does not exist.")
return None
value = frappe.db.get_value("Address", docname, fieldname)
print(f"DEBUG: Retrieved value: {value}")
return value
@staticmethod
def get_value_or_throw(docname: str, fieldname: str):
"""Get a specific field value of a document or throw an error if document does not exist."""
value = AddressService.get_value(docname, fieldname)
if value is not None:
return value
raise ValueError(f"Address with name {docname} does not exist.")
@staticmethod
def create(address_data: dict):
"""Create a new address."""
print("DEBUG: Creating new Address with data:", address_data)
address = frappe.get_doc({
"doctype": "Address",
**address_data
})
address.insert(ignore_permissions=True)
print("DEBUG: Created new Address:", address.as_dict())
return address