diff --git a/.github/helper/install.sh b/.github/helper/install.sh index 19a7c68e19..246bdbe096 100644 --- a/.github/helper/install.sh +++ b/.github/helper/install.sh @@ -50,7 +50,9 @@ if [ "$TYPE" == "server" ]; then sed -i 's/^socketio:/# socketio:/g' Procfile; f if [ "$TYPE" == "server" ]; then sed -i 's/^redis_socketio:/# redis_socketio:/g' Procfile; fi if [ "$TYPE" == "ui" ]; then bench setup requirements --node; fi -if [ "$TYPE" == "server" ]; then bench setup requirements --dev; fi +bench setup requirements --dev + +if [ "$TYPE" == "ui" ]; then sed -i 's/^web: bench serve/web: bench serve --with-coverage/g' Procfile; fi # install node-sass which is required for website theme test cd ./apps/frappe || exit @@ -60,4 +62,4 @@ cd ../.. bench start & bench --site test_site reinstall --yes if [ "$TYPE" == "server" ]; then bench --site test_site_producer reinstall --yes; fi -CI=Yes bench build --app frappe +if [ "$TYPE" == "server" ]; then CI=Yes bench build --app frappe; fi diff --git a/.github/helper/roulette.py b/.github/helper/roulette.py index 9831df7f30..90f4608a22 100644 --- a/.github/helper/roulette.py +++ b/.github/helper/roulette.py @@ -41,6 +41,7 @@ if __name__ == "__main__": # this is a push build, run all builds if not pr_number: os.system('echo "::set-output name=build::strawberry"') + os.system('echo "::set-output name=build-server::strawberry"') sys.exit(0) files_list = files_list or get_files_list(pr_number=pr_number, repo=repo) @@ -52,7 +53,8 @@ if __name__ == "__main__": ci_files_changed = any(f for f in files_list if is_ci(f)) only_docs_changed = len(list(filter(is_docs, files_list))) == len(files_list) only_frontend_code_changed = len(list(filter(is_frontend_code, files_list))) == len(files_list) - only_py_changed = len(list(filter(is_py, files_list))) == len(files_list) + updated_py_file_count = len(list(filter(is_py, files_list))) + only_py_changed = updated_py_file_count == len(files_list) if ci_files_changed: print("CI related files were updated, running all build processes.") @@ -65,8 +67,12 @@ if __name__ == "__main__": print("Only Frontend code was updated; Stopping Python build process.") sys.exit(0) - elif only_py_changed and build_type == "ui": - print("Only Python code was updated, stopping Cypress build process.") - sys.exit(0) + elif build_type == "ui": + if only_py_changed: + print("Only Python code was updated, stopping Cypress build process.") + sys.exit(0) + elif updated_py_file_count > 0: + # both frontend and backend code were updated + os.system('echo "::set-output name=build-server::strawberry"') os.system('echo "::set-output name=build::strawberry"') diff --git a/.github/workflows/ui-tests.yml b/.github/workflows/ui-tests.yml index 3eefd1ce82..fc8093444e 100644 --- a/.github/workflows/ui-tests.yml +++ b/.github/workflows/ui-tests.yml @@ -141,6 +141,12 @@ jobs: env: CYPRESS_RECORD_KEY: 4a48f41c-11b3-425b-aa88-c58048fa69eb + - name: Stop server + if: ${{ steps.check-build.outputs.build-server == 'strawberry' }} + run: | + ps -ef | grep "frappe serve" | awk '{print $2}' | xargs kill -s SIGINT 2> /dev/null || true + sleep 5 + - name: Check If Coverage Report Exists id: check_coverage uses: andstor/file-existence-action@v1 @@ -156,3 +162,13 @@ jobs: directory: /home/runner/frappe-bench/apps/frappe/.cypress-coverage/ verbose: true flags: ui-tests + + - name: Upload Server Coverage Data + if: ${{ steps.check-build.outputs.build-server == 'strawberry' }} + uses: codecov/codecov-action@v2 + with: + name: MariaDB + fail_ci_if_error: true + files: /home/runner/frappe-bench/sites/coverage.xml + verbose: true + flags: server diff --git a/.mergify.yml b/.mergify.yml index 0bd9641d5b..63fe1a0086 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -48,3 +48,7 @@ pull_request_rules: actions: merge: method: squash + commit_message_template: | + {{ title }} (#{{ number }}) + + {{ body }} diff --git a/cypress/fixtures/child_table_doctype.js b/cypress/fixtures/child_table_doctype.js new file mode 100644 index 0000000000..f65e5d1765 --- /dev/null +++ b/cypress/fixtures/child_table_doctype.js @@ -0,0 +1,30 @@ +export default { + name: "Child Table Doctype", + actions: [], + custom: 1, + autoname: "field:title", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "title", + fieldtype: "Data", + in_list_view: 1, + label: "Title", + unique: 1 + } + ], + links: [], + istable: 1, + modified: "2022-02-10 12:03:12.603763", + modified_by: "Administrator", + module: "Custom", + naming_rule: "By fieldname", + owner: "Administrator", + permissions: [], + sort_field: 'modified', + sort_order: 'ASC', + track_changes: 1 +}; \ No newline at end of file diff --git a/cypress/fixtures/doctype_to_link.js b/cypress/fixtures/doctype_to_link.js new file mode 100644 index 0000000000..f5335b1755 --- /dev/null +++ b/cypress/fixtures/doctype_to_link.js @@ -0,0 +1,45 @@ +export default { + name: "Doctype to Link", + actions: [], + custom: 1, + naming_rule: "By fieldname", + autoname: "field:title", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + "fieldname": "title", + "fieldtype": "Data", + "label": "Title", + "unique": 1 + } + ], + links: [ + { + "group": "Child Doctype", + "link_doctype": "Doctype With Child Table", + "link_fieldname": "title" + } + ], + modified: "2022-02-10 12:03:12.603763", + modified_by: "Administrator", + module: "Custom", + owner: "Administrator", + permissions: [ + { + create: 1, + delete: 1, + email: 1, + print: 1, + read: 1, + role: 'System Manager', + share: 1, + write: 1 + } + ], + sort_field: 'modified', + sort_order: 'ASC', + track_changes: 1 +}; \ No newline at end of file diff --git a/cypress/fixtures/doctype_with_child_table.js b/cypress/fixtures/doctype_with_child_table.js new file mode 100644 index 0000000000..bbb2127448 --- /dev/null +++ b/cypress/fixtures/doctype_with_child_table.js @@ -0,0 +1,46 @@ +export default { + name: "Doctype With Child Table", + actions: [], + custom: 1, + autoname: "field:title", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "title", + fieldtype: "Data", + label: "Title", + unique: 1 + }, + { + fieldname: "child_table", + fieldtype: "Table", + label: "Child Table", + options: "Child Table Doctype", + reqd: 1 + } + ], + links: [], + modified: "2022-02-10 12:03:12.603763", + modified_by: "Administrator", + module: "Custom", + naming_rule: "By fieldname", + owner: "Administrator", + permissions: [ + { + create: 1, + delete: 1, + email: 1, + print: 1, + read: 1, + role: 'System Manager', + share: 1, + write: 1 + } + ], + sort_field: 'modified', + sort_order: 'ASC', + track_changes: 1 +}; diff --git a/cypress/integration/control_link.js b/cypress/integration/control_link.js index bfa70ad338..7a7e94d2f5 100644 --- a/cypress/integration/control_link.js +++ b/cypress/integration/control_link.js @@ -95,6 +95,51 @@ context('Control Link', () => { }); }); + it('show title field in link', () => { + get_dialog_with_link().as('dialog'); + + cy.insert_doc("Property Setter", { + "doctype": "Property Setter", + "doc_type": "ToDo", + "property": "show_title_field_in_link", + "property_type": "Check", + "doctype_or_field": "DocType", + "value": "1" + }, true); + + cy.window().its('frappe').then(frappe => { + if (!frappe.boot) { + frappe.boot = { + link_title_doctypes: ['ToDo'] + }; + } else { + frappe.boot.link_title_doctypes = ['ToDo']; + } + }); + + cy.intercept('POST', '/api/method/frappe.desk.search.search_link').as('search_link'); + + cy.get('.frappe-control[data-fieldname=link] input').focus().as('input'); + cy.wait('@search_link'); + cy.get('@input').type('todo for link'); + cy.wait('@search_link'); + cy.get('.frappe-control[data-fieldname=link] ul').should('be.visible'); + cy.get('.frappe-control[data-fieldname=link] input').type('{enter}', { delay: 100 }); + cy.get('.frappe-control[data-fieldname=link] input').blur(); + cy.get('@dialog').then(dialog => { + cy.get('@todos').then(todos => { + let field = dialog.get_field('link'); + let value = field.get_value(); + let label = field.get_label_value(); + + expect(value).to.eq(todos[0]); + expect(label).to.eq('this is a test todo for link'); + + cy.remove_doc("Property Setter", "ToDo-main-show_title_field_in_link"); + }); + }); + }); + it('should update dependant fields (via fetch_from)', () => { cy.get('@todos').then(todos => { cy.visit(`/app/todo/${todos[0]}`); diff --git a/cypress/integration/dashboard_links.js b/cypress/integration/dashboard_links.js index 16ffd41cf4..93d10cf1fd 100644 --- a/cypress/integration/dashboard_links.js +++ b/cypress/integration/dashboard_links.js @@ -1,7 +1,21 @@ +import doctype_with_child_table from '../fixtures/doctype_with_child_table'; +import child_table_doctype from '../fixtures/child_table_doctype'; +import doctype_to_link from '../fixtures/doctype_to_link'; +const doctype_to_link_name = doctype_to_link.name; +const child_table_doctype_name = child_table_doctype.name; + context('Dashboard links', () => { before(() => { cy.visit('/login'); cy.login(); + cy.insert_doc('DocType', child_table_doctype, true); + cy.insert_doc('DocType', doctype_with_child_table, true); + cy.insert_doc('DocType', doctype_to_link, true); + return cy.window().its('frappe').then(frappe => { + return frappe.xcall("frappe.tests.ui_test_helpers.update_child_table", { + name: child_table_doctype_name + }); + }); }); it('Adding a new contact, checking for the counter on the dashboard and deleting the created contact', () => { @@ -62,4 +76,14 @@ context('Dashboard links', () => { cy.findByText('Website Analytics'); }); }); + + it('check if child table is populated with linked field on creation from dashboard link', () => { + cy.new_form(doctype_to_link_name); + cy.fill_field("title", "Test Linking"); + cy.findByRole("button", {name: "Save"}).click(); + + cy.get('.document-link .btn-new').click(); + cy.get('.frappe-control[data-fieldname="child_table"] .rows .data-row .col[data-fieldname="doctype_to_link"]') + .should('contain.text', 'Test Linking'); + }); }); diff --git a/cypress/integration/depends_on.js b/cypress/integration/depends_on.js index 9aa6b5d89d..12f54f2b6e 100644 --- a/cypress/integration/depends_on.js +++ b/cypress/integration/depends_on.js @@ -55,10 +55,31 @@ context('Depends On', () => { 'read_only_depends_on': "eval:doc.test_field=='Some Other Value'", 'options': "Child Test Depends On" }, + { + "label": "Dependent Tab", + "fieldname": "dependent_tab", + "fieldtype": "Tab Break", + "depends_on": "eval:doc.test_field=='Show Tab'" + }, + { + "fieldname": "tab_section", + "fieldtype": "Section Break", + }, + { + "label": "Field in Tab", + "fieldname": "field_in_tab", + "fieldtype": "Data", + } ] }); }); }); + it('should show the tab on other setting field value', () => { + cy.new_form('Test Depends On'); + cy.fill_field('test_field', 'Show Tab'); + cy.get('body').click(); + cy.findByRole("tab", {name: "Dependent Tab"}).should('be.visible'); + }); it('should set the field as mandatory depending on other fields value', () => { cy.new_form('Test Depends On'); cy.fill_field('test_field', 'Some Value'); diff --git a/cypress/integration/grid.js b/cypress/integration/grid.js new file mode 100644 index 0000000000..4fa52712cf --- /dev/null +++ b/cypress/integration/grid.js @@ -0,0 +1,92 @@ +context('Grid', () => { + beforeEach(() => { + cy.login(); + cy.visit('/app/website'); + }); + before(() => { + cy.login(); + cy.visit('/app/website'); + return cy.window().its('frappe').then(frappe => { + return frappe.call("frappe.tests.ui_test_helpers.create_contact_phone_nos_records"); + }); + }); + it('update docfield property using update_docfield_property', () => { + cy.visit('/app/contact/Test Contact'); + cy.window().its("cur_frm").then(frm => { + cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); + let field = frm.get_field("phone_nos"); + field.grid.update_docfield_property("is_primary_phone", "hidden", true); + + cy.get('@table').find('[data-idx="1"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get('@table-form').find('.frappe-control[data-fieldname="is_primary_phone"]').should("be.hidden"); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + + cy.get('@table').find('[data-idx="2"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get('@table-form').find('.frappe-control[data-fieldname="is_primary_phone"]').should("be.hidden"); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + }); + }); + it('update docfield property using toggle_display', () => { + cy.visit('/app/contact/Test Contact'); + cy.window().its("cur_frm").then(frm => { + cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); + let field = frm.get_field("phone_nos"); + field.grid.toggle_display("is_primary_mobile_no", false); + + cy.get('@table').find('[data-idx="1"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get('@table-form').find('.frappe-control[data-fieldname="is_primary_mobile_no"]').should("be.hidden"); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + + cy.get('@table').find('[data-idx="2"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get('@table-form').find('.frappe-control[data-fieldname="is_primary_mobile_no"]').should("be.hidden"); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + }); + }); + it('update docfield property using toggle_enable', () => { + cy.visit('/app/contact/Test Contact'); + cy.window().its("cur_frm").then(frm => { + cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); + let field = frm.get_field("phone_nos"); + field.grid.toggle_enable("phone", false); + + + cy.get('@table').find('[data-idx="1"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get('@table-form').find('.frappe-control[data-fieldname="phone"] .control-value').should('have.class', 'like-disabled-input'); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + + cy.get('@table').find('[data-idx="2"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get('@table-form').find('.frappe-control[data-fieldname="phone"] .control-value').should('have.class', 'like-disabled-input'); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + }); + }); + it('update docfield property using toggle_reqd', () => { + cy.visit('/app/contact/Test Contact'); + cy.window().its("cur_frm").then(frm => { + cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); + let field = frm.get_field("phone_nos"); + field.grid.toggle_reqd("phone", false); + + cy.get('@table').find('[data-idx="1"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get_field("phone").as('phone-field'); + cy.get('@phone-field').focus().clear().wait(500).blur(); + cy.get('@phone-field').should("not.have.class", "has-error"); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + + cy.get('@table').find('[data-idx="2"] .edit-grid-row').click(); + cy.get('.grid-row-open').as('table-form'); + cy.get_field("phone").as('phone-field'); + cy.get('@phone-field').focus().clear().wait(500).blur(); + cy.get('@phone-field').should("not.have.class", "has-error"); + cy.get('@table-form').find('.grid-footer-toolbar').click(); + + }); + }); +}); + diff --git a/cypress/integration/report_view.js b/cypress/integration/report_view.js index 0253e8fd43..4bc5784a53 100644 --- a/cypress/integration/report_view.js +++ b/cypress/integration/report_view.js @@ -11,30 +11,63 @@ context('Report View', () => { 'title': 'Doc 1', 'description': 'Random Text', 'enabled': 0, - // submit document - 'docstatus': 1 - }, true).as('doc'); + 'docstatus': 1 // submit document + }, true); + return cy.window().its('frappe').then(frappe => { + return frappe.call("frappe.tests.ui_test_helpers.create_multiple_contact_records"); + }); }); + it('Field with enabled allow_on_submit should be editable.', () => { cy.intercept('POST', 'api/method/frappe.client.set_value').as('value-update'); cy.visit(`/app/List/${doctype_name}/Report`); + // check status column added from docstatus cy.get('.dt-row-0 > .dt-cell--col-3').should('contain', 'Submitted'); let cell = cy.get('.dt-row-0 > .dt-cell--col-4'); + // select the cell cell.dblclick(); cell.get('.dt-cell__edit--col-4').findByRole('checkbox').check({ force: true }); + cy.wait('@value-update'); - cy.get('@doc').then(doc => { - cy.call('frappe.client.get_value', { - doctype: doc.doctype, - filters: { - name: doc.name, - }, - fieldname: 'enabled' - }).then(r => { - expect(r.message.enabled).to.equals(1); - }); + + cy.call('frappe.client.get_value', { + doctype: doctype_name, + filters: { + title: 'Doc 1', + }, + fieldname: 'enabled' + }).then(r => { + expect(r.message.enabled).to.equals(1); }); }); + + it('test load more with count selection buttons', () => { + cy.visit('/app/contact/view/report'); + + cy.get('.list-paging-area .list-count').should('contain.text', '20 of'); + cy.get('.list-paging-area .btn-more').click(); + cy.get('.list-paging-area .list-count').should('contain.text', '40 of'); + cy.get('.list-paging-area .btn-more').click(); + cy.get('.list-paging-area .list-count').should('contain.text', '60 of'); + + cy.get('.list-paging-area .btn-group .btn-paging[data-value="100"]').click(); + + cy.get('.list-paging-area .list-count').should('contain.text', '100 of'); + cy.get('.list-paging-area .btn-more').click(); + cy.get('.list-paging-area .list-count').should('contain.text', '200 of'); + cy.get('.list-paging-area .btn-more').click(); + cy.get('.list-paging-area .list-count').should('contain.text', '300 of'); + + // check if refresh works after load more + cy.get('.page-head .standard-actions [data-original-title="Refresh"]').click(); + cy.get('.list-paging-area .list-count').should('contain.text', '300 of'); + + cy.get('.list-paging-area .btn-group .btn-paging[data-value="500"]').click(); + + cy.get('.list-paging-area .list-count').should('contain.text', '500 of'); + cy.get('.list-paging-area .btn-more').click(); + cy.get('.list-paging-area .list-count').should('contain.text', '1000 of'); + }); }); \ No newline at end of file diff --git a/frappe/api.py b/frappe/api.py index e7f7bf5a04..226853c47b 100644 --- a/frappe/api.py +++ b/frappe/api.py @@ -159,7 +159,10 @@ def get_request_form_data(): else: data = frappe.local.form_dict.data - return frappe.parse_json(data) + try: + return frappe.parse_json(data) + except ValueError: + return frappe.local.form_dict def validate_auth(): @@ -208,7 +211,6 @@ def validate_oauth(authorization_header): pass - def validate_auth_via_api_keys(authorization_header): """ Authenticate request using API keys and set session user diff --git a/frappe/boot.py b/frappe/boot.py index 7f62d96cae..524913059c 100644 --- a/frappe/boot.py +++ b/frappe/boot.py @@ -89,6 +89,7 @@ def get_bootinfo(): bootinfo.additional_filters_config = get_additional_filters_from_hooks() bootinfo.desk_settings = get_desk_settings() bootinfo.app_logo_url = get_app_logo() + bootinfo.link_title_doctypes = get_link_title_doctypes() return bootinfo @@ -324,6 +325,15 @@ def get_desk_settings(): def get_notification_settings(): return frappe.get_cached_doc('Notification Settings', frappe.session.user) +def get_link_title_doctypes(): + dts = frappe.get_all("DocType", {"show_title_field_in_link": 1}) + custom_dts = frappe.get_all( + "Property Setter", + {"property": "show_title_field_in_link", "value": "1"}, + ["doc_type as name"], + ) + return [d.name for d in dts + custom_dts if d] + def set_time_zone(bootinfo): bootinfo.time_zone = { "system": get_time_zone(), diff --git a/frappe/build.py b/frappe/build.py index 6b93b8b93a..7a06ee3a22 100644 --- a/frappe/build.py +++ b/frappe/build.py @@ -1,25 +1,21 @@ -# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE import os -import re -import json import shutil +import re import subprocess +from distutils.spawn import find_executable from subprocess import getoutput -from io import StringIO from tempfile import mkdtemp, mktemp -from distutils.spawn import find_executable - -import frappe -from frappe.utils.minify import JavascriptMinify +from urllib.parse import urlparse import click import psutil -from urllib.parse import urlparse -from semantic_version import Version from requests import head from requests.exceptions import HTTPError +from semantic_version import Version +import frappe timestamps = {} app_paths = None @@ -32,6 +28,7 @@ class AssetsNotDownloadedError(Exception): class AssetsDontExistError(HTTPError): pass + def download_file(url, prefix): from requests import get @@ -277,12 +274,14 @@ def check_node_executable(): click.echo(f"{warn} Please install yarn using below command and try again.\nnpm install -g yarn") click.echo() + def get_node_env(): node_env = { "NODE_OPTIONS": f"--max_old_space_size={get_safe_max_old_space_size()}" } return node_env + def get_safe_max_old_space_size(): safe_max_old_space_size = 0 try: @@ -296,6 +295,7 @@ def get_safe_max_old_space_size(): return safe_max_old_space_size + def generate_assets_map(): symlinks = {} @@ -344,7 +344,6 @@ def clear_broken_symlinks(): os.remove(path) - def unstrip(message: str) -> str: """Pads input string on the right side until the last available column in the terminal """ @@ -397,94 +396,6 @@ def link_assets_dir(source, target, hard_link=False): symlink(source, target, overwrite=True) -def build(no_compress=False, verbose=False): - for target, sources in get_build_maps().items(): - pack(os.path.join(assets_path, target), sources, no_compress, verbose) - - -def get_build_maps(): - """get all build.jsons with absolute paths""" - # framework js and css files - - build_maps = {} - for app_path in app_paths: - path = os.path.join(app_path, "public", "build.json") - if os.path.exists(path): - with open(path) as f: - try: - for target, sources in (json.loads(f.read() or "{}")).items(): - # update app path - source_paths = [] - for source in sources: - if isinstance(source, list): - s = frappe.get_pymodule_path(source[0], *source[1].split("/")) - else: - s = os.path.join(app_path, source) - source_paths.append(s) - - build_maps[target] = source_paths - except ValueError as e: - print(path) - print("JSON syntax error {0}".format(str(e))) - return build_maps - - -def pack(target, sources, no_compress, verbose): - outtype, outtxt = target.split(".")[-1], "" - jsm = JavascriptMinify() - - for f in sources: - suffix = None - if ":" in f: - f, suffix = f.split(":") - if not os.path.exists(f) or os.path.isdir(f): - print("did not find " + f) - continue - timestamps[f] = os.path.getmtime(f) - try: - with open(f, "r") as sourcefile: - data = str(sourcefile.read(), "utf-8", errors="ignore") - - extn = f.rsplit(".", 1)[1] - - if ( - outtype == "js" - and extn == "js" - and (not no_compress) - and suffix != "concat" - and (".min." not in f) - ): - tmpin, tmpout = StringIO(data.encode("utf-8")), StringIO() - jsm.minify(tmpin, tmpout) - minified = tmpout.getvalue() - if minified: - outtxt += str(minified or "", "utf-8").strip("\n") + ";" - - if verbose: - print("{0}: {1}k".format(f, int(len(minified) / 1024))) - elif outtype == "js" and extn == "html": - # add to frappe.templates - outtxt += html_to_js_template(f, data) - else: - outtxt += "\n/*\n *\t%s\n */" % f - outtxt += "\n" + data + "\n" - - except Exception: - print("--Error in:" + f + "--") - print(frappe.get_traceback()) - - with open(target, "w") as f: - f.write(outtxt.encode("utf-8")) - - print("Wrote %s - %sk" % (target, str(int(os.path.getsize(target) / 1024)))) - - -def html_to_js_template(path, content): - """returns HTML template content as Javascript code, adding it to `frappe.templates`""" - return """frappe.templates["{key}"] = '{content}';\n""".format( - key=path.rsplit("/", 1)[-1][:-5], content=scrub_html_template(content)) - - def scrub_html_template(content): """Returns HTML content with removed whitespace and comments""" # remove whitespace to a single space @@ -496,37 +407,7 @@ def scrub_html_template(content): return content.replace("'", "\'") -def files_dirty(): - for target, sources in get_build_maps().items(): - for f in sources: - if ":" in f: - f, suffix = f.split(":") - if not os.path.exists(f) or os.path.isdir(f): - continue - if os.path.getmtime(f) != timestamps.get(f): - print(f + " dirty") - return True - else: - return False - - -def compile_less(): - if not find_executable("lessc"): - return - - for path in app_paths: - less_path = os.path.join(path, "public", "less") - if os.path.exists(less_path): - for fname in os.listdir(less_path): - if fname.endswith(".less") and fname != "variables.less": - fpath = os.path.join(less_path, fname) - mtime = os.path.getmtime(fpath) - if fpath in timestamps and mtime == timestamps[fpath]: - continue - - timestamps[fpath] = mtime - - print("compiling {0}".format(fpath)) - - css_path = os.path.join(path, "public", "css", fname.rsplit(".", 1)[0] + ".css") - os.system("lessc {0} > {1}".format(fpath, css_path)) +def html_to_js_template(path, content): + """returns HTML template content as Javascript code, adding it to `frappe.templates`""" + return """frappe.templates["{key}"] = '{content}';\n""".format( + key=path.rsplit("/", 1)[-1][:-5], content=scrub_html_template(content)) diff --git a/frappe/commands/site.py b/frappe/commands/site.py index 62488525b0..1684f26d49 100755 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -447,21 +447,17 @@ def disable_user(context, email): @pass_context def migrate(context, skip_failing=False, skip_search_index=False): "Run patches, sync schema and rebuild files/translations" - from frappe.migrate import migrate + from frappe.migrate import SiteMigration for site in context.sites: click.secho(f"Migrating {site}", fg="green") - frappe.init(site=site) - frappe.connect() try: - migrate( - context.verbose, + SiteMigration( skip_failing=skip_failing, - skip_search_index=skip_search_index - ) + skip_search_index=skip_search_index, + ).run(site=site) finally: print() - frappe.destroy() if not context.sites: raise SiteNotSpecifiedError diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index e3379a43aa..7246df8aa7 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -742,8 +742,9 @@ def run_ui_tests(context, app, headless=False, parallel=True, with_coverage=Fals @click.option('--profile', is_flag=True, default=False) @click.option('--noreload', "no_reload", is_flag=True, default=False) @click.option('--nothreading', "no_threading", is_flag=True, default=False) +@click.option('--with-coverage', is_flag=True, default=False) @pass_context -def serve(context, port=None, profile=False, no_reload=False, no_threading=False, sites_path='.', site=None): +def serve(context, port=None, profile=False, no_reload=False, no_threading=False, sites_path='.', site=None, with_coverage=False): "Start development web server" import frappe.app @@ -751,8 +752,12 @@ def serve(context, port=None, profile=False, no_reload=False, no_threading=False site = None else: site = context.sites[0] - - frappe.app.serve(port=port, profile=profile, no_reload=no_reload, no_threading=no_threading, site=site, sites_path='.') + with CodeCoverage(with_coverage, 'frappe'): + if with_coverage: + # unable to track coverage with threading enabled + no_threading = True + no_reload = True + frappe.app.serve(port=port, profile=profile, no_reload=no_reload, no_threading=no_threading, site=site, sites_path='.') @click.command('request') diff --git a/frappe/core/doctype/communication/communication.py b/frappe/core/doctype/communication/communication.py index 1ab07d92e4..f89f0d8765 100644 --- a/frappe/core/doctype/communication/communication.py +++ b/frappe/core/doctype/communication/communication.py @@ -2,6 +2,7 @@ # License: MIT. See LICENSE from collections import Counter +from typing import List import frappe from frappe import _ from frappe.model.document import Document @@ -367,15 +368,8 @@ def get_permission_query_conditions_for_communication(user): return """`tabCommunication`.email_account in ({email_accounts})"""\ .format(email_accounts=','.join(email_accounts)) -def get_contacts(email_strings, auto_create_contact=False): - email_addrs = [] - - for email_string in email_strings: - if email_string: - result = getaddresses([email_string]) - for email in result: - email_addrs.append(email[1]) - +def get_contacts(email_strings: List[str], auto_create_contact=False) -> List[str]: + email_addrs = get_emails(email_strings) contacts = [] for email in email_addrs: email = get_email_without_link(email) @@ -404,6 +398,17 @@ def get_contacts(email_strings, auto_create_contact=False): return contacts +def get_emails(email_strings: List[str]) -> List[str]: + email_addrs = [] + + for email_string in email_strings: + if email_string: + result = getaddresses([email_string]) + for email in result: + email_addrs.append(email[1]) + + return email_addrs + def add_contact_links_to_communication(communication, contact_name): contact_links = frappe.get_all("Dynamic Link", filters={ "parenttype": "Contact", @@ -449,8 +454,12 @@ def get_email_without_link(email): if not frappe.get_all("Email Account", filters={"enable_automatic_linking": 1}): return email - email_id = email.split("@")[0].split("+")[0] - email_host = email.split("@")[1] + try: + _email = email.split("@") + email_id = _email[0].split("+")[0] + email_host = _email[1] + except IndexError: + return email return "{0}@{1}".format(email_id, email_host) diff --git a/frappe/core/doctype/communication/test_communication.py b/frappe/core/doctype/communication/test_communication.py index f26e70771b..d933c2f494 100644 --- a/frappe/core/doctype/communication/test_communication.py +++ b/frappe/core/doctype/communication/test_communication.py @@ -5,6 +5,7 @@ from urllib.parse import quote import frappe from frappe.email.doctype.email_queue.email_queue import EmailQueue +from frappe.core.doctype.communication.communication import get_emails test_records = frappe.get_test_records('Communication') @@ -201,6 +202,19 @@ class TestCommunication(unittest.TestCase): self.assertIn(("Note", note.name), doc_links) + def parse_emails(self): + emails = get_emails( + [ + 'comm_recipient+DocType+DocName@example.com', + '"First, LastName" ', + 'test@user.com' + ] + ) + + self.assertEqual(emails[0], "comm_recipient+DocType+DocName@example.com") + self.assertEqual(emails[1], "first.lastname@email.com") + self.assertEqual(emails[2], "test@user.com") + class TestCommunicationEmailMixin(unittest.TestCase): def new_communication(self, recipients=None, cc=None, bcc=None): recipients = ', '.join(recipients or []) diff --git a/frappe/core/doctype/docfield/docfield.json b/frappe/core/doctype/docfield/docfield.json index 26ddce7d35..6eb8cf347f 100644 --- a/frappe/core/doctype/docfield/docfield.json +++ b/frappe/core/doctype/docfield/docfield.json @@ -17,6 +17,7 @@ "hide_days", "hide_seconds", "reqd", + "is_virtual", "search_index", "column_break_18", "options", @@ -534,13 +535,19 @@ "fieldname": "show_dashboard", "fieldtype": "Check", "label": "Show Dashboard" + }, + { + "default": "0", + "fieldname": "is_virtual", + "fieldtype": "Check", + "label": "Virtual" } ], "idx": 1, "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2022-01-03 11:56:19.812863", + "modified": "2022-01-27 21:22:20.529072", "modified_by": "Administrator", "module": "Core", "name": "DocField", diff --git a/frappe/core/doctype/doctype/doctype.json b/frappe/core/doctype/doctype/doctype.json index 03e3b65ea1..2bba4127bb 100644 --- a/frappe/core/doctype/doctype/doctype.json +++ b/frappe/core/doctype/doctype/doctype.json @@ -46,6 +46,7 @@ "allow_auto_repeat", "view_settings", "title_field", + "show_title_field_in_link", "search_fields", "default_print_format", "sort_field", @@ -582,6 +583,12 @@ "fieldname": "document_states_section", "fieldtype": "Section Break", "label": "Document States" + }, + { + "default": "0", + "fieldname": "show_title_field_in_link", + "fieldtype": "Check", + "label": "Show Title in Link Fields" } ], "icon": "fa fa-bolt", @@ -663,7 +670,7 @@ "link_fieldname": "reference_doctype" } ], - "modified": "2021-12-09 14:53:10.717788", + "modified": "2022-01-07 16:07:06.196534", "modified_by": "Administrator", "module": "Core", "name": "DocType", diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index d259367a16..6d0409521e 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -781,28 +781,30 @@ def validate_series(dt, autoname=None, name=None): def validate_links_table_fieldnames(meta): """Validate fieldnames in Links table""" - if frappe.flags.in_patch: return - if frappe.flags.in_fixtures: return - if not meta.links: return + if not meta.links or frappe.flags.in_patch or frappe.flags.in_fixtures: + return - for index, link in enumerate(meta.links): + fieldnames = tuple(field.fieldname for field in meta.fields) + for index, link in enumerate(meta.links, 1): link_meta = frappe.get_meta(link.link_doctype) if not link_meta.get_field(link.link_fieldname): - message = _("Document Links Row #{0}: Could not find field {1} in {2} DocType").format(index+1, frappe.bold(link.link_fieldname), frappe.bold(link.link_doctype)) + message = _("Document Links Row #{0}: Could not find field {1} in {2} DocType").format(index, frappe.bold(link.link_fieldname), frappe.bold(link.link_doctype)) frappe.throw(message, InvalidFieldNameError, _("Invalid Fieldname")) - if link.is_child_table and not meta.get_field(link.table_fieldname): - message = _("Document Links Row #{0}: Could not find field {1} in {2} DocType").format(index+1, frappe.bold(link.table_fieldname), frappe.bold(meta.name)) - frappe.throw(message, frappe.ValidationError, _("Invalid Table Fieldname")) + if not link.is_child_table: + continue - if link.is_child_table: - if not link.parent_doctype: - message = _("Document Links Row #{0}: Parent DocType is mandatory for internal links").format(index+1) - frappe.throw(message, frappe.ValidationError, _("Parent Missing")) + if not link.parent_doctype: + message = _("Document Links Row #{0}: Parent DocType is mandatory for internal links").format(index) + frappe.throw(message, frappe.ValidationError, _("Parent Missing")) - if not link.table_fieldname: - message = _("Document Links Row #{0}: Table Fieldname is mandatory for internal links").format(index+1) - frappe.throw(message, frappe.ValidationError, _("Table Fieldname Missing")) + if not link.table_fieldname: + message = _("Document Links Row #{0}: Table Fieldname is mandatory for internal links").format(index) + frappe.throw(message, frappe.ValidationError, _("Table Fieldname Missing")) + + if link.table_fieldname not in fieldnames: + message = _("Document Links Row #{0}: Could not find field {1} in {2} DocType").format(index, frappe.bold(link.table_fieldname), frappe.bold(meta.name)) + frappe.throw(message, frappe.ValidationError, _("Invalid Table Fieldname")) def validate_fields_for_doctype(doctype): meta = frappe.get_meta(doctype, cached=False) @@ -1076,6 +1078,9 @@ def validate_fields(meta): field.fetch_from = field.fetch_from.strip('\n').strip() def validate_data_field_type(docfield): + if docfield.get("is_virtual"): + return + if docfield.fieldtype == "Data" and not (docfield.oldfieldtype and docfield.oldfieldtype != "Data"): if docfield.options and (docfield.options not in data_field_options): df_str = frappe.bold(_(docfield.label)) @@ -1321,10 +1326,9 @@ def make_module_and_roles(doc, perm_fieldname="permissions"): else: raise -def check_fieldname_conflicts(doctype, fieldname): +def check_fieldname_conflicts(docfield): """Checks if fieldname conflicts with methods or properties""" - - doc = frappe.get_doc({"doctype": doctype}) + doc = frappe.get_doc({"doctype": docfield.dt}) available_objects = [x for x in dir(doc) if isinstance(x, str)] property_list = [ x for x in available_objects if isinstance(getattr(type(doc), x, None), property) @@ -1332,9 +1336,10 @@ def check_fieldname_conflicts(doctype, fieldname): method_list = [ x for x in available_objects if x not in property_list and callable(getattr(doc, x)) ] + msg = _("Fieldname {0} conflicting with meta object").format(docfield.fieldname) - if fieldname in method_list + property_list: - frappe.throw(_("Fieldname {0} conflicting with meta object").format(fieldname)) + if docfield.fieldname in method_list + property_list: + frappe.msgprint(msg, raise_exception=not docfield.is_virtual) def clear_linked_doctype_cache(): frappe.cache().delete_value('linked_doctypes_without_ignore_user_permissions_enabled') diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 2808a2710b..50a7b31bca 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -745,7 +745,7 @@ def delete_file(path): """Delete file from `public folder`""" if path: if ".." in path.split("/"): - frappe.msgprint(_("It is risky to delete this file: {0}. Please contact your System Manager.").format(path)) + frappe.throw(_("It is risky to delete this file: {0}. Please contact your System Manager.").format(path)) parts = os.path.split(path.strip("/")) if parts[0]=="files": diff --git a/frappe/core/doctype/report/test_report.py b/frappe/core/doctype/report/test_report.py index 36e3b09254..bf63afa5c5 100644 --- a/frappe/core/doctype/report/test_report.py +++ b/frappe/core/doctype/report/test_report.py @@ -3,8 +3,10 @@ import frappe, json, os import unittest -from frappe.desk.query_report import run, save_report +from frappe.desk.query_report import run, save_report, add_total_row +from frappe.desk.reportview import delete_report, save_report as _save_report from frappe.custom.doctype.customize_form.customize_form import reset_customization +from frappe.core.doctype.user_permission.test_user_permission import create_user test_records = frappe.get_test_records('Report') test_dependencies = ['User'] @@ -30,6 +32,60 @@ class TestReport(unittest.TestCase): self.assertEqual(columns[1].get('label'), 'Module') self.assertTrue('User' in [d.get('name') for d in data]) + def test_save_or_delete_report(self): + '''Test for validations when editing / deleting report of type Report Builder''' + + try: + report = frappe.get_doc({ + 'doctype': 'Report', + 'ref_doctype': 'User', + 'report_name': 'Test Delete Report', + 'report_type': 'Report Builder', + 'is_standard': 'No', + }).insert() + + # Check for PermissionError + create_user("test_report_owner@example.com", "Website Manager") + frappe.set_user("test_report_owner@example.com") + self.assertRaises(frappe.PermissionError, delete_report, report.name) + + # Check for Report Type + frappe.set_user("Administrator") + report.db_set("report_type", "Custom Report") + self.assertRaisesRegex( + frappe.ValidationError, + "Only reports of type Report Builder can be deleted", + delete_report, + report.name + ) + + # Check if creating and deleting works with proper validations + frappe.set_user("test@example.com") + report_name = _save_report( + 'Dummy Report', + 'User', + json.dumps([{ + 'fieldname': 'email', + 'fieldtype': 'Data', + 'label': 'Email', + 'insert_after_index': 0, + 'link_field': 'name', + 'doctype': 'User', + 'options': 'Email', + 'width': 100, + 'id':'email', + 'name': 'Email' + }]) + ) + + doc = frappe.get_doc("Report", report_name) + delete_report(doc.name) + + finally: + frappe.set_user("Administrator") + frappe.db.rollback() + + def test_custom_report(self): reset_customization('User') custom_report_name = save_report( @@ -226,3 +282,55 @@ result = [ # Set user back to administrator frappe.set_user('Administrator') + + def test_add_total_row_for_tree_reports(self): + report_settings = { + 'tree': True, + 'parent_field': 'parent_value' + } + + columns = [ + { + "fieldname": "parent_column", + "label": "Parent Column", + "fieldtype": "Data", + "width": 10 + }, + { + "fieldname": "column_1", + "label": "Column 1", + "fieldtype": "Float", + "width": 10 + }, + { + "fieldname": "column_2", + "label": "Column 2", + "fieldtype": "Float", + "width": 10 + } + ] + + result = [ + { + "parent_column": "Parent 1", + "column_1": 200, + "column_2": 150.50 + }, + { + "parent_column": "Child 1", + "column_1": 100, + "column_2": 75.25, + "parent_value": "Parent 1" + }, + { + "parent_column": "Child 2", + "column_1": 100, + "column_2": 75.25, + "parent_value": "Parent 1" + } + ] + + result = add_total_row(result, columns, meta=None, report_settings=report_settings) + self.assertEqual(result[-1][0], "Total") + self.assertEqual(result[-1][1], 200) + self.assertEqual(result[-1][2], 150.50) diff --git a/frappe/core/page/permission_manager/permission_manager.js b/frappe/core/page/permission_manager/permission_manager.js index 6b427fdebf..cb218b2eae 100644 --- a/frappe/core/page/permission_manager/permission_manager.js +++ b/frappe/core/page/permission_manager/permission_manager.js @@ -347,6 +347,7 @@ frappe.PermissionEngine = class PermissionEngine { } add_check_events() { + let me = this; this.body.on("click", ".show-user-permissions", () => { frappe.route_options = { allow: this.get_doctype() || "" }; frappe.set_route('List', 'User Permission'); @@ -373,7 +374,7 @@ frappe.PermissionEngine = class PermissionEngine { // exception: reverse chk.prop("checked", !chk.prop("checked")); } else { - this.get_perm(args.role)[args.ptype] = args.value; + me.get_perm(args.role)[args.ptype] = args.value; } } }); diff --git a/frappe/coverage.py b/frappe/coverage.py index 1969cae141..5f89800deb 100644 --- a/frappe/coverage.py +++ b/frappe/coverage.py @@ -29,6 +29,7 @@ FRAPPE_EXCLUSIONS = [ "*/commands/*", "*/frappe/change_log/*", "*/frappe/exceptions*", + "*/frappe/coverage.py", "*frappe/setup.py", "*/doctype/*/*_dashboard.py", "*/patches/*", diff --git a/frappe/custom/doctype/custom_field/custom_field.json b/frappe/custom/doctype/custom_field/custom_field.json index 235f11aad8..e51dfda14b 100644 --- a/frappe/custom/doctype/custom_field/custom_field.json +++ b/frappe/custom/doctype/custom_field/custom_field.json @@ -1,458 +1,468 @@ { - "actions": [], - "allow_import": 1, - "creation": "2013-01-10 16:34:01", - "description": "Adds a custom field to a DocType", - "doctype": "DocType", - "document_type": "Setup", - "engine": "InnoDB", - "field_order": [ - "dt", - "module", - "label", - "label_help", - "fieldname", - "insert_after", - "length", - "column_break_6", - "fieldtype", - "precision", - "hide_seconds", - "hide_days", - "options", - "fetch_from", - "fetch_if_empty", - "options_help", - "section_break_11", - "collapsible", - "collapsible_depends_on", - "default", - "depends_on", - "mandatory_depends_on", - "read_only_depends_on", - "properties", - "non_negative", - "reqd", - "unique", - "read_only", - "ignore_user_permissions", - "hidden", - "print_hide", - "print_hide_if_no_value", - "print_width", - "no_copy", - "allow_on_submit", - "in_list_view", - "in_standard_filter", - "in_global_search", - "in_preview", - "bold", - "report_hide", - "search_index", - "allow_in_quick_entry", - "ignore_xss_filter", - "translatable", - "hide_border", - "description", - "permlevel", - "width", - "columns" - ], - "fields": [{ - "bold": 1, - "fieldname": "dt", - "fieldtype": "Link", - "in_filter": 1, - "in_list_view": 1, - "label": "Document", - "oldfieldname": "dt", - "oldfieldtype": "Link", - "options": "DocType", - "reqd": 1, - "search_index": 1 - }, - { - "bold": 1, - "fieldname": "label", - "fieldtype": "Data", - "in_filter": 1, - "label": "Label", - "no_copy": 1, - "oldfieldname": "label", - "oldfieldtype": "Data" - }, - { - "fieldname": "label_help", - "fieldtype": "HTML", - "label": "Label Help", - "oldfieldtype": "HTML" - }, - { - "fieldname": "fieldname", - "fieldtype": "Data", - "in_list_view": 1, - "label": "Fieldname", - "no_copy": 1, - "oldfieldname": "fieldname", - "oldfieldtype": "Data", - "read_only": 1 - }, - { - "description": "Select the label after which you want to insert new field.", - "fieldname": "insert_after", - "fieldtype": "Select", - "label": "Insert After", - "no_copy": 1, - "oldfieldname": "insert_after", - "oldfieldtype": "Select" - }, - { - "fieldname": "column_break_6", - "fieldtype": "Column Break" - }, - { - "bold": 1, - "default": "Data", - "fieldname": "fieldtype", - "fieldtype": "Select", - "in_filter": 1, - "in_list_view": 1, - "label": "Field Type", - "oldfieldname": "fieldtype", - "oldfieldtype": "Select", - "options": "Attach\nAttach Image\nBarcode\nButton\nCheck\nCode\nColor\nColumn Break\nCurrency\nData\nDate\nDatetime\nDuration\nDynamic Link\nFloat\nFold\nGeolocation\nHeading\nHTML\nHTML Editor\nIcon\nImage\nInt\nLink\nLong Text\nMarkdown Editor\nPassword\nPercent\nRead Only\nRating\nSection Break\nSelect\nSmall Text\nTable\nTable MultiSelect\nText\nText Editor\nTime\nSignature\nTab Break", - "reqd": 1 - }, - { - "depends_on": "eval:in_list([\"Float\", \"Currency\", \"Percent\"], doc.fieldtype)", - "description": "Set non-standard precision for a Float or Currency field", - "fieldname": "precision", - "fieldtype": "Select", - "label": "Precision", - "options": "\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9" - }, - { - "fieldname": "options", - "fieldtype": "Small Text", - "in_list_view": 1, - "label": "Options", - "oldfieldname": "options", - "oldfieldtype": "Text" - }, - { - "fieldname": "fetch_from", - "fieldtype": "Small Text", - "label": "Fetch From" - }, - { - "default": "0", - "description": "If checked, this field will be not overwritten based on Fetch From if a value already exists.", - "fieldname": "fetch_if_empty", - "fieldtype": "Check", - "label": "Fetch If Empty" - }, - { - "fieldname": "options_help", - "fieldtype": "HTML", - "label": "Options Help", - "oldfieldtype": "HTML" - }, - { - "fieldname": "section_break_11", - "fieldtype": "Section Break" - }, - { - "default": "0", - "depends_on": "eval:doc.fieldtype==\"Section Break\"", - "fieldname": "collapsible", - "fieldtype": "Check", - "label": "Collapsible" - }, - { - "depends_on": "eval:doc.fieldtype==\"Section Break\"", - "fieldname": "collapsible_depends_on", - "fieldtype": "Code", - "label": "Collapsible Depends On" - }, - { - "fieldname": "default", - "fieldtype": "Text", - "label": "Default Value", - "oldfieldname": "default", - "oldfieldtype": "Text" - }, - { - "fieldname": "depends_on", - "fieldtype": "Code", - "label": "Depends On", - "length": 255 - }, - { - "fieldname": "description", - "fieldtype": "Text", - "label": "Field Description", - "oldfieldname": "description", - "oldfieldtype": "Text", - "print_width": "300px", - "width": "300px" - }, - { - "default": "0", - "fieldname": "permlevel", - "fieldtype": "Int", - "label": "Permission Level", - "oldfieldname": "permlevel", - "oldfieldtype": "Int" - }, - { - "fieldname": "width", - "fieldtype": "Data", - "label": "Width", - "oldfieldname": "width", - "oldfieldtype": "Data" - }, - { - "description": "Number of columns for a field in a List View or a Grid (Total Columns should be less than 11)", - "fieldname": "columns", - "fieldtype": "Int", - "label": "Columns" - }, - { - "fieldname": "properties", - "fieldtype": "Column Break", - "oldfieldtype": "Column Break", - "print_width": "50%", - "width": "50%" - }, - { - "default": "0", - "fieldname": "reqd", - "fieldtype": "Check", - "in_list_view": 1, - "label": "Is Mandatory Field", - "oldfieldname": "reqd", - "oldfieldtype": "Check" - }, - { - "default": "0", - "fieldname": "unique", - "fieldtype": "Check", - "label": "Unique" - }, - { - "default": "0", - "fieldname": "read_only", - "fieldtype": "Check", - "label": "Read Only" - }, - { - "default": "0", - "depends_on": "eval:doc.fieldtype===\"Link\"", - "fieldname": "ignore_user_permissions", - "fieldtype": "Check", - "label": "Ignore User Permissions" - }, - { - "default": "0", - "fieldname": "hidden", - "fieldtype": "Check", - "label": "Hidden" - }, - { - "default": "0", - "fieldname": "print_hide", - "fieldtype": "Check", - "label": "Print Hide", - "oldfieldname": "print_hide", - "oldfieldtype": "Check" - }, - { - "default": "0", - "depends_on": "eval:[\"Int\", \"Float\", \"Currency\", \"Percent\"].indexOf(doc.fieldtype)!==-1", - "fieldname": "print_hide_if_no_value", - "fieldtype": "Check", - "label": "Print Hide If No Value" - }, - { - "fieldname": "print_width", - "fieldtype": "Data", - "hidden": 1, - "label": "Print Width", - "no_copy": 1, - "print_hide": 1 - }, - { - "default": "0", - "fieldname": "no_copy", - "fieldtype": "Check", - "label": "No Copy", - "oldfieldname": "no_copy", - "oldfieldtype": "Check" - }, - { - "default": "0", - "fieldname": "allow_on_submit", - "fieldtype": "Check", - "label": "Allow on Submit", - "oldfieldname": "allow_on_submit", - "oldfieldtype": "Check" - }, - { - "default": "0", - "fieldname": "in_list_view", - "fieldtype": "Check", - "label": "In List View" - }, - { - "default": "0", - "fieldname": "in_standard_filter", - "fieldtype": "Check", - "label": "In Standard Filter" - }, - { - "default": "0", - "depends_on": "eval:([\"Data\", \"Select\", \"Table\", \"Text\", \"Text Editor\", \"Link\", \"Small Text\", \"Long Text\", \"Read Only\", \"Heading\", \"Dynamic Link\"].indexOf(doc.fieldtype) !== -1)", - "fieldname": "in_global_search", - "fieldtype": "Check", - "label": "In Global Search" - }, - { - "default": "0", - "fieldname": "bold", - "fieldtype": "Check", - "label": "Bold" - }, - { - "default": "0", - "fieldname": "report_hide", - "fieldtype": "Check", - "label": "Report Hide", - "oldfieldname": "report_hide", - "oldfieldtype": "Check" - }, - { - "default": "0", - "fieldname": "search_index", - "fieldtype": "Check", - "hidden": 1, - "label": "Index", - "no_copy": 1, - "print_hide": 1 - }, - { - "default": "0", - "description": "Don't HTML Encode HTML tags like <script> or just characters like < or >, as they could be intentionally used in this field", - "fieldname": "ignore_xss_filter", - "fieldtype": "Check", - "label": "Ignore XSS Filter" - }, - { - "default": "1", - "depends_on": "eval:['Data', 'Select', 'Text', 'Small Text', 'Text Editor'].includes(doc.fieldtype)", - "fieldname": "translatable", - "fieldtype": "Check", - "label": "Translatable" - }, - { - "depends_on": "eval:in_list(['Data', 'Link', 'Dynamic Link', 'Password', 'Select', 'Read Only', 'Attach', 'Attach Image', 'Int'], doc.fieldtype)", - "fieldname": "length", - "fieldtype": "Int", - "label": "Length" - }, - { - "fieldname": "mandatory_depends_on", - "fieldtype": "Code", - "label": "Mandatory Depends On", - "length": 255 - }, - { - "fieldname": "read_only_depends_on", - "fieldtype": "Code", - "label": "Read Only Depends On", - "length": 255 - }, - { - "default": "0", - "fieldname": "allow_in_quick_entry", - "fieldtype": "Check", - "label": "Allow in Quick Entry" - }, - { - "default": "0", - "depends_on": "eval:!in_list(['Table', 'Table MultiSelect'], doc.fieldtype);", - "fieldname": "in_preview", - "fieldtype": "Check", - "label": "In Preview" - }, - { - "default": "0", - "depends_on": "eval:doc.fieldtype=='Duration'", - "fieldname": "hide_seconds", - "fieldtype": "Check", - "label": "Hide Seconds" - }, - { - "default": "0", - "depends_on": "eval:doc.fieldtype=='Duration'", - "fieldname": "hide_days", - "fieldtype": "Check", - "label": "Hide Days" - }, - { - "default": "0", - "depends_on": "eval:doc.fieldtype=='Section Break'", - "fieldname": "hide_border", - "fieldtype": "Check", - "label": "Hide Border" - }, - { - "default": "0", - "depends_on": "eval:in_list([\"Int\", \"Float\", \"Currency\"], doc.fieldtype)", - "fieldname": "non_negative", - "fieldtype": "Check", - "label": "Non Negative" - }, - { - "fieldname": "module", - "fieldtype": "Link", - "label": "Module (for export)", - "options": "Module Def" - } - ], - "icon": "fa fa-glass", - "idx": 1, - "index_web_pages_for_search": 1, - "links": [], - "modified": "2021-09-04 12:45:23.810120", - "modified_by": "Administrator", - "module": "Custom", - "name": "Custom Field", - "owner": "Administrator", - "permissions": [{ - "create": 1, - "delete": 1, - "email": 1, - "print": 1, - "read": 1, - "report": 1, - "role": "Administrator", - "share": 1, - "write": 1 - }, - { - "create": 1, - "delete": 1, - "email": 1, - "print": 1, - "read": 1, - "report": 1, - "role": "System Manager", - "share": 1, - "write": 1 - } - ], - "search_fields": "dt,label,fieldtype,options", - "sort_field": "modified", - "sort_order": "ASC", - "track_changes": 1 + "actions": [], + "allow_import": 1, + "creation": "2013-01-10 16:34:01", + "description": "Adds a custom field to a DocType", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "dt", + "module", + "label", + "label_help", + "fieldname", + "insert_after", + "length", + "column_break_6", + "fieldtype", + "precision", + "hide_seconds", + "hide_days", + "options", + "fetch_from", + "fetch_if_empty", + "options_help", + "section_break_11", + "collapsible", + "collapsible_depends_on", + "default", + "depends_on", + "mandatory_depends_on", + "read_only_depends_on", + "properties", + "non_negative", + "reqd", + "unique", + "is_virtual", + "read_only", + "ignore_user_permissions", + "hidden", + "print_hide", + "print_hide_if_no_value", + "print_width", + "no_copy", + "allow_on_submit", + "in_list_view", + "in_standard_filter", + "in_global_search", + "in_preview", + "bold", + "report_hide", + "search_index", + "allow_in_quick_entry", + "ignore_xss_filter", + "translatable", + "hide_border", + "description", + "permlevel", + "width", + "columns" + ], + "fields": [ + { + "bold": 1, + "fieldname": "dt", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "label": "Document", + "oldfieldname": "dt", + "oldfieldtype": "Link", + "options": "DocType", + "reqd": 1, + "search_index": 1 + }, + { + "bold": 1, + "fieldname": "label", + "fieldtype": "Data", + "in_filter": 1, + "label": "Label", + "no_copy": 1, + "oldfieldname": "label", + "oldfieldtype": "Data" + }, + { + "fieldname": "label_help", + "fieldtype": "HTML", + "label": "Label Help", + "oldfieldtype": "HTML" + }, + { + "fieldname": "fieldname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Fieldname", + "no_copy": 1, + "oldfieldname": "fieldname", + "oldfieldtype": "Data", + "read_only": 1 + }, + { + "description": "Select the label after which you want to insert new field.", + "fieldname": "insert_after", + "fieldtype": "Select", + "label": "Insert After", + "no_copy": 1, + "oldfieldname": "insert_after", + "oldfieldtype": "Select" + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "bold": 1, + "default": "Data", + "fieldname": "fieldtype", + "fieldtype": "Select", + "in_filter": 1, + "in_list_view": 1, + "label": "Field Type", + "oldfieldname": "fieldtype", + "oldfieldtype": "Select", + "options": "Attach\nAttach Image\nBarcode\nButton\nCheck\nCode\nColor\nColumn Break\nCurrency\nData\nDate\nDatetime\nDuration\nDynamic Link\nFloat\nFold\nGeolocation\nHeading\nHTML\nHTML Editor\nIcon\nImage\nInt\nLink\nLong Text\nMarkdown Editor\nPassword\nPercent\nRead Only\nRating\nSection Break\nSelect\nSmall Text\nTable\nTable MultiSelect\nText\nText Editor\nTime\nSignature\nTab Break", + "reqd": 1 + }, + { + "depends_on": "eval:in_list([\"Float\", \"Currency\", \"Percent\"], doc.fieldtype)", + "description": "Set non-standard precision for a Float or Currency field", + "fieldname": "precision", + "fieldtype": "Select", + "label": "Precision", + "options": "\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9" + }, + { + "fieldname": "options", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Options", + "oldfieldname": "options", + "oldfieldtype": "Text" + }, + { + "fieldname": "fetch_from", + "fieldtype": "Small Text", + "label": "Fetch From" + }, + { + "default": "0", + "description": "If checked, this field will be not overwritten based on Fetch From if a value already exists.", + "fieldname": "fetch_if_empty", + "fieldtype": "Check", + "label": "Fetch If Empty" + }, + { + "fieldname": "options_help", + "fieldtype": "HTML", + "label": "Options Help", + "oldfieldtype": "HTML" + }, + { + "fieldname": "section_break_11", + "fieldtype": "Section Break" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype==\"Section Break\"", + "fieldname": "collapsible", + "fieldtype": "Check", + "label": "Collapsible" + }, + { + "depends_on": "eval:doc.fieldtype==\"Section Break\"", + "fieldname": "collapsible_depends_on", + "fieldtype": "Code", + "label": "Collapsible Depends On" + }, + { + "fieldname": "default", + "fieldtype": "Text", + "label": "Default Value", + "oldfieldname": "default", + "oldfieldtype": "Text" + }, + { + "fieldname": "depends_on", + "fieldtype": "Code", + "label": "Depends On", + "length": 255 + }, + { + "fieldname": "description", + "fieldtype": "Text", + "label": "Field Description", + "oldfieldname": "description", + "oldfieldtype": "Text", + "print_width": "300px", + "width": "300px" + }, + { + "default": "0", + "fieldname": "permlevel", + "fieldtype": "Int", + "label": "Permission Level", + "oldfieldname": "permlevel", + "oldfieldtype": "Int" + }, + { + "fieldname": "width", + "fieldtype": "Data", + "label": "Width", + "oldfieldname": "width", + "oldfieldtype": "Data" + }, + { + "description": "Number of columns for a field in a List View or a Grid (Total Columns should be less than 11)", + "fieldname": "columns", + "fieldtype": "Int", + "label": "Columns" + }, + { + "fieldname": "properties", + "fieldtype": "Column Break", + "oldfieldtype": "Column Break", + "print_width": "50%", + "width": "50%" + }, + { + "default": "0", + "fieldname": "reqd", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Mandatory Field", + "oldfieldname": "reqd", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "unique", + "fieldtype": "Check", + "label": "Unique" + }, + { + "default": "0", + "fieldname": "is_virtual", + "fieldtype": "Check", + "label": "Is Virtual" + }, + { + "default": "0", + "fieldname": "read_only", + "fieldtype": "Check", + "label": "Read Only" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype===\"Link\"", + "fieldname": "ignore_user_permissions", + "fieldtype": "Check", + "label": "Ignore User Permissions" + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden" + }, + { + "default": "0", + "fieldname": "print_hide", + "fieldtype": "Check", + "label": "Print Hide", + "oldfieldname": "print_hide", + "oldfieldtype": "Check" + }, + { + "default": "0", + "depends_on": "eval:[\"Int\", \"Float\", \"Currency\", \"Percent\"].indexOf(doc.fieldtype)!==-1", + "fieldname": "print_hide_if_no_value", + "fieldtype": "Check", + "label": "Print Hide If No Value" + }, + { + "fieldname": "print_width", + "fieldtype": "Data", + "hidden": 1, + "label": "Print Width", + "no_copy": 1, + "print_hide": 1 + }, + { + "default": "0", + "fieldname": "no_copy", + "fieldtype": "Check", + "label": "No Copy", + "oldfieldname": "no_copy", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "allow_on_submit", + "fieldtype": "Check", + "label": "Allow on Submit", + "oldfieldname": "allow_on_submit", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "in_list_view", + "fieldtype": "Check", + "label": "In List View" + }, + { + "default": "0", + "fieldname": "in_standard_filter", + "fieldtype": "Check", + "label": "In Standard Filter" + }, + { + "default": "0", + "depends_on": "eval:([\"Data\", \"Select\", \"Table\", \"Text\", \"Text Editor\", \"Link\", \"Small Text\", \"Long Text\", \"Read Only\", \"Heading\", \"Dynamic Link\"].indexOf(doc.fieldtype) !== -1)", + "fieldname": "in_global_search", + "fieldtype": "Check", + "label": "In Global Search" + }, + { + "default": "0", + "fieldname": "bold", + "fieldtype": "Check", + "label": "Bold" + }, + { + "default": "0", + "fieldname": "report_hide", + "fieldtype": "Check", + "label": "Report Hide", + "oldfieldname": "report_hide", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "search_index", + "fieldtype": "Check", + "hidden": 1, + "label": "Index", + "no_copy": 1, + "print_hide": 1 + }, + { + "default": "0", + "description": "Don't HTML Encode HTML tags like <script> or just characters like < or >, as they could be intentionally used in this field", + "fieldname": "ignore_xss_filter", + "fieldtype": "Check", + "label": "Ignore XSS Filter" + }, + { + "default": "1", + "depends_on": "eval:['Data', 'Select', 'Text', 'Small Text', 'Text Editor'].includes(doc.fieldtype)", + "fieldname": "translatable", + "fieldtype": "Check", + "label": "Translatable" + }, + { + "depends_on": "eval:in_list(['Data', 'Link', 'Dynamic Link', 'Password', 'Select', 'Read Only', 'Attach', 'Attach Image', 'Int'], doc.fieldtype)", + "fieldname": "length", + "fieldtype": "Int", + "label": "Length" + }, + { + "fieldname": "mandatory_depends_on", + "fieldtype": "Code", + "label": "Mandatory Depends On", + "length": 255 + }, + { + "fieldname": "read_only_depends_on", + "fieldtype": "Code", + "label": "Read Only Depends On", + "length": 255 + }, + { + "default": "0", + "fieldname": "allow_in_quick_entry", + "fieldtype": "Check", + "label": "Allow in Quick Entry" + }, + { + "default": "0", + "depends_on": "eval:!in_list(['Table', 'Table MultiSelect'], doc.fieldtype);", + "fieldname": "in_preview", + "fieldtype": "Check", + "label": "In Preview" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype=='Duration'", + "fieldname": "hide_seconds", + "fieldtype": "Check", + "label": "Hide Seconds" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype=='Duration'", + "fieldname": "hide_days", + "fieldtype": "Check", + "label": "Hide Days" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype=='Section Break'", + "fieldname": "hide_border", + "fieldtype": "Check", + "label": "Hide Border" + }, + { + "default": "0", + "depends_on": "eval:in_list([\"Int\", \"Float\", \"Currency\"], doc.fieldtype)", + "fieldname": "non_negative", + "fieldtype": "Check", + "label": "Non Negative" + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module (for export)", + "options": "Module Def" + } + ], + "icon": "fa fa-glass", + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2022-01-27 21:47:01.065556", + "modified_by": "Administrator", + "module": "Custom", + "name": "Custom Field", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "search_fields": "dt,label,fieldtype,options", + "sort_field": "modified", + "sort_order": "ASC", + "states": [], + "track_changes": 1 } \ No newline at end of file diff --git a/frappe/custom/doctype/custom_field/custom_field.py b/frappe/custom/doctype/custom_field/custom_field.py index 8f7b21dd24..cb1ea2c54d 100644 --- a/frappe/custom/doctype/custom_field/custom_field.py +++ b/frappe/custom/doctype/custom_field/custom_field.py @@ -54,7 +54,7 @@ class CustomField(Document): old_fieldtype = self.db_get('fieldtype') is_fieldtype_changed = (not self.is_new()) and (old_fieldtype != self.fieldtype) - if is_fieldtype_changed and not CustomizeForm.allow_fieldtype_change(old_fieldtype, self.fieldtype): + if not self.is_virtual and is_fieldtype_changed and not CustomizeForm.allow_fieldtype_change(old_fieldtype, self.fieldtype): frappe.throw(_("Fieldtype cannot be changed from {0} to {1}").format(old_fieldtype, self.fieldtype)) if not self.fieldname: @@ -65,7 +65,7 @@ class CustomField(Document): if not self.flags.ignore_validate: from frappe.core.doctype.doctype.doctype import check_fieldname_conflicts - check_fieldname_conflicts(self.dt, self.fieldname) + check_fieldname_conflicts(self) def on_update(self): if not frappe.flags.in_setup_wizard: diff --git a/frappe/custom/doctype/customize_form/customize_form.json b/frappe/custom/doctype/customize_form/customize_form.json index bdf95ad351..1ee9d4a02a 100644 --- a/frappe/custom/doctype/customize_form/customize_form.json +++ b/frappe/custom/doctype/customize_form/customize_form.json @@ -27,6 +27,7 @@ "autoname", "view_settings_section", "title_field", + "show_title_field_in_link", "image_field", "default_print_format", "column_break_29", @@ -296,6 +297,12 @@ "fieldtype": "Table", "label": "States", "options": "DocType State" + }, + { + "default": "0", + "fieldname": "show_title_field_in_link", + "fieldtype": "Check", + "label": "Show Title in Link Fields" } ], "hide_toolbar": 1, @@ -304,7 +311,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-12-14 16:45:04.308690", + "modified": "2022-01-07 16:07:06.196534", "modified_by": "Administrator", "module": "Custom", "name": "Customize Form", diff --git a/frappe/custom/doctype/customize_form/customize_form.py b/frappe/custom/doctype/customize_form/customize_form.py index 1593ed49a5..92a540447f 100644 --- a/frappe/custom/doctype/customize_form/customize_form.py +++ b/frappe/custom/doctype/customize_form/customize_form.py @@ -418,6 +418,9 @@ class CustomizeForm(Document): return property_value def validate_fieldtype_change(self, df, old_value, new_value): + if df.is_virtual: + return + allowed = self.allow_fieldtype_change(old_value, new_value) if allowed: old_value_length = cint(frappe.db.type_map.get(old_value)[1]) @@ -430,7 +433,8 @@ class CustomizeForm(Document): self.validate_fieldtype_length() else: self.flags.update_db = True - if not allowed: + + else: frappe.throw(_("Fieldtype cannot be changed from {0} to {1} in row {2}").format(old_value, new_value, df.idx)) def validate_fieldtype_length(self): @@ -512,7 +516,8 @@ doctype_properties = { 'email_append_to': 'Check', 'subject_field': 'Data', 'sender_field': 'Data', - 'autoname': 'Data' + 'autoname': 'Data', + 'show_title_field_in_link': 'Check' } docfield_properties = { @@ -558,7 +563,8 @@ docfield_properties = { 'allow_in_quick_entry': 'Check', 'hide_border': 'Check', 'hide_days': 'Check', - 'hide_seconds': 'Check' + 'hide_seconds': 'Check', + 'is_virtual': 'Check', } doctype_link_properties = { diff --git a/frappe/custom/doctype/customize_form_field/customize_form_field.json b/frappe/custom/doctype/customize_form_field/customize_form_field.json index a545cd9fe1..4351e76609 100644 --- a/frappe/custom/doctype/customize_form_field/customize_form_field.json +++ b/frappe/custom/doctype/customize_form_field/customize_form_field.json @@ -14,6 +14,7 @@ "non_negative", "reqd", "unique", + "is_virtual", "in_list_view", "in_standard_filter", "in_global_search", @@ -115,6 +116,12 @@ "fieldtype": "Check", "label": "Unique" }, + { + "default": "0", + "fieldname": "is_virtual", + "fieldtype": "Check", + "label": "Is Virtual" + }, { "default": "0", "fieldname": "in_list_view", @@ -436,7 +443,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2022-01-03 14:50:32.035768", + "modified": "2022-01-27 21:45:22.349776", "modified_by": "Administrator", "module": "Custom", "name": "Customize Form Field", diff --git a/frappe/custom/doctype/property_setter/property_setter.py b/frappe/custom/doctype/property_setter/property_setter.py index 0a65aa6f5d..a86cf5efd6 100644 --- a/frappe/custom/doctype/property_setter/property_setter.py +++ b/frappe/custom/doctype/property_setter/property_setter.py @@ -1,4 +1,4 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE import frappe @@ -18,53 +18,19 @@ class PropertySetter(Document): def validate(self): self.validate_fieldtype_change() + if self.is_new(): delete_property_setter(self.doc_type, self.property, self.field_name, self.row_name) - - # clear cache frappe.clear_cache(doctype = self.doc_type) def validate_fieldtype_change(self): - if self.field_name in not_allowed_fieldtype_change and \ - self.property == 'fieldtype': - frappe.throw(_("Field type cannot be changed for {0}").format(self.field_name)) - - def get_property_list(self, dt): - return frappe.db.get_all('DocField', - fields=['fieldname', 'label', 'fieldtype'], - filters={ - 'parent': dt, - 'fieldtype': ['not in', ('Section Break', 'Column Break', 'Tab Break', 'HTML', 'Read Only', 'Fold') + frappe.model.table_fields], - 'fieldname': ['!=', ''] - }, - order_by='label asc', - as_dict=1 - ) - - def get_setup_data(self): - return { - 'doctypes': frappe.get_all("DocType", pluck="name"), - 'dt_properties': self.get_property_list('DocType'), - 'df_properties': self.get_property_list('DocField') - } - - def get_field_ids(self): - return frappe.db.get_values( - "DocField", - filters={"parent": self.doc_type}, - fieldname=["name", "fieldtype", "label", "fieldname"], - as_dict=True, - ) - - def get_defaults(self): - if not self.field_name: - return frappe.get_all("DocType", filters={"name": self.doc_type}, fields="*")[0] - else: - return frappe.db.get_values( - "DocField", - filters={"fieldname": self.field_name, "parent": self.doc_type}, - fieldname="*", - )[0] + if ( + self.property == 'fieldtype' + and self.field_name in not_allowed_fieldtype_change + ): + frappe.throw( + _("Field type cannot be changed for {0}").format(self.field_name) + ) def on_update(self): if frappe.flags.in_patch: @@ -74,6 +40,7 @@ class PropertySetter(Document): from frappe.core.doctype.doctype.doctype import validate_fields_for_doctype validate_fields_for_doctype(self.doc_type) + def make_property_setter(doctype, fieldname, property, value, property_type, for_doctype = False, validate_fields_for_doctype=True): # WARNING: Ignores Permissions @@ -91,6 +58,7 @@ def make_property_setter(doctype, fieldname, property, value, property_type, for property_setter.insert() return property_setter + def delete_property_setter(doc_type, property, field_name=None, row_name=None): """delete other property setters on this, if this is new""" filters = dict(doc_type=doc_type, property=property) @@ -100,4 +68,3 @@ def delete_property_setter(doc_type, property, field_name=None, row_name=None): filters["row_name"] = row_name frappe.db.delete('Property Setter', filters) - diff --git a/frappe/database/database.py b/frappe/database/database.py index 9fa1ff161c..c833bdeed3 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -177,6 +177,8 @@ class Database(object): raise frappe.QueryTimeoutError(e) elif frappe.conf.db_type == 'postgres': + # TODO: added temporarily + print(e) raise if ignore_ddl and (self.is_missing_column(e) or self.is_missing_table(e) or self.cant_drop_field_or_key(e)): diff --git a/frappe/database/mariadb/framework_mariadb.sql b/frappe/database/mariadb/framework_mariadb.sql index 7c9309ee9f..f2a1206c7c 100644 --- a/frappe/database/mariadb/framework_mariadb.sql +++ b/frappe/database/mariadb/framework_mariadb.sql @@ -224,6 +224,7 @@ CREATE TABLE `tabDocType` ( `email_append_to` int(1) NOT NULL DEFAULT 0, `subject_field` varchar(255) DEFAULT NULL, `sender_field` varchar(255) DEFAULT NULL, + `show_title_field_in_link` int(1) NOT NULL DEFAULT 0, `migration_hash` varchar(255) DEFAULT NULL, PRIMARY KEY (`name`) ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/frappe/database/postgres/framework_postgres.sql b/frappe/database/postgres/framework_postgres.sql index 1662b7b93e..1e79bf67d8 100644 --- a/frappe/database/postgres/framework_postgres.sql +++ b/frappe/database/postgres/framework_postgres.sql @@ -229,6 +229,7 @@ CREATE TABLE "tabDocType" ( "email_append_to" smallint NOT NULL DEFAULT 0, "subject_field" varchar(255) DEFAULT NULL, "sender_field" varchar(255) DEFAULT NULL, + "show_title_field_in_link" smallint NOT NULL DEFAULT 0, "migration_hash" varchar(255) DEFAULT NULL, PRIMARY KEY ("name") ) ; diff --git a/frappe/database/schema.py b/frappe/database/schema.py index dd54385c83..7cab8d42b2 100644 --- a/frappe/database/schema.py +++ b/frappe/database/schema.py @@ -67,7 +67,7 @@ class DBTable: """ get columns from docfields and custom fields """ - fields = self.meta.get_fieldnames_with_value(True) + fields = self.meta.get_fieldnames_with_value(with_field_meta=True) # optional fields like _comments if not self.meta.get('istable'): @@ -85,6 +85,9 @@ class DBTable: }) for field in fields: + if field.get("is_virtual"): + continue + self.columns[field.get('fieldname')] = DbColumn( self, field.get('fieldname'), diff --git a/frappe/desk/doctype/dashboard/dashboard_list.js b/frappe/desk/doctype/dashboard/dashboard_list.js new file mode 100644 index 0000000000..d60a324048 --- /dev/null +++ b/frappe/desk/doctype/dashboard/dashboard_list.js @@ -0,0 +1,16 @@ +frappe.listview_settings['Dashboard'] = { + button: { + show(doc) { + return doc.name; + }, + get_label() { + return frappe.utils.icon("dashboard-list", "sm"); + }, + get_description(doc) { + return __('View {0}', [`${doc.name}`]); + }, + action(doc) { + frappe.set_route('dashboard-view', doc.name); + } + }, +}; \ No newline at end of file diff --git a/frappe/desk/form/load.py b/frappe/desk/form/load.py index 58d5b30103..4d35ebf5e8 100644 --- a/frappe/desk/form/load.py +++ b/frappe/desk/form/load.py @@ -49,7 +49,7 @@ def getdoc(doctype, name, user=None): raise doc.add_seen() - + set_link_titles(doc) frappe.response.docs.append(doc) @frappe.whitelist() @@ -367,6 +367,60 @@ def get_additional_timeline_content(doctype, docname): return contents +def set_link_titles(doc): + link_titles = {} + link_titles.update(get_title_values_for_link_and_dynamic_link_fields(doc)) + link_titles.update(get_title_values_for_table_and_multiselect_fields(doc)) + + send_link_titles(link_titles) + +def get_title_values_for_link_and_dynamic_link_fields(doc, link_fields=None): + link_titles = {} + + if not link_fields: + meta = frappe.get_meta(doc.doctype) + link_fields = meta.get_link_fields() + meta.get_dynamic_link_fields() + + for field in link_fields: + if not doc.get(field.fieldname): + continue + + doctype = field.options if field.fieldtype == "Link" else doc.get(field.options) + + meta = frappe.get_meta(doctype) + if not meta or not (meta.title_field and meta.show_title_field_in_link): + continue + + link_title = frappe.db.get_value( + doctype, doc.get(field.fieldname), meta.title_field, cache=True + ) + link_titles.update({doctype + "::" + doc.get(field.fieldname): link_title}) + + return link_titles + +def get_title_values_for_table_and_multiselect_fields(doc, table_fields=None): + link_titles = {} + + if not table_fields: + meta = frappe.get_meta(doc.doctype) + table_fields = meta.get_table_fields() + + for field in table_fields: + if not doc.get(field.fieldname): + continue + + for value in doc.get(field.fieldname): + link_titles.update(get_title_values_for_link_and_dynamic_link_fields(value)) + + return link_titles + +def send_link_titles(link_titles): + """Append link titles dict in `frappe.local.response`.""" + if "_link_titles" not in frappe.local.response: + frappe.local.response["_link_titles"] = {} + + frappe.local.response["_link_titles"].update(link_titles) + def update_user_info(docinfo): for d in docinfo.communications: frappe.utils.add_user_info(d.sender, docinfo.user_info) @@ -387,3 +441,4 @@ def get_user_info_for_viewers(users): frappe.utils.add_user_info(user, user_info) return user_info + diff --git a/frappe/desk/query_report.py b/frappe/desk/query_report.py index 97bceeb725..9ed956e986 100644 --- a/frappe/desk/query_report.py +++ b/frappe/desk/query_report.py @@ -73,7 +73,7 @@ def get_report_result(report, filters): return res @frappe.read_only() -def generate_report_result(report, filters=None, user=None, custom_columns=None): +def generate_report_result(report, filters=None, user=None, custom_columns=None, report_settings=None): user = user or frappe.session.user filters = filters or [] @@ -108,7 +108,7 @@ def generate_report_result(report, filters=None, user=None, custom_columns=None) result = get_filtered_data(report.ref_doctype, columns, result, user) if cint(report.add_total_row) and result and not skip_total_row: - result = add_total_row(result, columns) + result = add_total_row(result, columns, report_settings=report_settings) return { "result": result, @@ -210,7 +210,7 @@ def get_script(report_name): @frappe.whitelist() @frappe.read_only() -def run(report_name, filters=None, user=None, ignore_prepared_report=False, custom_columns=None): +def run(report_name, filters=None, user=None, ignore_prepared_report=False, custom_columns=None, report_settings=None): report = get_report_doc(report_name) if not user: user = frappe.session.user @@ -238,7 +238,7 @@ def run(report_name, filters=None, user=None, ignore_prepared_report=False, cust dn = "" result = get_prepared_report_result(report, filters, dn, user) else: - result = generate_report_result(report, filters, user, custom_columns) + result = generate_report_result(report, filters, user, custom_columns, report_settings) result["add_total_row"] = report.add_total_row and not result.get( "skip_total_row", False @@ -435,9 +435,19 @@ def build_xlsx_data(columns, data, visible_idx, include_indentation, ignore_visi return result, column_widths -def add_total_row(result, columns, meta=None): +def add_total_row(result, columns, meta=None, report_settings=None): total_row = [""] * len(columns) has_percent = [] + is_tree = False + parent_field = '' + + if report_settings: + if isinstance(report_settings, (str,)): + report_settings = json.loads(report_settings) + + is_tree = report_settings.get('tree') + parent_field = report_settings.get('parent_field') + for i, col in enumerate(columns): fieldtype, options, fieldname = None, None, None if isinstance(col, str): @@ -464,12 +474,12 @@ def add_total_row(result, columns, meta=None): for row in result: if i >= len(row): continue - cell = row.get(fieldname) if isinstance(row, dict) else row[i] if fieldtype in ["Currency", "Int", "Float", "Percent", "Duration"] and flt( cell ): - total_row[i] = flt(total_row[i]) + flt(cell) + if not (is_tree and row.get(parent_field)): + total_row[i] = flt(total_row[i]) + flt(cell) if fieldtype == "Percent" and i not in has_percent: has_percent.append(i) diff --git a/frappe/desk/reportview.py b/frappe/desk/reportview.py index c45fc9bfdd..b0e1f901aa 100644 --- a/frappe/desk/reportview.py +++ b/frappe/desk/reportview.py @@ -262,22 +262,66 @@ def compress(data, args=None): } @frappe.whitelist() -def save_report(): - """save report""" - - data = frappe.local.form_dict - if frappe.db.exists('Report', data['name']): - d = frappe.get_doc('Report', data['name']) +def save_report(name, doctype, report_settings): + """Save reports of type Report Builder from Report View""" + + if frappe.db.exists('Report', name): + report = frappe.get_doc('Report', name) + if report.is_standard == "Yes": + frappe.throw(_("Standard Reports cannot be edited")) + + if report.report_type != "Report Builder": + frappe.throw(_("Only reports of type Report Builder can be edited")) + + if ( + report.owner != frappe.session.user + and not frappe.has_permission("Report", "write") + ): + frappe.throw( + _("Insufficient Permissions for editing Report"), + frappe.PermissionError + ) else: - d = frappe.new_doc('Report') - d.report_name = data['name'] - d.ref_doctype = data['doctype'] - - d.report_type = "Report Builder" - d.json = data['json'] - frappe.get_doc(d).save() - frappe.msgprint(_("{0} is saved").format(d.name), alert=True) - return d.name + report = frappe.new_doc('Report') + report.report_name = name + report.ref_doctype = doctype + + report.report_type = "Report Builder" + report.json = report_settings + report.save(ignore_permissions=True) + frappe.msgprint( + _("Report {0} saved").format(frappe.bold(report.name)), + indicator="green", + alert=True, + ) + return report.name + +@frappe.whitelist() +def delete_report(name): + """Delete reports of type Report Builder from Report View""" + + report = frappe.get_doc("Report", name) + if report.is_standard == "Yes": + frappe.throw(_("Standard Reports cannot be deleted")) + + if report.report_type != "Report Builder": + frappe.throw(_("Only reports of type Report Builder can be deleted")) + + if ( + report.owner != frappe.session.user + and not frappe.has_permission("Report", "delete") + ): + frappe.throw( + _("Insufficient Permissions for deleting Report"), + frappe.PermissionError + ) + + report.delete(ignore_permissions=True) + frappe.msgprint( + _("Report {0} deleted").format(frappe.bold(report.name)), + indicator="green", + alert=True, + ) @frappe.whitelist() @frappe.read_only() diff --git a/frappe/desk/search.py b/frappe/desk/search.py index 95397070ae..3b76953ed1 100644 --- a/frappe/desk/search.py +++ b/frappe/desk/search.py @@ -49,8 +49,10 @@ def sanitize_searchfield(searchfield): # this is called by the Link Field @frappe.whitelist() def search_link(doctype, txt, query=None, filters=None, page_length=20, searchfield=None, reference_doctype=None, ignore_user_permissions=False): - search_widget(doctype, txt.strip(), query, searchfield=searchfield, page_length=page_length, filters=filters, reference_doctype=reference_doctype, ignore_user_permissions=ignore_user_permissions) - frappe.response['results'] = build_for_autosuggest(frappe.response["values"]) + search_widget(doctype, txt.strip(), query, searchfield=searchfield, page_length=page_length, filters=filters, + reference_doctype=reference_doctype, ignore_user_permissions=ignore_user_permissions) + + frappe.response["results"] = build_for_autosuggest(frappe.response["values"], doctype=doctype) del frappe.response["values"] # this is called by the search box @@ -138,6 +140,12 @@ def search_widget(doctype, txt, query=None, searchfield=None, start=0, fields = list(set(fields + json.loads(filter_fields))) formatted_fields = ['`tab%s`.`%s`' % (meta.name, f.strip()) for f in fields] + title_field_query = get_title_field_query(meta) + + # Insert title field query after name + if title_field_query: + formatted_fields.insert(1, title_field_query) + # find relevance as location of search term from the beginning of string `name`. used for sorting results. formatted_fields.append("""locate({_txt}, `tab{doctype}`.`name`) as `_relevance`""".format( _txt=frappe.db.escape((txt or "").replace("%", "").replace("@", "")), doctype=doctype)) @@ -205,11 +213,38 @@ def get_std_fields_list(meta, key): return sflist -def build_for_autosuggest(res): +def get_title_field_query(meta): + title_field = meta.title_field if meta.title_field else None + show_title_field_in_link = meta.show_title_field_in_link if meta.show_title_field_in_link else None + field = None + + if title_field and show_title_field_in_link: + field = "`tab{0}`.{1} as `label`".format(meta.name, title_field) + + return field + +def build_for_autosuggest(res, doctype): results = [] - for r in res: - out = {"value": r[0], "description": ", ".join(unique(cstr(d) for d in r if d)[1:])} - results.append(out) + meta = frappe.get_meta(doctype) + if not (meta.title_field and meta.show_title_field_in_link): + for r in res: + r = list(r) + results.append({ + "value": r[0], + "description": ", ".join(unique(cstr(d) for d in r[1:] if d)) + }) + + else: + title_field_exists = meta.title_field and meta.show_title_field_in_link + _from = 2 if title_field_exists else 1 # to exclude title from description if title_field_exists + for r in res: + r = list(r) + results.append({ + "value": r[0], + "label": r[1] if title_field_exists else None, + "description": ", ".join(unique(cstr(d) for d in r[_from:] if d)) + }) + return results def scrub_custom_query(query, key, txt): @@ -272,3 +307,12 @@ def get_user_groups(): return frappe.get_all('User Group', fields=['name as id', 'name as value'], update={ 'is_group': True }) + +@frappe.whitelist() +def get_link_title(doctype, docname): + meta = frappe.get_meta(doctype) + + if meta.title_field and meta.show_title_field_in_link: + return frappe.db.get_value(doctype, docname, meta.title_field) + + return docname \ No newline at end of file diff --git a/frappe/installer.py b/frappe/installer.py index d892ff4ddc..0948620ab0 100644 --- a/frappe/installer.py +++ b/frappe/installer.py @@ -529,10 +529,9 @@ def extract_sql_gzip(sql_gz_path): import subprocess try: - # dvf - decompress, verbose, force original_file = sql_gz_path decompressed_file = original_file.rstrip(".gz") - cmd = 'gzip -dvf < {0} > {1}'.format(original_file, decompressed_file) + cmd = 'gzip --decompress --force < {0} > {1}'.format(original_file, decompressed_file) subprocess.check_call(cmd, shell=True) except Exception: raise diff --git a/frappe/migrate.py b/frappe/migrate.py index d13fe858f7..eabd0ff3e0 100644 --- a/frappe/migrate.py +++ b/frappe/migrate.py @@ -1,30 +1,54 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE import json import os -import sys +from textwrap import dedent + import frappe -import frappe.translate -import frappe.modules.patch_handler import frappe.model.sync -from frappe.utils.fixtures import sync_fixtures -from frappe.utils.connections import check_connection -from frappe.utils.dashboard import sync_dashboards +import frappe.modules.patch_handler +import frappe.translate from frappe.cache_manager import clear_global_cache -from frappe.desk.notifications import clear_notifications -from frappe.website.utils import clear_website_cache from frappe.core.doctype.language.language import sync_languages -from frappe.modules.utils import sync_customizations from frappe.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs -from frappe.search.website_search import build_index_for_all_routes from frappe.database.schema import add_column +from frappe.desk.notifications import clear_notifications from frappe.modules.patch_handler import PatchType +from frappe.modules.utils import sync_customizations +from frappe.search.website_search import build_index_for_all_routes +from frappe.utils.connections import check_connection +from frappe.utils.dashboard import sync_dashboards +from frappe.utils.fixtures import sync_fixtures +from frappe.website.utils import clear_website_cache + +BENCH_START_MESSAGE = dedent( + """ + Cannot run bench migrate without the services running. + If you are running bench in development mode, make sure that bench is running: + + $ bench start + Otherwise, check the server logs and ensure that all the required services are running. + """ +) -def migrate(verbose=True, skip_failing=False, skip_search_index=False): - '''Migrate all apps to the current version, will: +def atomic(method): + def wrapper(*args, **kwargs): + try: + ret = method(*args, **kwargs) + frappe.db.commit() + return ret + except Exception: + frappe.db.rollback() + raise + + return wrapper + + +class SiteMigration: + """Migrate all apps to the current version, will: - run before migrate hooks - run patches - sync doctypes (schema) @@ -35,70 +59,117 @@ def migrate(verbose=True, skip_failing=False, skip_search_index=False): - sync languages - sync web pages (from /www) - run after migrate hooks - ''' - - service_status = check_connection(redis_services=["redis_cache"]) - if False in service_status.values(): - for service in service_status: - if not service_status.get(service, True): - print("{} service is not running.".format(service)) - print("""Cannot run bench migrate without the services running. -If you are running bench in development mode, make sure that bench is running: + """ -$ bench start + def __init__(self, skip_failing: bool = False, skip_search_index: bool = False) -> None: + self.skip_failing = skip_failing + self.skip_search_index = skip_search_index -Otherwise, check the server logs and ensure that all the required services are running.""") - sys.exit(1) + def setUp(self): + """Complete setup required for site migration + """ + frappe.flags.touched_tables = set() + self.touched_tables_file = frappe.get_site_path("touched_tables.json") + add_column(doctype="DocType", column_name="migration_hash", fieldtype="Data") + clear_global_cache() - touched_tables_file = frappe.get_site_path('touched_tables.json') - if os.path.exists(touched_tables_file): - os.remove(touched_tables_file) + if os.path.exists(self.touched_tables_file): + os.remove(self.touched_tables_file) - try: - add_column(doctype="DocType", column_name="migration_hash", fieldtype="Data") - frappe.flags.touched_tables = set() frappe.flags.in_migrate = True - clear_global_cache() + def tearDown(self): + """Run operations that should be run post schema updation processes + This should be executed irrespective of outcome + """ + frappe.translate.clear_cache() + clear_website_cache() + clear_notifications() + + with open(self.touched_tables_file, "w") as f: + json.dump(list(frappe.flags.touched_tables), f, sort_keys=True, indent=4) + + if not self.skip_search_index: + print(f"Building search index for {frappe.local.site}") + build_index_for_all_routes() + + frappe.publish_realtime("version-update") + frappe.flags.touched_tables.clear() + frappe.flags.in_migrate = False + @atomic + def pre_schema_updates(self): + """Executes `before_migrate` hooks + """ for app in frappe.get_installed_apps(): - for fn in frappe.get_hooks('before_migrate', app_name=app): + for fn in frappe.get_hooks("before_migrate", app_name=app): frappe.get_attr(fn)() - frappe.modules.patch_handler.run_all(skip_failing=skip_failing, patch_type=PatchType.pre_model_sync) + @atomic + def run_schema_updates(self): + """Run patches as defined in patches.txt, sync schema changes as defined in the {doctype}.json files + """ + frappe.modules.patch_handler.run_all(skip_failing=self.skip_failing, patch_type=PatchType.pre_model_sync) frappe.model.sync.sync_all() - frappe.modules.patch_handler.run_all(skip_failing=skip_failing, patch_type=PatchType.post_model_sync) - frappe.translate.clear_cache() + frappe.modules.patch_handler.run_all(skip_failing=self.skip_failing, patch_type=PatchType.post_model_sync) + + @atomic + def post_schema_updates(self): + """Execute pending migration tasks post patches execution & schema sync + This includes: + * Sync `Scheduled Job Type` and scheduler events defined in hooks + * Sync fixtures & custom scripts + * Sync in-Desk Module Dashboards + * Sync customizations: Custom Fields, Property Setters, Custom Permissions + * Sync Frappe's internal language master + * Sync Portal Menu Items + * Sync Installed Applications Version History + * Execute `after_migrate` hooks + """ sync_jobs() sync_fixtures() sync_dashboards() sync_customizations() sync_languages() - frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu() - - # syncs static files - clear_website_cache() - - # updating installed applications data - frappe.get_single('Installed Applications').update_versions() + frappe.get_single("Portal Settings").sync_menu() + frappe.get_single("Installed Applications").update_versions() for app in frappe.get_installed_apps(): - for fn in frappe.get_hooks('after_migrate', app_name=app): + for fn in frappe.get_hooks("after_migrate", app_name=app): frappe.get_attr(fn)() - if not skip_search_index: - # Run this last as it updates the current session - print('Building search index for {}'.format(frappe.local.site)) - build_index_for_all_routes() - - frappe.db.commit() - - clear_notifications() - - frappe.publish_realtime("version-update") - frappe.flags.in_migrate = False - finally: - with open(touched_tables_file, 'w') as f: - json.dump(list(frappe.flags.touched_tables), f, sort_keys=True, indent=4) - frappe.flags.touched_tables.clear() + def required_services_running(self) -> bool: + """Returns True if all required services are running. Returns False and prints + instructions to stdout when required services are not available. + """ + service_status = check_connection(redis_services=["redis_cache"]) + are_services_running = all(service_status.values()) + + if not are_services_running: + for service in service_status: + if not service_status.get(service, True): + print(f"Service {service} is not running.") + print(BENCH_START_MESSAGE) + + return are_services_running + + def run(self, site: str): + """Run Migrate operation on site specified. This method initializes + and destroys connections to the site database. + """ + if not self.required_services_running(): + raise SystemExit(1) + + if site: + frappe.init(site=site) + frappe.connect() + + self.setUp() + try: + self.pre_schema_updates() + self.run_schema_updates() + finally: + self.post_schema_updates() + self.tearDown() + frappe.destroy() diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index 6c67028368..2d9ce59454 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -1,16 +1,14 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE +import datetime import frappe -import datetime from frappe import _ -from frappe.model import default_fields, table_fields, child_table_fields +from frappe.model import child_table_fields, default_fields, display_fieldtypes, table_fields from frappe.model.naming import set_new_name from frappe.model.utils.link_count import notify_link_count from frappe.modules import load_doctype_module -from frappe.model import display_fieldtypes -from frappe.utils import (cint, flt, now, cstr, strip_html, - sanitize_html, sanitize_email, cast_fieldtype) +from frappe.utils import cast_fieldtype, cint, cstr, flt, now, sanitize_html, strip_html from frappe.utils.html_utils import unescape_html from frappe.model.docstatus import DocStatus @@ -35,13 +33,12 @@ def get_controller(doctype): module_name, custom = frappe.db.get_value( "DocType", doctype, ("module", "custom"), cache=True - ) or ["Core", False] + ) or ("Core", False) if custom: - if frappe.db.field_exists("DocType", "is_tree"): - is_tree = frappe.db.get_value("DocType", doctype, "is_tree", cache=True) - else: - is_tree = False + is_tree = frappe.db.get_value( + "DocType", doctype, "is_tree", ignore=True, cache=True + ) _class = NestedSet if is_tree else Document else: class_overrides = frappe.get_hooks('override_doctype_class') @@ -75,9 +72,12 @@ def get_controller(doctype): return site_controllers[doctype] class BaseDocument(object): - ignore_in_getter = ("doctype", "_meta", "meta", "_table_fields", "_valid_columns") + ignore_in_setter = ("doctype", "_meta", "meta", "_table_fields", "_valid_columns") def __init__(self, d): + if d.get("doctype"): + self.doctype = d["doctype"] + self.update(d) self.dont_update_if_missing = [] @@ -105,10 +105,9 @@ class BaseDocument(object): }) """ - # first set default field values of base document - for key in default_fields: - if key in d: - self.set(key, d[key]) + # set name first, as it is used a reference in child document + if "name" in d: + self.name = d["name"] for key, value in d.items(): self.set(key, value) @@ -143,10 +142,14 @@ class BaseDocument(object): else: value = self.__dict__.get(key, default) - if value is None and key not in self.ignore_in_getter \ - and key in (d.fieldname for d in self.meta.get_table_fields()): - self.set(key, []) - value = self.__dict__.get(key) + if value is None and key in ( + d.fieldname for d in self.meta.get_table_fields() + ): + value = [] + self.set(key, value) + + if limit and isinstance(value, (list, tuple)) and len(value) > limit: + value = value[:limit] return value else: @@ -156,6 +159,9 @@ class BaseDocument(object): return self.get(key, filters=filters, limit=1)[0] def set(self, key, value, as_value=False): + if key in self.ignore_in_setter: + return + if isinstance(value, list) and not as_value: self.__dict__[key] = [] self.extend(key, value) @@ -181,6 +187,7 @@ class BaseDocument(object): if isinstance(value, (dict, BaseDocument)): if not self.__dict__.get(key): self.__dict__[key] = [] + value = self._init_child(value, key) self.__dict__[key].append(value) @@ -217,11 +224,11 @@ class BaseDocument(object): def _init_child(self, value, key): if not self.doctype: return value + if not isinstance(value, BaseDocument): - if "doctype" not in value or value['doctype'] is None: - value["doctype"] = self.get_table_field_doctype(key) - if not value["doctype"]: - raise AttributeError(key) + value["doctype"] = self.get_table_field_doctype(key) + if not value["doctype"]: + raise AttributeError(key) value = get_controller(value["doctype"])(value) value.init_valid_columns() @@ -241,7 +248,7 @@ class BaseDocument(object): return value - def get_valid_dict(self, sanitize=True, convert_dates_to_str=False, ignore_nulls = False): + def get_valid_dict(self, sanitize=True, convert_dates_to_str=False, ignore_nulls=False, ignore_virtual=False): d = frappe._dict() for fieldname in self.meta.get_valid_columns(): d[fieldname] = self.get(fieldname) @@ -251,7 +258,26 @@ class BaseDocument(object): continue df = self.meta.get_field(fieldname) - if df: + + if df and df.get("is_virtual"): + if ignore_virtual: + del d[fieldname] + continue + + from frappe.utils.safe_exec import get_safe_globals + + if d[fieldname] is None: + if df.get("options"): + d[fieldname] = frappe.safe_eval( + code=df.get("options"), + eval_globals=get_safe_globals(), + eval_locals={"doc": self}, + ) + else: + _val = getattr(self, fieldname, None) + if _val and not callable(_val): + d[fieldname] = _val + elif df: if df.fieldtype=="Check": d[fieldname] = 1 if cint(d[fieldname]) else 0 @@ -325,6 +351,7 @@ class BaseDocument(object): def as_dict(self, no_nulls=False, no_default_fields=False, convert_dates_to_str=False, no_child_table_fields=False): doc = self.get_valid_dict(convert_dates_to_str=convert_dates_to_str) doc["doctype"] = self.doctype + for df in self.meta.get_table_fields(): children = self.get(df.fieldname) or [] doc[df.fieldname] = [ @@ -372,26 +399,43 @@ class BaseDocument(object): fieldname = [df.fieldname for df in self.meta.get_table_fields() if df.options==doctype] return fieldname[0] if fieldname else None - def db_insert(self): - """INSERT the document (with valid columns) in the database.""" + def db_insert(self, ignore_if_duplicate=False): + """INSERT the document (with valid columns) in the database. + + args: + ignore_if_duplicate: ignore primary key collision + at database level (postgres) + in python (mariadb) + """ if not self.name: # name will be set by document class in most cases set_new_name(self) + conflict_handler = "" + # On postgres we can't implcitly ignore PK collision + # So instruct pg to ignore `name` field conflicts + if ignore_if_duplicate and frappe.db.db_type == "postgres": + conflict_handler = "on conflict (name) do nothing" + if not self.creation: self.creation = self.modified = now() self.created_by = self.modified_by = frappe.session.user # if doctype is "DocType", don't insert null values as we don't know who is valid yet - d = self.get_valid_dict(convert_dates_to_str=True, ignore_nulls = self.doctype in DOCTYPES_FOR_DOCTYPE) + d = self.get_valid_dict( + convert_dates_to_str=True, + ignore_nulls=self.doctype in DOCTYPES_FOR_DOCTYPE, + ignore_virtual=True, + ) columns = list(d) try: frappe.db.sql("""INSERT INTO `tab{doctype}` ({columns}) - VALUES ({values})""".format( - doctype = self.doctype, - columns = ", ".join("`"+c+"`" for c in columns), - values = ", ".join(["%s"] * len(columns)) + VALUES ({values}) {conflict_handler}""".format( + doctype=self.doctype, + columns=", ".join("`"+c+"`" for c in columns), + values=", ".join(["%s"] * len(columns)), + conflict_handler=conflict_handler ), list(d.values())) except Exception as e: if frappe.db.is_primary_key_violation(e): @@ -404,8 +448,11 @@ class BaseDocument(object): self.db_insert() return - frappe.msgprint(_("{0} {1} already exists").format(self.doctype, frappe.bold(self.name)), title=_("Duplicate Name"), indicator="red") - raise frappe.DuplicateEntryError(self.doctype, self.name, e) + if not ignore_if_duplicate: + frappe.msgprint(_("{0} {1} already exists") + .format(self.doctype, frappe.bold(self.name)), + title=_("Duplicate Name"), indicator="red") + raise frappe.DuplicateEntryError(self.doctype, self.name, e) elif frappe.db.is_unique_key_violation(e): # unique constraint @@ -733,7 +780,7 @@ class BaseDocument(object): type_map = frappe.db.type_map - for fieldname, value in self.get_valid_dict().items(): + for fieldname, value in self.get_valid_dict(ignore_virtual=True).items(): df = self.meta.get_field(fieldname) if not df or df.fieldtype == 'Check': @@ -811,7 +858,7 @@ class BaseDocument(object): if frappe.flags.in_install: return - for fieldname, value in self.get_valid_dict().items(): + for fieldname, value in self.get_valid_dict(ignore_virtual=True).items(): if not value or not isinstance(value, str): continue diff --git a/frappe/model/document.py b/frappe/model/document.py index 66a0cef7dd..cb36c18b47 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -249,11 +249,7 @@ class Document(BaseDocument): if getattr(self.meta, "issingle", 0): self.update_single(self.get_valid_dict()) else: - try: - self.db_insert() - except frappe.DuplicateEntryError as e: - if not ignore_if_duplicate: - raise e + self.db_insert(ignore_if_duplicate=ignore_if_duplicate) # children for d in self.get_all_children(): diff --git a/frappe/model/meta.py b/frappe/model/meta.py index 372392f689..a3d167fb9b 100644 --- a/frappe/model/meta.py +++ b/frappe/model/meta.py @@ -14,16 +14,28 @@ Example: ''' +import json +import os from datetime import datetime + import click -import frappe, json, os -from frappe.utils import cstr, cint, cast -from frappe.model import default_fields, no_value_fields, optional_fields, data_fieldtypes, table_fields, child_table_fields -from frappe.model.document import Document + +import frappe +from frappe import _ +from frappe.model import ( + child_table_fields, + data_fieldtypes, + default_fields, + no_value_fields, + optional_fields, + table_fields, +) from frappe.model.base_document import BaseDocument -from frappe.modules import load_doctype_module +from frappe.model.document import Document from frappe.model.workflow import get_workflow_name -from frappe import _ +from frappe.modules import load_doctype_module +from frappe.utils import cast, cint, cstr + def get_meta(doctype, cached=True): if cached: @@ -444,9 +456,16 @@ class Meta(Document): self.permissions = [Document(d) for d in custom_perms] def get_fieldnames_with_value(self, with_field_meta=False): - return [df if with_field_meta else df.fieldname \ - for df in self.fields if df.fieldtype not in no_value_fields] + def is_value_field(docfield): + return not ( + docfield.get("is_virtual") + or docfield.fieldtype in no_value_fields + ) + + if with_field_meta: + return [df for df in self.fields if is_value_field(df)] + return [df.fieldname for df in self.fields if is_value_field(df)] def get_fields_to_check_permissions(self, user_permission_doctypes): fields = self.get("fields", { @@ -546,7 +565,7 @@ class Meta(Document): # For internal links parent doctype will be the key doctype = link.parent_doctype or link.link_doctype # group found - if link.group and group.label == link.group: + if link.group and _(group.label) == _(link.group): if doctype not in group.get('items'): group.get('items').append(doctype) link.added = True diff --git a/frappe/patches.txt b/frappe/patches.txt index db9610a767..0d2a6162c2 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -184,6 +184,7 @@ frappe.patches.v13_0.queryreport_columns frappe.patches.v13_0.jinja_hook frappe.patches.v13_0.update_notification_channel_if_empty frappe.patches.v13_0.set_first_day_of_the_week +execute:frappe.reload_doc('custom', 'doctype', 'custom_field') frappe.patches.v14_0.update_workspace2 # 20.09.2021 frappe.patches.v14_0.save_ratings_in_fraction #23-12-2021 frappe.patches.v14_0.transform_todo_schema diff --git a/frappe/public/icons/timeless/symbol-defs.svg b/frappe/public/icons/timeless/symbol-defs.svg index f2977e3016..bf4e02a7af 100644 --- a/frappe/public/icons/timeless/symbol-defs.svg +++ b/frappe/public/icons/timeless/symbol-defs.svg @@ -814,6 +814,13 @@ + + + + + + + diff --git a/frappe/public/js/frappe/form/controls/base_control.js b/frappe/public/js/frappe/form/controls/base_control.js index ce871c50cb..4ee52d16b8 100644 --- a/frappe/public/js/frappe/form/controls/base_control.js +++ b/frappe/public/js/frappe/form/controls/base_control.js @@ -39,6 +39,9 @@ frappe.ui.form.Control = class BaseControl { if (this.df.get_status) { return this.df.get_status(this); } + if (this.df.is_virtual) { + return "Read"; + } if ((!this.doctype && !this.docname) || this.df.parenttype === 'Web Form' || this.df.is_web_form) { // like in case of a dialog box @@ -52,7 +55,7 @@ frappe.ui.form.Control = class BaseControl { if(explain) console.log("By Hidden Dependency: None"); // eslint-disable-line no-console return "None"; - } else if (cint(this.df.read_only)) { + } else if (cint(this.df.read_only || this.df.is_virtual)) { // eslint-disable-next-line if (explain) console.log("By Read Only: Read"); // eslint-disable-line no-console return "Read"; diff --git a/frappe/public/js/frappe/form/controls/link.js b/frappe/public/js/frappe/form/controls/link.js index 9f02485a9e..2295cad41a 100644 --- a/frappe/public/js/frappe/form/controls/link.js +++ b/frappe/public/js/frappe/form/controls/link.js @@ -29,7 +29,7 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat setTimeout(function() { if(me.$input.val() && me.get_options()) { let doctype = me.get_options(); - let name = me.$input.val(); + let name = me.get_input_value(); me.$link.toggle(true); me.$link_open.attr('href', frappe.utils.get_form_link(doctype, name)); } @@ -69,6 +69,59 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat this.$input_area.find(".link-btn").remove(); } } + set_formatted_input(value) { + super.set_formatted_input(); + if (!value) return; + + if (!this.title_value_map) { + this.title_value_map = {}; + } + this.set_link_title(value); + } + set_link_title(value) { + let doctype = this.get_options(); + + if (!doctype) return; + + if (in_list(frappe.boot.link_title_doctypes, doctype)) { + let link_title = frappe.utils.get_link_title(doctype, value); + if (!link_title) { + link_title = frappe.utils + .fetch_link_title(doctype, value) + .then(link_title => { + this.set_input_value(link_title); + this.title_value_map[link_title] = value; + }); + } else { + this.set_input_value(link_title); + this.title_value_map[link_title] = value; + } + } else { + this.set_input_value(value); + } + } + parse_validate_and_set_in_model(value, e, label) { + if (this.parse) value = this.parse(value, label); + if (label) { + this.label = label; + frappe.utils.add_link_title(this.df.options, value, label); + } + + return this.validate_and_set_in_model(value, e); + } + get_input_value() { + if (this.$input) { + const input_value = this.$input.val(); + return this.title_value_map?.[input_value] || input_value; + } + return null; + } + get_label_value() { + return this.$input ? this.$input.val() : ""; + } + set_input_value(value) { + this.$input && this.$input.val(value); + } open_advanced_search() { var doctype = this.get_options(); if(!doctype) return; @@ -98,7 +151,7 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat } // partially entered name field - frappe.route_options.name_field = this.get_value(); + frappe.route_options.name_field = this.get_label_value(); // reference to calling link frappe._from_link = this; @@ -120,6 +173,11 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat maxItems: 99, autoFirst: true, list: [], + replace: function (suggestion) { + // Override Awesomeplete replace function as it is used to set the input value + // https://github.com/LeaVerou/awesomplete/issues/17104#issuecomment-359185403 + this.input.value = suggestion.label || suggestion.value; + }, data: function (item) { return { label: item.label || item.value, @@ -236,9 +294,11 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat me.selected = false; return; } - var value = me.get_input_value(); - if(value!==me.last_value) { - me.parse_validate_and_set_in_model(value); + let value = me.get_input_value(); + let label = me.get_label_value(); + + if (value !== me.last_value || me.label !== label) { + me.parse_validate_and_set_in_model(value, null, label); } }); @@ -258,14 +318,15 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat // prevent selection on tab var TABKEY = 9; - if(e.keyCode === TABKEY) { + if (e.keyCode === TABKEY) { e.preventDefault(); me.awesomplete.close(); return false; } - if(item.action) { + if (item.action) { item.value = ""; + item.label = ""; item.action.apply(me); } @@ -277,12 +338,12 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat frappe.boot.user.last_selected_values[me.df.options] = item.value; } - me.parse_validate_and_set_in_model(item.value); + me.parse_validate_and_set_in_model(item.value, null, item.label); }); this.$input.on("awesomplete-selectcomplete", function(e) { - var o = e.originalEvent; - if(o.text.value.indexOf("__link_option") !== -1) { + let o = e.originalEvent; + if (o.text.value.indexOf("__link_option") !== -1) { me.$input.val(""); } }); diff --git a/frappe/public/js/frappe/form/controls/multiselect_pills.js b/frappe/public/js/frappe/form/controls/multiselect_pills.js index b4a1ecf30d..bf93ac0dd8 100644 --- a/frappe/public/js/frappe/form/controls/multiselect_pills.js +++ b/frappe/public/js/frappe/form/controls/multiselect_pills.js @@ -83,15 +83,21 @@ frappe.ui.form.ControlMultiSelectPills = class ControlMultiSelectPills extends f } get_pill_html(value) { + const label = this.get_label(value); const encoded_value = encodeURIComponent(value); return ` `; } + get_label(value) { + const item = this._data?.find(d => d.value === value); + return item ? item.label || item.value : null; + } + get_awesomplete_settings() { const settings = super.get_awesomplete_settings(); diff --git a/frappe/public/js/frappe/form/controls/table_multiselect.js b/frappe/public/js/frappe/form/controls/table_multiselect.js index 15dfd9649e..477679bc92 100644 --- a/frappe/public/js/frappe/form/controls/table_multiselect.js +++ b/frappe/public/js/frappe/form/controls/table_multiselect.js @@ -49,7 +49,7 @@ frappe.ui.form.ControlTableMultiSelect = class ControlTableMultiSelect extends f setup_buttons() { this.$input_area.find('.link-btn').remove(); } - parse(value) { + parse(value, label) { const link_field = this.get_link_field(); if (value) { @@ -62,6 +62,7 @@ frappe.ui.form.ControlTableMultiSelect = class ControlTableMultiSelect extends f [link_field.fieldname]: value }); } + frappe.utils.add_link_title(link_field.options, value, label); } this._rows_list = this.rows.map(row => row[link_field.fieldname]); return this.rows; @@ -126,10 +127,12 @@ frappe.ui.form.ControlTableMultiSelect = class ControlTableMultiSelect extends f this.$input_area.prepend(html); } get_pill_html(value) { + const link_field = this.get_link_field(); const encoded_value = encodeURIComponent(value); + const pill_name = frappe.utils.get_link_title(link_field.options, value) || value; return ` `; diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index d5adf6e7bd..a11ddce520 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -334,7 +334,7 @@ frappe.ui.form.Form = class FrappeForm { this.doc = frappe.get_doc(this.doctype, this.docname); // check permissions - if(!this.has_read_permission()) { + if (!this.has_read_permission()) { frappe.show_not_permitted(__(this.doctype) + " " + __(this.docname)); return; } @@ -1363,6 +1363,7 @@ frappe.ui.form.Form = class FrappeForm { set_df_property(fieldname, property, value, docname, table_field, table_row_name=null) { let df; + if (!docname || !table_field) { df = this.get_docfield(fieldname); } else { @@ -1372,8 +1373,10 @@ frappe.ui.form.Form = class FrappeForm { df = frappe.meta.get_docfield(filtered_fields[0].parent, table_field, table_row_name); } } + if (df && df[property] != value) { df[property] = value; + if (table_field && table_row_name) { if (this.fields_dict[fieldname].grid.grid_rows_by_docname[table_row_name]) { this.fields_dict[fieldname].grid.grid_rows_by_docname[table_row_name].refresh_field(fieldname); @@ -1661,23 +1664,17 @@ frappe.ui.form.Form = class FrappeForm { // make new doctype from the current form // will handover to `make_methods` if defined // or will create and match link fields - var me = this; + let me = this; if(this.make_methods && this.make_methods[doctype]) { return this.make_methods[doctype](this); } else if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { this.custom_buttons[__(this.custom_make_buttons[doctype])].trigger('click'); } else { frappe.model.with_doctype(doctype, function() { - var new_doc = frappe.model.get_new_doc(doctype); + let new_doc = frappe.model.get_new_doc(doctype, null, null, true); // set link fields (if found) - frappe.get_meta(doctype).fields.forEach(function(df) { - if(df.fieldtype==='Link' && df.options===me.doctype) { - new_doc[df.fieldname] = me.doc.name; - } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { - new_doc[df.fieldname] = me.doc[df.fieldname]; - } - }); + me.set_link_field(doctype, new_doc); frappe.ui.form.make_quick_entry(doctype, null, null, new_doc); // frappe.set_route('Form', doctype, new_doc.name); @@ -1685,6 +1682,20 @@ frappe.ui.form.Form = class FrappeForm { } } + set_link_field(doctype, new_doc) { + let me = this; + frappe.get_meta(doctype).fields.forEach(function(df) { + if (df.fieldtype === 'Link' && df.options === me.doctype) { + new_doc[df.fieldname] = me.doc.name; + } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { + new_doc[df.fieldname] = me.doc[df.fieldname]; + } else if (df.fieldtype === 'Table' && df.options && df.reqd) { + let row = new_doc[df.fieldname][0]; + me.set_link_field(df.options, row); + } + }); + } + update_in_all_rows(table_fieldname, fieldname, value) { // update the child value in all tables where it is missing if(!value) return; diff --git a/frappe/public/js/frappe/form/formatters.js b/frappe/public/js/frappe/form/formatters.js index fd3fcb1bc7..c39c4046b4 100644 --- a/frappe/public/js/frappe/form/formatters.js +++ b/frappe/public/js/frappe/form/formatters.js @@ -110,12 +110,14 @@ frappe.form.formatters = { Link: function(value, docfield, options, doc) { var doctype = docfield._options || docfield.options; var original_value = value; + let link_title = frappe.utils.get_link_title(doctype, value); + if(value && value.match && value.match(/^['"].*['"]$/)) { value.replace(/^.(.*).$/, "$1"); } if(options && (options.for_print || options.only_value)) { - return value; + return link_title || value; } if(frappe.form.link_formatters[doctype]) { @@ -139,13 +141,14 @@ frappe.form.formatters = { return ` - ${__(options && options.label || value)}`; + data-name="${original_value}" + data-value="${original_value}"> + ${__(options && options.label || link_title || value)}`; } else { - return value; + return link_title || value; } } else { - return value; + return link_title || value; } }, Date: function(value) { diff --git a/frappe/public/js/frappe/form/grid.js b/frappe/public/js/frappe/form/grid.js index c17ca06003..ea90387922 100644 --- a/frappe/public/js/frappe/form/grid.js +++ b/frappe/public/js/frappe/form/grid.js @@ -502,10 +502,9 @@ export default class Grid { set_column_disp(fieldname, show) { if ($.isArray(fieldname)) { - for (var i = 0, l = fieldname.length; i < l; i++) { - var fname = fieldname[i]; - this.get_docfield(fname).hidden = show ? 0 : 1; - this.set_editable_grid_column_disp(fname, show); + for (let field of fieldname) { + this.update_docfield_property(field, "hidden", show); + this.set_editable_grid_column_disp(field, show); } } else { this.get_docfield(fieldname).hidden = show ? 0 : 1; @@ -555,17 +554,17 @@ export default class Grid { } toggle_reqd(fieldname, reqd) { - this.get_docfield(fieldname).reqd = reqd; + this.update_docfield_property(fieldname, "reqd", reqd); this.debounced_refresh(); } toggle_enable(fieldname, enable) { - this.get_docfield(fieldname).read_only = enable ? 0 : 1; + this.update_docfield_property(fieldname, "read_only", enable ? 0 : 1); this.debounced_refresh(); } toggle_display(fieldname, show) { - this.get_docfield(fieldname).hidden = show ? 0 : 1; + this.update_docfield_property(fieldname, "hidden", show ? 0 : 1); this.debounced_refresh(); } diff --git a/frappe/public/js/frappe/form/grid_row.js b/frappe/public/js/frappe/form/grid_row.js index a40f428969..0ee5a180e0 100644 --- a/frappe/public/js/frappe/form/grid_row.js +++ b/frappe/public/js/frappe/form/grid_row.js @@ -5,11 +5,7 @@ export default class GridRow { this.on_grid_fields_dict = {}; this.on_grid_fields = []; $.extend(this, opts); - if (this.doc && this.parent_df.options) { - frappe.meta.make_docfield_copy_for(this.parent_df.options, this.doc.name, this.docfields); - const docfields = frappe.meta.get_docfields(this.parent_df.options, this.doc.name); - this.docfields = docfields.length ? docfields : opts.docfields; - } + this.set_docfields(); this.columns = {}; this.columns_list = []; this.row_check_html = ''; @@ -41,6 +37,22 @@ export default class GridRow { this.set_data(); } } + + set_docfields(update=false) { + if (this.doc && this.parent_df.options) { + frappe.meta.make_docfield_copy_for(this.parent_df.options, this.doc.name, this.docfields); + const docfields = frappe.meta.get_docfields(this.parent_df.options, this.doc.name); + if (update) { + // to maintain references + this.docfields.forEach(df => { + Object.assign(df, docfields.find(d => d.fieldname === df.fieldname)); + }); + } else { + this.docfields = docfields; + } + } + } + set_data() { this.wrapper.data({ "doc": this.doc @@ -148,6 +160,11 @@ export default class GridRow { }, __('Move To'), 'Update'); } refresh() { + // update docfields for new record + if (this.frm && this.doc && this.doc.__islocal) { + this.set_docfields(true); + } + if(this.frm && this.doc) { this.doc = locals[this.doc.doctype][this.doc.name]; } diff --git a/frappe/public/js/frappe/form/layout.js b/frappe/public/js/frappe/form/layout.js index 0de6b1db0d..8c0f8953c5 100644 --- a/frappe/public/js/frappe/form/layout.js +++ b/frappe/public/js/frappe/form/layout.js @@ -547,24 +547,28 @@ frappe.ui.form.Layout = class Layout { } refresh_dependency() { - // Resolve "depends_on" and show / hide accordingly + /** + Resolve "depends_on" and show / hide accordingly + build dependants' dictionary + */ - // build dependants' dictionary let has_dep = false; - for (let fkey in this.fields_list) { - let f = this.fields_list[fkey]; - f.dependencies_clear = true; + const fields = this.fields_list.concat(this.tabs); + + for (let fkey in fields) { + let f = fields[fkey]; if (f.df.depends_on || f.df.mandatory_depends_on || f.df.read_only_depends_on) { has_dep = true; + break; } } if (!has_dep) return; // show / hide based on values - for (let i = this.fields_list.length - 1; i >= 0; i--) { - let f = this.fields_list[i]; + for (let i = fields.length - 1; i >= 0; i--) { + let f = fields[i]; f.guardian_has_value = true; if (f.df.depends_on) { // evaluate guardian diff --git a/frappe/public/js/frappe/form/multi_select_dialog.js b/frappe/public/js/frappe/form/multi_select_dialog.js index bc5f7a9b52..61922a2422 100644 --- a/frappe/public/js/frappe/form/multi_select_dialog.js +++ b/frappe/public/js/frappe/form/multi_select_dialog.js @@ -1,6 +1,6 @@ frappe.ui.form.MultiSelectDialog = class MultiSelectDialog { constructor(opts) { - /* Options: doctype, target, setters, get_query, action, add_filters_group, data_fields, primary_action_label */ + /* Options: doctype, target, setters, get_query, action, add_filters_group, data_fields, primary_action_label, columns */ Object.assign(this, opts); this.for_select = this.doctype == "[Select]"; if (!this.for_select) { @@ -400,23 +400,22 @@ frappe.ui.form.MultiSelectDialog = class MultiSelectDialog { return this.results.filter(res => checked_values.includes(res.name)); } + get_datatable_columns() { + if (this.get_query && this.get_query().query && this.columns) return this.columns; + + if (Array.isArray(this.setters)) + return ["name", ...this.setters.map(df => df.fieldname)]; + + return ["name", ...Object.keys(this.setters)]; + } + make_list_row(result = {}) { var me = this; // Make a head row by default (if result not passed) let head = Object.keys(result).length === 0; let contents = ``; - let columns = ["name"]; - - if ($.isArray(this.setters)) { - for (let df of this.setters) { - columns.push(df.fieldname); - } - } else { - columns = columns.concat(Object.keys(this.setters)); - } - - columns.forEach(function (column) { + this.get_datatable_columns().forEach(function (column) { contents += `
${ head ? `${__(frappe.model.unscrub(column))}` @@ -486,7 +485,7 @@ frappe.ui.form.MultiSelectDialog = class MultiSelectDialog { get_filters_from_setters() { let me = this; - let filters = this.get_query ? this.get_query().filters : {} || {}; + let filters = (this.get_query ? this.get_query().filters : {}) || {}; let filter_fields = []; if ($.isArray(this.setters)) { diff --git a/frappe/public/js/frappe/form/quick_entry.js b/frappe/public/js/frappe/form/quick_entry.js index e412b1dec8..86523d7088 100644 --- a/frappe/public/js/frappe/form/quick_entry.js +++ b/frappe/public/js/frappe/form/quick_entry.js @@ -55,7 +55,7 @@ frappe.ui.form.QuickEntryForm = class QuickEntryForm { // prepare a list of mandatory, bold and allow in quick entry fields this.mandatory = fields.filter(df => { - return ((df.reqd || df.bold || df.allow_in_quick_entry) && !df.read_only); + return ((df.reqd || df.bold || df.allow_in_quick_entry) && !df.read_only && !df.is_virtual); }); } diff --git a/frappe/public/js/frappe/form/save.js b/frappe/public/js/frappe/form/save.js index 934c90f017..da642b7ca5 100644 --- a/frappe/public/js/frappe/form/save.js +++ b/frappe/public/js/frappe/form/save.js @@ -249,30 +249,39 @@ frappe.ui.form.update_calling_link = (newdoc) => { }; if (is_valid_doctype()) { - // set value - if (doc && doc.parentfield) { - //update values for child table - $.each(frappe._from_link.frm.fields_dict[doc.parentfield].grid.grid_rows, function (index, field) { - if (field.doc && field.doc.name === frappe._from_link.docname) { - frappe._from_link.set_value(newdoc.name); + frappe.model.with_doctype(newdoc.doctype, () => { + let meta = frappe.get_meta(newdoc.doctype); + // set value + if (doc && doc.parentfield) { + //update values for child table + $.each(frappe._from_link.frm.fields_dict[doc.parentfield].grid.grid_rows, function (index, field) { + if (field.doc && field.doc.name === frappe._from_link.docname) { + if (meta.title_field && meta.show_title_field_in_link) { + frappe.utils.add_link_title(newdoc.doctype, newdoc.name, newdoc[meta.title_field]); + } + frappe._from_link.set_value(newdoc.name); + } + }); + } else { + if (meta.title_field && meta.show_title_field_in_link) { + frappe.utils.add_link_title(newdoc.doctype, newdoc.name, newdoc[meta.title_field]); } - }); - } else { - frappe._from_link.set_value(newdoc.name); - } + frappe._from_link.set_value(newdoc.name); + } - // refresh field - frappe._from_link.refresh(); + // refresh field + frappe._from_link.refresh(); - // if from form, switch - if (frappe._from_link.frm) { - frappe.set_route("Form", - frappe._from_link.frm.doctype, frappe._from_link.frm.docname) - .then(() => { - frappe.utils.scroll_to(frappe._from_link_scrollY); - }); - } + // if from form, switch + if (frappe._from_link.frm) { + frappe.set_route("Form", + frappe._from_link.frm.doctype, frappe._from_link.frm.docname) + .then(() => { + frappe.utils.scroll_to(frappe._from_link_scrollY); + }); + } - frappe._from_link = null; + frappe._from_link = null; + }); } } diff --git a/frappe/public/js/frappe/form/script_manager.js b/frappe/public/js/frappe/form/script_manager.js index 6169fa75b8..29f1c86d17 100644 --- a/frappe/public/js/frappe/form/script_manager.js +++ b/frappe/public/js/frappe/form/script_manager.js @@ -192,9 +192,18 @@ frappe.ui.form.ScriptManager = class ScriptManager { } function setup_add_fetch(df) { - if ((['Data', 'Read Only', 'Text', 'Small Text', 'Currency', 'Check', 'Attach Image', - 'Text Editor', 'Code', 'Link', 'Float', 'Int', 'Date', 'Select', 'Duration'].includes(df.fieldtype) || df.read_only==1) - && df.fetch_from && df.fetch_from.indexOf(".")!=-1) { + let is_read_only_field = ( + ['Data', 'Read Only', 'Text', 'Small Text', 'Currency', 'Check', 'Text Editor', 'Attach Image', + 'Code', 'Link', 'Float', 'Int', 'Date', 'Select', 'Duration'].includes(df.fieldtype) + || df.read_only == 1 + || df.is_virtual == 1 + ) + + if ( + is_read_only_field + && df.fetch_from + && df.fetch_from.indexOf(".") != -1 + ) { var parts = df.fetch_from.split("."); me.frm.add_fetch(parts[0], parts[1], df.fieldname, df.parent); } diff --git a/frappe/public/js/frappe/form/tab.js b/frappe/public/js/frappe/form/tab.js index c8ca016398..0e740ce49c 100644 --- a/frappe/public/js/frappe/form/tab.js +++ b/frappe/public/js/frappe/form/tab.js @@ -40,7 +40,7 @@ export default class Tab { hide = true; } - hide && this.toggle(false); + this.toggle(!hide); } toggle(show) { diff --git a/frappe/public/js/frappe/list/base_list.js b/frappe/public/js/frappe/list/base_list.js index 8fa512bfc0..d09a1c7043 100644 --- a/frappe/public/js/frappe/list/base_list.js +++ b/frappe/public/js/frappe/list/base_list.js @@ -391,10 +391,10 @@ frappe.views.BaseList = class BaseList { $this.addClass("btn-info"); this.start = 0; - this.page_length = $this.data().value; + this.page_length = this.selected_page_count = $this.data().value; } else if ($this.is(".btn-more")) { this.start = this.start + this.page_length; - this.page_length = 20; + this.page_length = this.selected_page_count || 20; } this.refresh(); }); diff --git a/frappe/public/js/frappe/list/list_view.js b/frappe/public/js/frappe/list/list_view.js index 3cde04313f..5cfc7c75d4 100644 --- a/frappe/public/js/frappe/list/list_view.js +++ b/frappe/public/js/frappe/list/list_view.js @@ -1483,7 +1483,7 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { return [ filter[1], "=", - JSON.stringify([filter[2], filter[3]]), + encodeURIComponent(JSON.stringify([filter[2], filter[3]])), ].join(""); }) .join("&"); @@ -1672,7 +1672,8 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { frappe.model.is_value_type(field_doc) && field_doc.fieldtype !== "Read Only" && !field_doc.hidden && - !field_doc.read_only + !field_doc.read_only && + !field_doc.is_virtual ); }; diff --git a/frappe/public/js/frappe/request.js b/frappe/public/js/frappe/request.js index 35f946df65..5766619d59 100644 --- a/frappe/public/js/frappe/request.js +++ b/frappe/public/js/frappe/request.js @@ -260,6 +260,14 @@ frappe.request.call = function(opts) { $.extend(frappe._messages, data.__messages); } + // sync link titles + if (data._link_titles) { + if (!frappe._link_titles) { + frappe._link_titles = {}; + } + $.extend(frappe._link_titles, data._link_titles); + } + // callbacks var status_code_handler = statusCode[xhr.statusCode().status]; if (status_code_handler) { diff --git a/frappe/public/js/frappe/ui/filters/filter.js b/frappe/public/js/frappe/ui/filters/filter.js index f5726d3a29..91922b0bc1 100644 --- a/frappe/public/js/frappe/ui/filters/filter.js +++ b/frappe/public/js/frappe/ui/filters/filter.js @@ -314,6 +314,10 @@ frappe.ui.Filter = class { return this.utils.get_selected_value(this.field, this.get_condition()); } + get_selected_label() { + return this.utils.get_selected_label(this.field); + } + get_condition() { return this.filter_edit_area.find('.condition').val(); } @@ -361,7 +365,7 @@ frappe.ui.Filter = class { get_filter_button_text() { let value = this.utils.get_formatted_value( this.field, - this.get_selected_value() + this.get_selected_label() || this.get_selected_value() ); return `${__(this.field.df.label)} ${__(this.get_condition())} ${__( value @@ -449,6 +453,12 @@ frappe.ui.filter_utils = { return val; }, + get_selected_label(field) { + if (in_list(["Link", "Dynamic Link"], field.df.fieldtype)) { + return field.get_label_value(); + } + }, + get_default_condition(df) { if (df.fieldtype == 'Data') { return 'like'; diff --git a/frappe/public/js/frappe/utils/common.js b/frappe/public/js/frappe/utils/common.js index b324cecd39..1f3558b367 100644 --- a/frappe/public/js/frappe/utils/common.js +++ b/frappe/public/js/frappe/utils/common.js @@ -259,8 +259,16 @@ frappe.utils.xss_sanitise = function (string, options) { '/': '/' }; const REGEX_SCRIPT = /)<[^<]*)*<\/script>/gi; // used in jQuery 1.7.2 src/ajax.js Line 14 + const REGEX_ALERT = /confirm\(.*\)|alert\(.*\)|prompt\(.*\)/gi; // captures alert, confirm, prompt options = Object.assign({}, DEFAULT_OPTIONS, options); // don't deep copy, immutable beauty. + // Rule 3 - TODO: Check event handlers? + // script and alert should be checked first or else it will be escaped + if (options.strategies.includes('js')) { + sanitised = sanitised.replace(REGEX_SCRIPT, ""); + sanitised = sanitised.replace(REGEX_ALERT, ""); + } + // Rule 1 if (options.strategies.includes('html')) { for (let char in HTML_ESCAPE_MAP) { @@ -270,11 +278,6 @@ frappe.utils.xss_sanitise = function (string, options) { } } - // Rule 3 - TODO: Check event handlers? - if (options.strategies.includes('js')) { - sanitised = sanitised.replace(REGEX_SCRIPT, ""); - } - return sanitised; } diff --git a/frappe/public/js/frappe/utils/utils.js b/frappe/public/js/frappe/utils/utils.js index 725296a121..dc75239ed5 100644 --- a/frappe/public/js/frappe/utils/utils.js +++ b/frappe/public/js/frappe/utils/utils.js @@ -1416,5 +1416,42 @@ Object.assign(frappe.utils, { arr.push(i); } return arr; + }, + + get_link_title(doctype, name) { + if (!doctype || !name || !frappe._link_titles) { + return; + } + + return frappe._link_titles[doctype + "::" + name]; + }, + + add_link_title(doctype, name, value) { + if (!doctype || !name) { + return; + } + + if (!frappe._link_titles) { + // for link titles + frappe._link_titles = {}; + } + + frappe._link_titles[doctype + "::" + name] = value; + }, + + fetch_link_title(doctype, name) { + try { + return frappe.xcall("frappe.desk.search.get_link_title", { + "doctype": doctype, + "docname": name + }).then(title => { + frappe.utils.add_link_title(doctype, name, title); + return title; + }); + } catch (error) { + console.log('Error while fetching link title.'); // eslint-disable-line + console.log(error); // eslint-disable-line + return Promise.resolve(name); + } } }); diff --git a/frappe/public/js/frappe/views/reports/query_report.js b/frappe/public/js/frappe/views/reports/query_report.js index 7ba0a0228f..c226a3a458 100644 --- a/frappe/public/js/frappe/views/reports/query_report.js +++ b/frappe/public/js/frappe/views/reports/query_report.js @@ -578,6 +578,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList { args: { report_name: this.report_name, filters: filters, + report_settings: this.report_settings }, callback: resolve, always: () => this.page.btn_secondary.prop('disabled', false) @@ -834,7 +835,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList { let data = this.data; let columns = this.columns.filter((col) => !col.hidden); - if (this.raw_data.add_total_row) { + if (this.raw_data.add_total_row && !this.report_settings.tree) { data = data.slice(); data.splice(-1, 1); } @@ -854,7 +855,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList { treeView: this.tree_report, layout: 'fixed', cellHeight: 33, - showTotalRow: this.raw_data.add_total_row, + showTotalRow: this.raw_data.add_total_row && !this.report_settings.tree, direction: frappe.utils.is_rtl() ? 'rtl' : 'ltr', hooks: { columnTotal: frappe.utils.report_column_total diff --git a/frappe/public/js/frappe/views/reports/report_view.js b/frappe/public/js/frappe/views/reports/report_view.js index 44d82988e6..d073f5c7f2 100644 --- a/frappe/public/js/frappe/views/reports/report_view.js +++ b/frappe/public/js/frappe/views/reports/report_view.js @@ -18,7 +18,6 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { setup_defaults() { super.setup_defaults(); this.page_title = __('Report:') + ' ' + this.page_title; - this.menu_items = this.report_menu_items(); this.view = 'Report'; const route = frappe.get_route(); @@ -52,6 +51,11 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { this.page.main.addClass('report-view'); } + setup_page() { + this.menu_items = this.report_menu_items(); + super.setup_page(); + } + toggle_side_bar() { super.toggle_side_bar(); // refresh datatable when sidebar is toggled to accomodate extra space @@ -644,6 +648,7 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { // not a cancelled doc && data.docstatus !== 2 && !df.read_only + && !df.is_virtual && !df.hidden // not a standard field i.e., owner, modified_by, etc. && frappe.model.is_non_std_field(df.fieldname)) @@ -1025,7 +1030,7 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { title += ` (${__(doctype)})`; } - const editable = frappe.model.is_non_std_field(fieldname) && !docfield.read_only; + const editable = frappe.model.is_non_std_field(fieldname) && !docfield.read_only && !docfield.is_virtual; const align = (() => { const is_numeric = frappe.model.is_numeric_field(docfield); @@ -1207,7 +1212,7 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { args: { name: name, doctype: this.doctype, - json: JSON.stringify(report_settings) + report_settings: JSON.stringify(report_settings) }, callback:(r) => { if(r.exc) { @@ -1244,6 +1249,17 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { } } + delete_report() { + return frappe.call({ + method: 'frappe.desk.reportview.delete_report', + args: { name: this.report_name }, + callback(response) { + if (response.exc) return; + window.history.back(); + } + }); + } + get_column_widths() { if (this.datatable) { return this.datatable @@ -1465,12 +1481,42 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView { } }); - // save buttons - if(frappe.user.is_report_manager()) { - items = items.concat([ - { label: __('Save'), action: () => this.save_report('save') }, - { label: __('Save As'), action: () => this.save_report('save_as') } - ]); + const can_edit_or_delete = (action) => { + const method = action == "delete" ? "can_delete" : "can_write"; + return ( + this.report_doc + && this.report_doc.is_standard !== "Yes" + && ( + frappe.model[method]("Report") + || this.report_doc.owner === frappe.session.user + ) + ); + }; + + // A user with role Report Manager or Report Owner can save + if (can_edit_or_delete()) { + items.push({ + label: __("Save"), + action: () => this.save_report('save') + }); + } + + // anyone can save as + items.push({ + label: __('Save As'), + action: () => this.save_report('save_as') + }); + + // A user with role Report Manager or Report Owner can delete + if (can_edit_or_delete("delete")) { + items.push({ + label: __("Delete"), + action: () => frappe.confirm( + "Are you sure you want to delete this report?", + () => this.delete_report(), + ), + shortcut: "Shift+Ctrl+D" + }); } // user permissions diff --git a/frappe/public/js/frappe/views/treeview.js b/frappe/public/js/frappe/views/treeview.js index 7179e4ab56..d5c6fb5e80 100644 --- a/frappe/public/js/frappe/views/treeview.js +++ b/frappe/public/js/frappe/views/treeview.js @@ -343,7 +343,7 @@ frappe.views.TreeView = class TreeView { this.ignore_fields = this.opts.ignore_fields || []; var mandatory_fields = $.map(me.opts.meta.fields, function(d) { - return (d.reqd || d.bold && !d.read_only) ? d : null }); + return (d.reqd || d.bold && !d.read_only && !!d.is_virtual) ? d : null }); var opts_field_names = this.fields.map(function(d) { return d.fieldname diff --git a/frappe/public/scss/common/css_variables.scss b/frappe/public/scss/common/css_variables.scss index b8b7f869fa..5871ac3626 100644 --- a/frappe/public/scss/common/css_variables.scss +++ b/frappe/public/scss/common/css_variables.scss @@ -136,6 +136,8 @@ --shadow-md: 0px 8px 14px rgba(25, 39, 52, 0.08), 0px 2px 6px rgba(25, 39, 52, 0.04); --shadow-lg: 0px 18px 22px rgba(25, 39, 52, 0.1), 0px 1px 10px rgba(0, 0, 0, 0.06), 0px 0.5px 5px rgba(25, 39, 52, 0.04); + --drop-shadow: 0px 0.5px 0px rgba(0, 0, 0, 0.05), 0px 0px 0px rgba(0, 0, 0, 0), 0px 2px 4px rgba(0, 0, 0, 0.05); + --modal-shadow: var(--shadow-md); --card-shadow: var(--shadow-sm); --btn-shadow: var(--shadow-xs); diff --git a/frappe/public/scss/desk/list.scss b/frappe/public/scss/desk/list.scss index a49b5a463e..b6cf6d7711 100644 --- a/frappe/public/scss/desk/list.scss +++ b/frappe/public/scss/desk/list.scss @@ -187,7 +187,31 @@ $level-margin-right: 8px; } .list-paging-area, .footnote-area { - border-top: 1px sol var(--border-color); + border-top: 1px solid var(--border-color); + + .btn-group { + box-shadow: var(--drop-shadow); + border-radius: var(--border-radius-md); + + &> .btn:nth-child(2) { + border-left: none; + border-right: none; + } + + .btn-paging { + box-shadow: none; + margin-left: 0px !important; + border: 1px solid var(--dark-border-color); + + &.btn-info { + background-color: var(--gray-400); + border-color: var(--gray-400); + color: var(--white); + font-weight: var(--text-bold); + } + } + } + } .frappe-card { diff --git a/frappe/public/scss/desk/sidebar.scss b/frappe/public/scss/desk/sidebar.scss index 82bd1cdf13..0bb6ba5f40 100644 --- a/frappe/public/scss/desk/sidebar.scss +++ b/frappe/public/scss/desk/sidebar.scss @@ -104,10 +104,10 @@ body[data-route^="Module"] .main-menu { } .sidebar-image-section { + width: min(100%, 170px); cursor: pointer; .sidebar-image { - width: min(100%, 170px); height: auto; max-height: 170px; object-fit: cover; diff --git a/frappe/templates/print_formats/standard.html b/frappe/templates/print_formats/standard.html index 796663a248..f5ce1d3f5a 100644 --- a/frappe/templates/print_formats/standard.html +++ b/frappe/templates/print_formats/standard.html @@ -3,7 +3,7 @@ {% for page in layout %}
- {{ add_header(loop.index, layout|len, doc, letter_head, no_letterhead, footer, print_settings) }} + {{ add_header(loop.index, layout|len, doc, letter_head, no_letterhead, footer, print_settings, print_heading_template) }}
{% if print_settings.repeat_header_footer %} diff --git a/frappe/templates/print_formats/standard_macros.html b/frappe/templates/print_formats/standard_macros.html index 580a41f959..a4129501b0 100644 --- a/frappe/templates/print_formats/standard_macros.html +++ b/frappe/templates/print_formats/standard_macros.html @@ -186,12 +186,12 @@ data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}" {%- endif -%} {% endmacro %} -{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead, footer, print_settings=None) -%} +{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead, footer, print_settings=None, print_heading_template=None) -%} {% if letter_head and not no_letterhead %}
{{ letter_head }}
{% endif %} - {% if doc.print_heading_template %} - {{ frappe.render_template(doc.print_heading_template, {"doc":doc}) }} + {% if print_heading_template %} + {{ frappe.render_template(print_heading_template, {"doc":doc}) }} {% else %}