@frappe.whitelist()
def get_item_group(self):
item_group = frappe.get_list("Item Group",
fields=["name", "file_image"] ,
filters={
'in_slider': 1
},
)
for ig in item_group:
if ig.file_image:
ig.file_image = frappe.utils.get_file_link(ig.file_image)
return item_group
code
@frappe.whitelist()
def get_item_group_details(self, item_group):
if not item_group:
return {}
try:
item_group_doc = frappe.get_doc("Item Group", item_group)
return {
"name": item_group_doc.name,
"file_image": frappe.utils.get_file_link(item_group_doc.image),
}
except Exception as e:
frappe.log_error(_("Item Group '{0}' not found. Error: {1}").format(item_group, str(e)), "get_package_details")
return {}
Assistant
User
اريد ان يتم جلب صوره مجموعه الاصناف ايضاً
Assistant
إذا كنت ترغب في جلب صورة مجموعة الأصناف (Item Group) بالإضافة إلى اسمها، يمكنك تعديل الكود الحالي لجلب صورة المجموعة عند استرجاع تفاصيل المجموعة. سأوضح لك كيفية القيام بذلك.
تعديل الدالة get_item_group:
لتشمل الصورة (التي تمثل مجموعة الأصناف) فعليك التأكد من استخدام حقل الصورة الصحيح. عادةً سيكون هناك حقل يُسمى file_image أو image في نموذج "Item Group". سأفترض أنك ترغب في استخدام الحقل file_image.
python
The code you've provided is a Python script used in the Frappe framework, which is commonly employed in ERPNext applications. This script defines two whitelisted methods that interact with the "Item Group" doctype. Let's break down the functionality of each method:
Method 1: get_item_group
This method retrieves a list of item groups that have in_slider set to 1, meaning they are likely featured in some promotional slider.
python
@frappe.whitelist()
def get_item_group():
item_group = frappe.get_list("Item Group",
fields=["name", "file_image"],
filters={
'in_slider': 1
},
)
# Loop through each item group and update the file_image if it exists
for ig in item_group:
if ig.file_image:
ig.file_image = frappe.utils.get_file_link(ig.file_image)
return item_group
Purpose: Fetches a filtered list of item groups, returning their names and associated image links.
Improvements:
Ensure file_image field on the "Item Group" doctype is set to Image.
You might want to handle cases where images do not have links correctly.
Method 2: get_item_group_details
This method retrieves detailed information about a specific item group based on the passed name.
python
@frappe.whitelist()
def get_item_group_details(item_group):
if not item_group:
return {}
try:
item_group_doc = frappe.get_doc("Item Group", item_group)
return {
"name": item_group_doc.name,
"file_image": frappe.utils.get_file_link(item_group_doc.image),
}
except Exception as e:
frappe.log_error(_("Item Group '{0}' not found. Error: {1}").format(item_group, str(e)), "get_package_details")
return {}
Purpose: Retrieves detailed information (like name and file image) for a specific item group.
Error Handling: If the item group is not found, it logs an error message and returns an empty dictionary.
Improvements:
Ensure that item_group_doc.image is correctly referenced; it might be better to check for item_group_doc.file_image instead of item_group_doc.image, depending on your schema.
General Observations:
Security: The @frappe.whitelist() decorator allows these methods to be called from the client side. Make sure to validate inputs in production code to prevent injection or other vulnerabilities.
Localization: Assuming you're using _() for localizing error messages, ensure you have included the necessary imports for translation.
Error Logging: Consider standardizing error messages and possibly handling various types of exceptions to improve robustness.
This code provides a solid basis for interacting with the "Item Group" in your ERPNext application, allowing for flexibility and extensibility if necessary.
@frappe.whitelist()
def get_item_group():
item_group = frappe.get_list("Item Group",
fields=["name", "file_image"],
filters={
'in_slider': 1
},
)
# Loop through each item group and update the file_image if it exists
for ig in item_group:
if ig.file_image:
ig.file_image = frappe.utils.get_file_link(ig.file_image)
return item_group
تعديل الدالة get_item_group_details:
هنا ستقوم أيضاً بجلب صورة مجموعة الأصناف في تفاصيل المجموعة:
python
@frappe.whitelist()
def get_item_group_details(item_group):
if not item_group:
return {}
try:
item_group_doc = frappe.get_doc("Item Group", item_group)
return {
"name": item_group_doc.name,
"file_image": frappe.utils.get_file_link(item_group_doc.file_image), # استخدام file_image هنا
}
except Exception as e:
frappe.log_error(_("Item Group '{0}' not found. Error: {1}").format(item_group, str(e)), "get_package_details")
return {}
ملاحظات إضافية:
تأكد من أن الحقل الذي تستخدمه لجلب الصورة متوفر في نموذج "Item Group". إذا كان لديك حقل آخر (مثل image)، فتأكد من تغيير ذلك في الكود.
تأكد من أن لديك الأذونات المناسبة للوصول إلى الصور في المشروع الخاص بك.
في عرض واجهة المستخدم، تأكد من أنك تعرض الصور بالطريقة المناسبة.
إذا كنت بحاجة إلى أي مساعدة إضافية، فلا تتردد في طلب المزيد من التوضيحات!