This is hook.py
app_name = "it_asset_management"
app_title = "IT Asset Management"
app_publisher = "IT"
app_description = "IT Asset Management"
app_email = "it@example.com"
app_license = "mit"
# required_apps = []
# Includes in <head>
# ------------------
# include js, css files in header of desk.html
# app_include_css = "/assets/it_asset_management/css/it_asset_management.css"
# app_include_js = "/assets/it_asset_management/js/it_asset_management.js"
and api.py
import frappe
from frappe import _
@frappe.whitelist()
def update_or_create_computer_activity():
data = frappe.local.form_dict
if not data:
frappe.throw(_("No data received"))
serial_number = data.get("serial_number")
if not serial_number:
frappe.throw(_("Serial number is required"))
# Kiểm tra nếu tài sản đã tồn tại
existing_log = frappe.get_all("Computer Activity Logs", filters={"serial_number": serial_number}, limit=1)
if existing_log:
# Cập nhật log hiện tại
log = frappe.get_doc("Computer Activity Logs", existing_log[0].name)
log.update({
"last_update": data.get("last_update"),
"ip_address": data.get("ip_address"),
"mac_address": data.get("mac_address"),
"computer_name": data.get("computer_name"),
"computer_model": data.get("computer_model"),
"manufacture": data.get("manufacture")
})
log.save()
return {"message": "Log updated successfully", "log_name": log.name}
else:
# Tạo log mới
log = frappe.get_doc({
"doctype": "Computer Activity Logs",
"serial_number": serial_number,
"computer_name": data.get("computer_name"),
"computer_model": data.get("computer_model"),
"manufacture": data.get("manufacture"),
"mac_address": data.get("mac_address"),
"ip_address": data.get("ip_address"),
"last_update": data.get("last_update")
})
log.insert()
return {"message": "Log created successfully", "log_name": log.name}
error when use postman using POST
"exception": "frappe.exceptions.PermissionError: You are not permitted to access this resource.Function it_asset_management.api.update_or_create_computer_activity is not whitelisted.",
"exc_type": "PermissionError",
"exc": "[\"Traceback (most recent call last):\\n File \\\"apps/frappe/frappe/app.py\\\", line 114, in application\\n response = frappe.api.handle(request)\\n
JSON data
{
"computer_name": "MyComputer",
"serial_number": "1234567890",
"computer_model": "Dell XPS",
"manufacture": "Main Office",
"mac_address": "00:1A:2B:3C:4D:5E",
"ip_address": "192.168.1.100",
"last_update": "2024-07-18 14:00:00"
}
this is doctype
{
"actions": [],
"allow_rename": 1,
"creation": "2024-07-25 12:22:12.088010",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"computer_infomation_section",
"computer_name",
"column_break_xfmf",
"serial_number",
"column_break_yodw",
"computer_model",
"column_break_gsnn",
"manufacture",
"section_break_ntex",
"mac_address",
"column_break_ocas",
"ip_address",
"column_break_wvsk",
"lasted_update"
],
"fields": [
{
"fieldname": "computer_infomation_section",
"fieldtype": "Section Break",
"label": "Computer Infomation"
},
{
"fieldname": "computer_name",
"fieldtype": "Data",
"label": "Computer Name"
},
{
"fieldname": "column_break_xfmf",
"fieldtype": "Column Break"
},
{
"fieldname": "serial_number",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Serial number",
"reqd": 1
},
{
"fieldname": "column_break_yodw",
"fieldtype": "Column Break"
},
{
"fieldname": "computer_model",
"fieldtype": "Data",
"label": "Computer Model"
},
{
"fieldname": "column_break_gsnn",
"fieldtype": "Column Break"
},
{
"fieldname": "manufacture",
"fieldtype": "Data",
"label": "Manufacture"
},
{
"fieldname": "section_break_ntex",
"fieldtype": "Section Break"
},
{
"fieldname": "mac_address",
"fieldtype": "Data",
"label": "Mac Address"
},
{
"fieldname": "column_break_ocas",
"fieldtype": "Column Break"
},
{
"fieldname": "ip_address",
"fieldtype": "Data",
"label": "IP Address"
},
{
"fieldname": "column_break_wvsk",
"fieldtype": "Column Break"
},
{
"fieldname": "lasted_update",
"fieldtype": "Datetime",
"label": "Lasted Update"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-07-25 12:32:14.915183",
"modified_by": "Administrator",
"module": "IT Asset Management",
"name": "Computer Activity Logs",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"sort_field": "modified",
"sort_order": "DESC",
"states": []
}
thank you.